求一组数中的最大值及其位置。例如:数据为:2 7 34 4 23 56 10 45。最大值应为:56,位置应为:
5条回答
技术专家团-小桥流水 2023-04-06 08:53关注不需要输入数组个数吗?下面的代码不需要输入数组元素个数,运行结果如下:

代码:
#include <stdio.h> int main() { int a[100], n=0; int max, index = 0; char ch; while (1) { scanf("%d", &a[n]); if (n == 0) { max = a[0]; index = 0; } else if (a[n] > max) { max = a[n]; index = n; } n++; ch = getchar(); if (ch == '\n')//输入回车符结束 break; } printf("最大值:%d,位置:%d\n", max, index); //位置输出下标,下标从0开始 return 0; }本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 1无用