下标法:
#include <stdio.h>
#define SIZE 10
int main() {
int scores[SIZE] = {85, 90, 76, 92, 88, 95, 82, 89, 91, 87};
int maxScore = scores[0];
int maxIndex = 0;
for (int i = 1; i < SIZE; i++) {
if (scores[i] > maxScore) {
maxScore = scores[i];
maxIndex = i;
}
}
printf("最高分数:%d\n", maxScore);
printf("最高分数学生的序号:%d\n", maxIndex);
return 0;
}
地址法:
#include <stdio.h>
#define SIZE 10
int main() {
int scores[SIZE] = {85, 90, 76, 92, 88, 95, 82, 89, 91, 87};
int maxScore = *scores;
int maxIndex = 0;
for (int i = 1; i < SIZE; i++) {
if (*(scores + i) > maxScore) {
maxScore = *(scores + i);
maxIndex = i;
}
}
printf("最高分数:%d\n", maxScore);
printf("最高分数学生的序号:%d\n", maxIndex);
return 0;