写一个c程序代码实现给定n个数,找出其中第二小的数
时间: 2023-04-02 19:01:21 浏览: 84
好的,这个问题我可以回答。以下是一个示例代码:
#include <stdio.h>
int main() {
int n, i, smallest, second_smallest;
printf("请输入数字的个数:");
scanf("%d", &n);
int nums[n];
printf("请输入数字:");
for (i = ; i < n; i++) {
scanf("%d", &nums[i]);
}
smallest = nums[];
second_smallest = nums[1];
if (smallest > second_smallest) {
smallest = nums[1];
second_smallest = nums[];
}
for (i = 2; i < n; i++) {
if (nums[i] < smallest) {
second_smallest = smallest;
smallest = nums[i];
} else if (nums[i] < second_smallest) {
second_smallest = nums[i];
}
}
printf("第二小的数是:%d\n", second_smallest);
return ;
}
阅读全文