C# 用c语言对数组进行排序#
我有急事,有点迷路了。在用户输入的10(或更少)个数字的数组中(我已经完成了这部分),我需要找到第二个最小的数字。我的朋友给我发了这段代码,但我很难理解它并用c#编写它: 解决了!!!:C# 用c语言对数组进行排序#,c#,C#,我有急事,有点迷路了。在用户输入的10(或更少)个数字的数组中(我已经完成了这部分),我需要找到第二个最小的数字。我的朋友给我发了这段代码,但我很难理解它并用c#编写它: 解决了!!!: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { cla
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int vnesena;
int? min1 = null;
int? min2 = null;
for(int i=1; i<11; i=i+1)
{
Console.WriteLine("Vpiši " + i +"." + " število: ");
vnesena = Convert.ToInt32(Console.ReadLine());
if (vnesena == 0)
{
break;
}
if (min1 == null || vnesena < min1)
{
min2 = min1;
min1 = vnesena;
}
else if (vnesena != min1 && (min2==null || vnesena<min2))
{
min2 = vnesena;
}
}
if (min1 == null || min2 == null)
{
Console.WriteLine("Opozorilo o napaki");
}
else
{
Console.WriteLine("Izhod: " + min2);
}
Console.ReadKey();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
int vnesena;
int?min1=null;
int?min2=null;
对于(int i=1;i来解释给出的代码:
将2个变量设置为“无”。(这样以后可以进行检查。int?
如果您想在此处使用null
作为一个概念,则可以使用int
通过值开始循环
获取下一个值
如果未设置最小值或新值低于最小值,则将第二个最低值替换为前一个最低值,并将最低值替换为输入的新值
否则,请检查新值是否与最小值不同,如果未设置最小值或输入的值低于第二个最低值,则用此新值替换第二个最低值
循环完成后,如果没有填入任何一个最小值,则输出不存在这样的值,否则输出第二个最小值
想象一下,如果您必须手动执行此操作,您可能会在遍历数组时跟踪最低值和第二最低值,而程序只是自动执行此过程。问题是什么
这是你朋友给你的东西的粗略翻译,在我的脑海里并不难翻译
int enteredValue;
int? smallest = null, secondSmallest = null;
for (int i = 0; i < 10; i = i + 1)
{
Console.WriteLine("Vpiši " + i+1 + " število: ");
enteredValue = Convert.ToInt32(Console.ReadLine());
if (smallest==null || enteredValue<smallest) {
secondSmallest=smallest;
smallest = enteredValue;
} else if (enteredValue!=smallest && enteredValue<secondSmallest) {
secondSmallest= enteredValue;
}
}
int输入值;
int?最小值=null,次最小值=null;
对于(int i=0;i<10;i=i+1)
{
控制台写入线(“Vpiši”+i+1+“število:”);
enteredValue=Convert.ToInt32(Console.ReadLine());
如果(minimate==null | | enteredValue,那么代码太复杂了,请尝试类似的方法
int[] numbers = new int[10];
for (int i = 0; i < 10; i++)
{
numbers[i] = int.Parse(Console.ReadLine());
}
Array.Sort(numbers);
Console.WriteLine("Second smallest number: " + numbers[1]);
int[]数字=新的int[10];
对于(int i=0;i<10;i++)
{
numbers[i]=int.Parse(Console.ReadLine());
}
数组。排序(数字);
Console.WriteLine(“第二个最小的数字:+数字[1]);
如果代码不太明显,让我解释一下:
声明一个由10个整数组成的数组
循环10次,每次请求用户输入并将输入作为整数放入数组
对数组进行排序,使每个数字按数字顺序排列(先最小后最大)
第一个整数是最小的(在索引0处输入,所以数字[0]),第二个最小的显然是数字[1]
当然,要使这段代码正常工作,您必须在控制台程序中使用这段代码
正如您没有提到是否允许使用内置排序函数等,我假设Array.Sort()是有效的
编辑:您更新了主题,因此我将更改代码以匹配标准
int[] numbers = new int[10];
bool tooShortInput = false;
for (int i = 0; i < 10; i++)
{
int input = int.Parse(Console.ReadLine());
if (input != 0)
{
numbers[i] = input;
}
else
{
if (i == 2)
{
Console.WriteLine("You only entered two numbers!");
tooShortInput = true;
break;
}
else
{
for (int j = 0; j < 10; j++)
{
if (numbers[j] == 0)
{
numbers[j] = 2147483647;
}
}
break;
}
}
}
// Sort the array
int temp = 0;
for (int write = 0; write < numbers.Length; write++) {
for (int sort = 0; sort < numbers.Length - 1; sort++) {
if (numbers[sort] > numbers[sort + 1]) {
temp = numbers[sort + 1];
numbers[sort + 1] = numbers[sort];
numbers[sort] = temp;
}
}
}
if (!tooShortInput)
{
Console.WriteLine("Second smallest number: " + numbers[1]);
}
int[]数字=新的int[10];
bool-tooShortInput=false;
对于(int i=0;i<10;i++)
{
int input=int.Parse(Console.ReadLine());
如果(输入!=0)
{
数字[i]=输入;
}
其他的
{
如果(i==2)
{
WriteLine(“您只输入了两个数字!”);
tooShortInput=true;
打破
}
其他的
{
对于(int j=0;j<10;j++)
{
如果(数字[j]==0)
{
数字[j]=2147483647;
}
}
打破
}
}
}
//对数组进行排序
内部温度=0;
for(int write=0;write数字[排序+1]){
温度=数字[排序+1];
数字[排序+1]=数字[排序];
数字[排序]=温度;
}
}
}
如果(!tooShortInput)
{
Console.WriteLine(“第二个最小的数字:+数字[1]);
}
如果您不理解更新的代码,请告诉我,我会解释
注意:这是用android手机快速编码和测试的,所以很明显,这段代码不是五星质量,甚至不接近,但它符合:-)
您好,TuukkaX。为什么使用循环而不利用Array.Sort方法
int[] numbers = new int[4] { 4, 2, 6, 8 };
Array.Sort(numbers);
int secondSmallestNumber = numbers[1];
这不是C#-这是伪代码。如果你向你的朋友寻求C#帮助,他发给你,他就不是你的朋友!给我们看看你迄今为止写的C。@Jamiec-我想OP的朋友发给他伪代码。很容易翻译成C#或任何其他语言,除非你是编程新手。var val=x.OrderBy(I=>I)。Skip(1)。First()
当然你的教授不会接受这个:)旁注:正确算法的解释可以通过以下方式找到:你期望的答案有多接近于一匙羹?你有什么需要详细说明的吗?我甚至翻译了朋友的算法。如果你想要更多,我建议聘请私人导师为你做这项工作,因为这已经非常接近了,IMO。如果(min1=null | | vnesena