#include <stdio.h> #include <stdlib.h> /* 求数组的最大值和次大值。 */ int main() { int n; while (printf("Please input n:\n"),fflush(stdin), scanf("%d", &n) != EOF){ if (n < 2){ printf("Please input a bigger number.\n"); continue; } printf("Please input %d numbers:\n",&n); int *numbers = (int*)malloc(n*sizeof(int)); for (int i = 0; i < n; ++i) scanf("%d",numbers+i); int max1, max2; if (numbers[0] > numbers[1]){ max1 = numbers[0]; max2 = numbers[1]; } else{ max1 = numbers[1]; max2 = numbers[0]; } for (int i = 2; i < n; ++i) if (numbers[i] > max1){ max2 = max1; max1 = numbers[i]; } else if (numbers[i] > max2) max2 = numbers[i]; printf("The biggest number is: %d\n", max1); printf("The second biggest number is: %d\n", max2); } system("pause"); return 0; }
时间: 2024-10-06 04:50:47