C# 排序数组-按降序排序频率
在C#中,我很难对数组进行排序;具体地说,我尝试按降序对频率进行排序,同时让每个频率在左侧保持其适当的键或点值 例如,输出应该类似于它现在所做的,但是值将是交错的,而不是26-2 例: 等等C# 排序数组-按降序排序频率,c#,arrays,sorting,C#,Arrays,Sorting,在C#中,我很难对数组进行排序;具体地说,我尝试按降序对频率进行排序,同时让每个频率在左侧保持其适当的键或点值 例如,输出应该类似于它现在所做的,但是值将是交错的,而不是26-2 例: 等等 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication8 {
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--;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序8
{
班级计划
{
静态void Main(字符串[]参数)
{
int[]freq=新的int[27];
整数合计;
随机r=新随机();
对于(int i=0;i<10000;i++)
{
总计=r.Next(1,14);
总+=r.Next(1,14);
频率[总计]+;
}
freq=freq.OrderByDescending(x=>x).ToArray();
Console.WriteLine(“*Total*\t\t*Freq*”);
Console.WriteLine();
int键=26;
对于(int i=0;i),频率与左边正确的点值不相关。数字不应该从26-2完美地从左边递减。通过编辑您的问题,您能说出您的期望吗?我认为您应该简单地更改for循环for(int i=0;i<26;i++)中的条件更新,但是,左边的交错值,只要这些左值是正确的,在正确的重复的频率上,这个链接是C++的。
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--;
}
}
}