Please help me with this problem
Please help me with this problem
@cbcao263 hey here how you can solve the problem, maintain count of 1, 2 and 3, then first print all one then all two then all three, and can maintain and additional count to see if this is last number to be printed or not and print ‘+’ accordingly.
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
int one, two,three; //maintain count of number
one=0;
two=0;
three=0;
cin>>s;
int n=s.size();
for(int i=0;i<n;i++)
{
if(s[i]=='1')
one++;
else if(s[i]=='2')
two++;
else if(s[i]=='3')
three++;
}
int total=one+two+three;
while(total>0)
{
if(one>0)
{
cout<<1;
one--;
total--;
if(total>0)
cout<<'+';
}
else if(two>0)
{
cout<<2;
two--;
total--;
if(total>0)
cout<<'+';
}
else if(three>0)
{
cout<<3;
three--;
total--;
if(total>0)
cout<<'+';
}
}
return 0;
}
Code is bit lengthy as I am performing simple iterations and keeps program simple to understand.
If this resolves your doubt mark it as resolved.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.