概要
西暦の四つの数字が全て異なる年を「美しい年」であるとする。
与えられた年が美しい年の次の美しい年まで何年かかるか求めよ。
解法
少なくとも 1000 年以内程度で美しい年になるので、一年ずつ加算してそれぞれについてチェック。
コード
typedef ostringstream OSS; #define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i ) #define ALL( c ) (c).begin(), (c).end() bool check( int y ) { OSS oss; oss << y; string s = oss.str(); sort( ALL( s ) ); REP( i, 0, s.size() - 1 ) { if ( s[i] == s[ i + 1 ] ) { return false; } } return true; } int solve( int y ) { for ( int i = y + 1; ; i++ ) { if ( check( i ) ) { return i; } } return -1; } int main() { cin.tie( 0 ); ios::sync_with_stdio( false ); int y; cin >> y; cout << solve( y ) << endl; return 0; }