Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
104 views
in Technique[技术] by (71.8m points)

c# - Getting back the number of added digits

There is some task i work on and i can't figure out how to do this. The problem is: I have an array of integers and i need to get sum of those numbers (i did it), but then i need to get back those integers from that added figures.

e.g.

array = "56 65 74 100 99 68 86 180 90"

we do add digits of number and make sort by those values then we get back those numbers of added digits.

For example 99 will be 18, 100 will be 1 so in the list 100 will come before 99. When two numbers are the same, we class them as if they were strings (alphabetical ordering) and not numbers:

180 is before 90 since, having the same value (9), it comes before as a string.

sort result = "100 180 90 56 65 74 68 86 99"

    public static string orderWeight(string strng)
    {
        string[] encoded = strng.Split(' '); // getting nums from array
        int[] ordered = encoded.Select(x => x.Sum(c => c - '0'))
                               .OrderBy(x => x).ToArray() // sum of digits

        return null;
    }

then how can i get back those number cause 56 and 65 = 11 and 11. I have no clue =(

The point of my task is just do some sort of added digits whatever

question from:https://stackoverflow.com/questions/65870383/getting-back-the-number-of-added-digits

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You don't need to select the sum, you can just order by the sum so it returns the array with the full numbers in order:

 string[] decoded = encoded.OrderBy(x => x.Sum(c => c - '0')).ToArray()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...