输入一个长数字,并返回所有数字的数组和每个数字的计数

本文关键字:数字 数组 一个 输入 返回 | 更新日期: 2023-09-27 18:12:14

例如,如果输入设置为1234,程序将返回11213141,因为数字1出现一次,数字2出现一次。。。等等。另一个例子:142225=>11234151

我的程序可以很好地处理小输入,但如果输入有10位数或更多,结果将毫无意义。请帮忙。

class Example
{
    // Get sorted(ascending) list for each digit in num
    public static List<int> GetList(long num)
    {
        List<int> listOfInts = new List<int>();
        while (num > 0)
        {
            int remainder = (int) num % 10;
            listOfInts.Add(remainder);
            num = num / 10;
        }
        listOfInts.Sort();
        return listOfInts;
    }
    // Get minimum digit in the list
    public static int getMinimumInt(List<int> l)
    {
        int min = 10;
        foreach (int s in l)
        {
            if (s <= min)
            {
                min = s;
            }
        }
        return min;
    }
    // Get count of the minimum digit specified
    public static int getCount(int i,List<int> l)
    {
        int count = 0;
        foreach (int s in l)
        {
            if (s == i)
            {
                count++;
            }
        }
        return count;   
    }
    public static void Main()
    {
        long input =  1234567891020;  // Arbituary input
        // initialize
        List<int> outputList=new List<int>();  // List that would be eventually outputted
        List<int> listOfInt = new List<int>();
        listOfInt = GetList(input);
        //Loop end till no element left in listOfInt
        while ((listOfInt.ToArray()).Length!=0)
        {
            int item = getMinimumInt(listOfInt);
            int count = getCount(item, listOfInt);
            outputList.Add(item);             // Add the item to be counted 
            outputList.Add(count);            // Add count of the item 
            listOfInt.RemoveRange(0, count); // Remove digits that have been counted
        }
        // Output the list
        foreach (int i in outputList)
        {
            Console.Write(i);
        }
        Console.WriteLine();
        Console.ReadLine();
    }
} 
}

输入一个长数字,并返回所有数字的数组和每个数字的计数

GetList()函数中,将10位以上的long强制转换为整数:

int remainder = (int) num % 10;

试图将一个10位以上的数字放入int中意味着您遇到了32位整数的最高值,即2147483647。这可以解释为什么你的结果看起来很奇怪。

请改用long。如果这还不够,你可以尝试System.Numerics.BigInteger,它将允许你向它添加更多的数字,直到你的内存用完。

您可以使用这种LINQ方法,它不关心数字,只关心字符:

string output = String.Concat(input
    .GroupBy(c => c)
    .Select(g => String.Format("{0}{1}", g.Key, g.Count())));

如果希望结果为long,请使用long.TryParse(output, out longvariable)

        int sourceVal = 12341231;
        string sourceStr = sourceVal.ToString();
        List<char> uniqueChars = null;
#if LINQ
        uniqueChars = sourceStr.ToCharArray().Distinct().ToList();
#else
        uniqueChars = new List<char>();
        foreach (char c in sourceStr)
            if (!uniqueChars.Contains(c))
                uniqueChars.Add(c);
#endif
        string result = "";
        foreach (var wantedChar in uniqueChars)
#if LINQ
            result += wantedChar.ToString() + (sourceStr.Count(f => f == wantedChar)).ToString();
#else
            result += wantedChar.ToString() + (sourceStr.Split(wantedChar).Length - 1).ToString();
#endif
        Console.WriteLine("Result = " + result);

这是我的代码,使其与您的代码保持相似。如果要限制计数,请在末尾使用模数(%10(将计数保持为个位数。

现在看到了林克的另一个答案,我认为这要整洁得多。假设你的最大答案是192939495969798999,如果按字符升序排序,你需要一个long而不是int来存储它。

只需更改

int remainder = (int) num % 10;

int remainder = (int)(num % 10);

第一种是将num转换为int,然后进行mod 10。当num大于int.MaxValue(通常为负数(时,这将导致溢出。第二个先做mod 10,然后做cast,这是安全的,因为mod会产生一个可以很容易地放入int的值。