c# - 按数字的总和排序

标签 c# algorithm sorting

例子:

a = "56 65 74 100 99 68 86 180 90",按数字权重排序变为:"100 180 90 56 65 74 68 86 99"

当两个数字具有相同的“权重”时,让我们将它们分类为字符串而不是数字:100 在 180 之前,因为它的“权重”(1) 小于 180 (9) 和 180在 90 之前,因为它具有与之前作为字符串相同的“权重”(9)。

列表中的所有数字都是正数,列表可以为空。

我的测试:

[TestMethod]
public void Test1()
{
     Assert.AreEqual("2000 103 123 4444 99",
         WeightSort.orderWeight("103 123 4444 99 2000"));
}

[TestMethod]
public void Test2()
{
    Assert.AreEqual("11 11 2000 10003 22 123 1234000 44444444 9999",
        WeightSort.orderWeight("2000 10003 1234000 44444444 9999 11 11 22 123"));
}

我的类(class)计算权重的顺序:

public class WeightSort
{
    public static string orderWeight(string strng)
    {
        List<int> list = strng.Split(' ').Select(Int32.Parse).OrderBy(i => i).ToList();
        List<int> SumofNums = new List<int>();
        List<string> SumandNums = new List<string>();
        List<string> SumandNums2 = new List<string>();
        List<string> Nums = new List<string>();

        foreach (var itm in list)
        {
            int num = (int)GetSumOfDigits(itm);
            SumofNums.Add(num);
            SumandNums.Add(itm + "," + num);
        }
        SumofNums = SumofNums.OrderBy(i => i).ToList();
        string txt = "";            
        foreach (var itm in SumofNums)
        {
            var item = itm.ToString();
            if (!Nums.Contains(item))
            {
                foreach (var itm2 in SumandNums)
                {
                    var itm3 = itm2.Split(',');
                    if (item == itm3[1])
                    {
                        SumandNums2.Add(itm2);
                        if (string.IsNullOrEmpty(txt))
                            txt = itm3[0];
                        else
                            txt = txt + " " + itm3[0];
                    }
                }
                Nums.Add(item);
            }            
        }

        return txt;
    }

    static long GetSumOfDigits(long n)
    {
        long num2 = 0;
        long num3 = n;
        long r = 0;
        while (num3 != 0)
        {
            r = num3 % 10;
            num3 = num3 / 10;
            num2 = num2 + r;
        }

        return num2;
    }
}

如果只有一个但没有重复我可以处理。 请帮助我重写我的类(class),以便它也可以处理重复项..

数字总和:

string weights = "103 123 4444 99 2000";

1) 2000, digit sum = 2;
2) 103, digit sum = 4;
3) 123, digit sum = 6;
4) 4444, digit sum = 16;
5) 99, digit sum = 18;

the correct order is "2000 103 123 4444 99"

最佳答案

如果按权重排序,您可以使用 Linq

  • 位数
  • 按字典顺序(“作为字符串”)

实现

  String a = "56 65 74 100 99 68 86 180 90";

  // 100 180 90 56 65 74 68 86 99
  String result = String.Join(" ", a
    .Split(' ')
    .OrderBy(item => item.Sum(ch => ch - '0')) // sum of digits
    .ThenBy(item => item));                    // lexicographic ("as string")

关于c# - 按数字的总和排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33388312/

相关文章:

algorithm - 使用 2 组坐标对真阳性、假阳性和假阴性进行分类

java - 快速文本搜索

具有空字段的 Java 比较器

c++ - 如何使用区分大小写的元素对 std::list 进行排序?

c# - 如何从其他 View 模型调用主视图模型中的函数?

c# - 让asp :calendar cell clickable,不只是数字

c# - 如何在C#中获取Javascript变量值

c# - 根据子元素值(C#、Json.NET)为元素选择 token 模式

algorithm - 有序列表的复合哈希

javascript - 如何在排序之前保存数组的副本?