torus711 のアレ

主に競技プログラミングの問題について書きます.PC 以外だと数式が表示されないかもしれないです

Codeforces #197, A : Helpful Maths

概要

整数 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;
}