概要
整数 1, 2, 3 と '+' からなる、足し算を表す式が与えられる。
数字が非減少な順序で現れるように書き換えよ。
解法
'+' で分割してソートしてから '+' を挟んで出力します。
コード
typedef vector<int> VI; typedef istringstream ISS; #define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i ) #define ALL( c ) (c).begin(), (c).end() #define PB( n ) push_back( n ) int main() { cin.tie( 0 ); ios::sync_with_stdio( false ); string s; cin >> s; VI as; { replace( ALL( s ), '+', ' ' ); ISS iss( s ); int a; while ( iss >> a ) { cin >> a; as.PB( a ); } } sort( ALL( as ) ); REP( i, 0, as.size() ) { if ( i ) { cout << '+'; } cout << as[i]; } cout << endl; return 0; }