用C# 写 :函数1输入nn100个数 函数2找到最小数并跟最前面数交换
下面是用C#实现的代码:
using System;
class Program {
static void Main(string[] args) {
// 输入n个数
Console.Write("请输入n的值:");
int n = int.Parse(Console.ReadLine());
int[] arr = new int[n];
Console.WriteLine("请输入" + n + "个数:");
for (int i = 0; i < n; i++) {
arr[i] = int.Parse(Console.ReadLine());
}
// 找到最小数并与第一个数交换
int minIndex = 0;
for (int i = 1; i < n; i++) {
if (arr[i] < arr[minIndex]) {
minIndex = i;
}
}
int temp = arr[0];
arr[0] = arr[minIndex];
arr[minIndex] = temp;
// 输出交换后的数组
Console.WriteLine("交换后的数组为:");
for (int i = 0; i < n; i++) {
Console.Write(arr[i] + " ");
}
}
}
示例输入输出:
请输入n的值:5
请输入5个数:
3
5
1
4
2
交换后的数组为:
1 5 3 4 2
``
原文地址: https://www.cveoy.top/t/topic/hsIi 著作权归作者所有。请勿转载和采集!