339A. Helpful Maths
- time limit per test2 seconds
- memory limit per test256 megabytes
- inputstandard input
- outputstandard output
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.
老师写下了多个数字的和。学生们应该计算总数。为了简化计算,总和只包含数字1、2和3。不过,这对Xenia来说还不够。她才刚开始数数,所以只有当总和按非降序排列时,她才能计算总和。例如,她不能计算和1+3+2+1,但她可以计算和1+1+2和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.
你有写在黑板上的总数。重新排列总和并打印总和,这样Xenia就可以计算总和。
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.
第一行包含一个非空字符串s——Xenia的总和需要计数。字符串s不包含空格。它只包含数字和字符“+”。此外,字符串s是数字1、2和3的正确和。字符串s的长度最多为100个字符。
Output
Print the new sum that Xenia can count.
打印Xenia可以计算的新金额。
Examples
input1
3+2+1
output1
1+2+3
input2
1+1+3+1+3
output2
1+1+1+3+3
input3
2
output3
2
Solution
数字部分在字符串的2i位置(0<=i<=len/2),对数字部分进行排序并输出
Code
#include <iostream>
using namespace std;
//339A. Helpful Maths
void bubbleSort(int a[], int n)
{
for(int i = 0; i <= n-1; i++)
for(int j = i + 1; j <= n; j++)
if(a[i] > a[j])
swap(a[i], a[j]);
}
int main() {
string str;
cin >> str;
int len =str.size();
if(len == 1){
cout << str << endl;
}else{
int arr[len/2+1];
for(int i = 0;i <= len/2;i++){
arr[i] = str[2*i] - '0';
}
bubbleSort(arr,len/2);
cout << arr[0];
for(int i = 1;i <= len/2;i++){
cout << '+' << arr[i];
}
cout << endl;
}
return 0;
}