Codeforces 339A Helpful Maths Solution & Explanation

Difficulty : 800

Problem Description

Xenia the beginner mathematician is a third year student at elementary school. She is now learning the addition operation.

The teacher has written down the sum of multiple numbers. Pupils should calculate the sum. To make the calculation easier, the sum only contains numbers 1, 2 and 3. Still, that isn’t enough for Xenia. She is only beginning to count, so she can calculate a sum only if the summands follow in non-decreasing order. For example, she can’t calculate sum 1+3+2+1 but she can calculate sums 1+1+2 and 3+3.

You’ve got the sum that was written on the board. Rearrange the summans and print the sum in such a way that Xenia can calculate the sum.

Input

The first line contains a non-empty string s — the sum Xenia needs to count. String s contains no spaces. It only contains digits and characters “+”. Besides, string s is a correct sum of numbers 1, 2 and 3. String s is at most 100 characters long.

Output

Print the new sum that Xenia can count.

Examples

Input3+2+1
Output1+2+3
Input1+1+3+1+3
Output1+1+1+3+3
Input2
Output2

Solution

In our solution, we turn the string to an integer array and use built-in function to sort the array.

Than we can easily combine it to the result string.

C# Solution

Solution1

string inStr = Console.ReadLine();

string[] numStrs = inStr.Split('+');
int[] nums = new int[numStrs.Length];

for (int i = 0; i < numStrs.Length; i++)
{
    nums[i] = int.Parse(numStrs[i]);
}

Array.Sort(nums);

Console.WriteLine(String.Join("+", nums));

Java Solution

Solution1

import java.util.Scanner;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] numStrs = scanner.nextLine().split("\\+");
        int[] nums = new int[numStrs.length];
        
        for(int i=0;i<numStrs.length;i++){
            nums[i] = Integer.parseInt(numStrs[i]);
        }
        
        Arrays.sort(nums);
        
        String[] sortedStrings = new String[nums.length];
        for (int i = 0; i < nums.length; i++) {
            sortedStrings[i] = String.valueOf(nums[i]);
        }
        
        
        System.out.println(String.join("+", sortedStrings));
    }
}

Python3 Solution

Solution1

inStr = input()

nums = [int(num) for num in inStr.split('+')]
nums.sort()

res_str = '+'.join(map(str, nums))

print(res_str)

JavaScript Solution

Solution1

var inStr = readline()

var numStrs = inStr.split('+');

var nums = numStrs.map(function(numStr) {
    return parseInt(numStr); 
});

nums.sort();

print(nums.join("+"));

Conclusion

Reference

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 339A – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *