一个数组选出最小的前三个数C语言
以下是一个简单的C语言程序,用于找到数组中最小的前三个数:
#include <stdio.h>
int main() {
int arr[] = {5, 3, 8, 2, 9, 1, 7, 4, 6}; // example array
int i, j, temp, n = 9;
// Selection sort algorithm
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
// Print the smallest three numbers
printf("The smallest three numbers in the array are: %d, %d, %d", arr[0], arr[1], arr[2]);
return 0;
}
这个程序使用选择排序算法对数组进行排序,并打印出数组中最小的前三个数。在本例中,数组中最小的三个数是1、2和3
原文地址: https://www.cveoy.top/t/topic/gIzu 著作权归作者所有。请勿转载和采集!