【发布时间】:2019-06-14 18:17:32
【问题描述】:
我想用 C# 编写代码。
事实上,我想用 C# 编写一个程序,让程序接收一个数字列表,然后接收另一个数字,最后检查接收到的数字在给定列表中出现了多少次。
我从 GitHub 上搜索并获得了以下 C# 代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Array9a
{
class Program
{
static void Main(string[] args)
{
int i, j,N,count;
Console.WriteLine("Enter the Maximum Range for the Array");
N = Convert.ToInt32(Console.ReadLine());
string[] a = new string[N];
int[] freq = new int[N];
for (i = 0; i < N; i++)
{
a[i] = Console.ReadLine();
freq[i] = -1;
}
for (i = 0; i < N; i++)
{
count = 1;
for (j = i + 1; j < N; j++)
{
if (a[i] == a[j])
{
count++;
freq[j] = 0;
}
}
if (freq[i] != 0)
{
freq[i] = count;
}
}
for (i = 0; i < N; i++)
{
if (freq[i] != 1)
{
Console.Write("{0}{1}", a[i], freq[i]);
}
}
Console.ReadLine();
}
}
}
上述代码的输出是所有元素的频率。但我想修改代码,让程序接收一个数字,然后检查给定数字的频率。
最近我在学习 C#。提前致谢
【问题讨论】:
-
那么您是否只想计算给定数字出现的次数?
-
@John 是的。只是程序计算给定数字出现的频率。
-
您似乎希望您的程序计算所有数字?
-
考虑将数据存储在
Dictionary<int, int>而不是数组中。 -
@RoadRunner 你说得对。事实上,我想修改它,只计算给定数字的频率。
标签: c#