Jishan's Log 9: Codeforces 399A solution
Codeforces 399A: Helpful Maths
What does the problem want?
The problem states that a teacher has written a summation expression on the board and Xenia must solve it. However, Xenia can count in ascending order ONLY and not in any other way. We must arrange the numbers of the expression in ascending order so that Xenia can count them.
How to solve it?
The Algorithm:
1) Take a string input
2) Take a character array. Iterate through the string and insert the chars in the string into the char array if, and only if the char is not '+'. After that, sort the array and then finally print it.
The Code:
#include<bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int main()
{
fastio;
string s; cin >> s;
char temp[s.length()]; int n = 0;
for(int i=0; i<s.length(); ++i)
{
if(s[i] != '+')
{
temp[n] = s[i];
n++;
}
}
sort(temp, temp+n);
for(int i=0; i<n; ++i)
{
if(i == n-1) cout << temp[i] << "\n";
else cout << temp[i] << "+";
}
}
Comments
Post a Comment