日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

如何用C#找出數(shù)組中只出現(xiàn)了一次的數(shù)字_C#教程

作者:桑榆肖物 ? 更新時間: 2023-01-02 編程語言

前言

.NET 生態(tài)越來越好,初學(xué)的朋友也越來越多。處理同一件簡單的問題,隨著我們知識的積累解決問題的方法也會越來越多。

開始學(xué)習(xí)一門新的語言,我們經(jīng)常會去解決之前用別的語言解決過無數(shù)次的老問題,今天我們來看看這么一道簡單的查重題。

題目

c#輸入十個數(shù),找出其中所有只出現(xiàn)過一次的數(shù)字。

題目分析

讓輸入10個數(shù)字,這個很簡單,控制臺程序用 Console.ReadLine() 然后強(qiáng)制轉(zhuǎn)換為 int。 最后讓找出那個只出現(xiàn)了一次的元素,那么我們可以在輸入過程中處理,也可以輸入完成后處理,可以有以下解決方案。

方法一

首先我們介紹中規(guī)中矩的簡單方法,涉及到 Dictionary 字典的用法。

Dictionary的主要用途是提供快速的基于鍵值的元素查找。Dictionary的結(jié)構(gòu)一般是這樣的:Dictionary<[key], [value]>
我們可以將輸入的 int 為 key,出現(xiàn)的次數(shù)為 value,對每個輸入的數(shù)字進(jìn)行檢索和計(jì)數(shù),最終打印出只出現(xiàn)過一次的數(shù)據(jù):

Dictionary<int, int> input= new Dictionary<int, int>();
for(int i = 0; i < 10; i++)
{
    Console.Write($"請輸入第{i+1}個數(shù):");
    int temp = Convert.ToInt32(Console.ReadLine());
    // 如果存在要添加的
    if (input.ContainsKey(temp))
    {
        // 記錄輸入次數(shù)+1
        input[temp]++;
    }
    else
    {
        // 不存在計(jì)數(shù)1次
        input.Add(temp, 1);
    }
}

Console.WriteLine($"出現(xiàn)過一次的有:");
foreach(var one in input)
{
    if(one.Value == 1)
    {
        Console.WriteLine(one.Key);
    }
}

方法二

我們可以使用 List<int> 記錄用戶的輸入,并同時在每次輸入時查詢之前是否已經(jīng)輸入過,若已經(jīng)輸入過則保存到另一個 List<int> 中。最后比較兩個 List<int> 得出結(jié)論

// 記錄輸入
List<int> numbers = new List<int> { };
// 記錄重復(fù)的
List<int> notthis = new List<int> { };
for (int i = 0; i < 10; i++)
{
    Console.Write($"請輸入第{i + 1}個數(shù):");
    int temp = Convert.ToInt32(Console.ReadLine());
    if (numbers.Contains(temp))
    {
        notthis.Add(temp);
    }
    numbers.Add(temp);
}
Console.WriteLine($"出現(xiàn)過一次的有:");
foreach (int one in numbers)
{
    if (!notthis.Contains(one))
    {
        Console.WriteLine(one);
    }
}

這段后面的部分可以使用 Except 進(jìn)行差集計(jì)算優(yōu)化為:

List<int> haveone = numbers.Except(notthis).ToList();
Console.WriteLine($"出現(xiàn)過一次的有:{string.Join(",", haveone)}");

方法三

我們也可以使用 Linq 來處理,先對其進(jìn)行分組,然后查詢出僅現(xiàn)過1次的數(shù)據(jù)。

List<int> numbers = new List<int> { };
for (int i = 0; i < 10; i++)
{
    Console.Write($"請輸入第{i + 1}個數(shù):");
    int temp = Convert.ToInt32(Console.ReadLine());
    numbers.Add(temp);
}

var linquse = numbers.GroupBy(x => x)
    .Where(g => g.Count() == 1)
    .Select(s => s.Key);
Console.WriteLine($"出現(xiàn)過一次的有:{string.Join(",", linquse)}");

補(bǔ)充:C#在數(shù)組中找出現(xiàn)次數(shù)最多的一個數(shù)

計(jì)數(shù)法:

先選定數(shù)組第一個數(shù),然后從數(shù)組第一個數(shù)字開始計(jì)數(shù),每和選定的數(shù)字相同,計(jì)樹count就+1,一直遍歷完所有的數(shù),count=n(n為自然數(shù))

然后選定第二個數(shù),然后從數(shù)組第一個數(shù)字開始計(jì)數(shù),每和選定的數(shù)字相同,計(jì)樹count就+1,一直遍歷完所有的數(shù),count=n(n為自然數(shù))

以此類推。。。

在比較之前先確認(rèn)選定的數(shù)字在之前是否有被選中參與比較過,代碼如下:

        public static int Search(int[] arrs)
        {            
            int len = arrs.Length;
            int max = 0;  //出現(xiàn)最多的次數(shù)
            int num = 0;  //當(dāng)前的數(shù)字
            
            List<int> temps = new List<int>(); //a
            for (int i = 0; i < len; i++)
            {
                if (temps.Contains(arrs[i])) continue;      //排除之前參與過的數(shù)字        
                int count = 0;
                for (int j = 0; j < len; j++)
                {
                    if (arrs[i] == arrs[j])
                    {
                        count++;
                    }
                }
                if (count > max)
                {
                    max = count;
                    num = arrs[i];
                }
                temps.Add(arrs[i]); //a
            }
            return num;
        }

總結(jié)

原文鏈接:https://blog.csdn.net/marin1993/article/details/128123678

欄目分類
最近更新