这个题目我刚开始是用了两个for循环,第一个for循环求出max和min,第二个for循环里嵌套if求出max和min的下标,刚写完自己感觉就很麻烦,后来看到朋友的思路,挺好的。
代码如下:
int temp1 = 0, temp2 = 0;
// int a[] = {5,-9,32,77,64,-24,14,0,21,45};
//
// for (int i = 0; i < 10; i++) {
// temp1 = a[temp1] > a[i] ? temp1 : i;
// temp2 = a[temp2] < a[i] ? temp2 : i;
// }
//
// printf("数组中的最大值为:a[%d] = %d\n",temp1,a[temp1]);
// printf("数组中的最小值为:a[%d] = %d\n",temp2,a[temp2]);