我在数组中编写了一个 5x6 随机数的代码,如何找到其中最大的数字然后打印它的位置 (x,y)?
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main () {
int array[5][6];
srand(time(NULL));
int x, y;
for(x = 0; x < 5; x++) {
for(y = 0; y < 6; y++) {
array[x][y] = rand() % 31 + 10;
printf("%d \t", array[x][y]);
}
printf("\n");
}
return 0;
}
最佳答案
int maxX = -1, maxY = -1;
int maxValue = 0
for(int x = 0; x < 5; x++) {
for(int y = 0; y < 6; y++) {
if(maxValue <= array[x][y]) {
maxValue = array[x][y];
maxX = x;
maxY = y;
}
}
}
// print maxX and maxY and maxValue
关于C 数组 - 最大数及其位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17947972/