在C#中,我在排序数组时遇到了困难;具体来说,我试图按降序对频率进行排序,同时让每个频率在左边保持正确的键值或点值。
例如,输出应该类似于现在的输出,但是值将交错而不是26-2。
例如:
26------50
23------754
20------334
25------96
依旧......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
int[] freq = new int[27];
int total;
Random r = new Random();
for (int i = 0; i < 10000; i++)
{
total = r.Next(1, 14);
total += r.Next(1, 14);
freq[total]++;
}
freq = freq.OrderByDescending(x => x).ToArray();
Console.WriteLine("*Total*\t\t*Freq*");
Console.WriteLine();
int key = 26;
for (int i = 0; i <= 24; i++) //Total point range being identified
{
Console.WriteLine(" {0}\t{1,12}", key, freq[i]);
key--;
}
}
}
}