→ ccas:謝謝…研究中… 推 61.228.55.86 05/15
next_permutation()的話 和acm146的方法是一樣的
c名題精選百則有
#include <iostream>
#include <cstring>
using namespace std;
const int MAX = 64;
inline void swap( char &x, char &y ){
char t = x;
x = y;
y = t;
return;
}
int main( void ){
char str[MAX];
int len;
int p1, p2;
int t;
while( true ){
cin >> str;
if( '#' == str[0] )
break;
len = strlen( str );
if( 1 == len ){
cout << "No Successor" << endl;
continue;
}
for( p1 = len - 1; p1 >= 0; --p1 ){
if( str[p1] < str[p1 + 1] )
break;
}
if( -1 == p1 ){
cout << "No Successor" << endl;
continue;
}
for( p2 = len - 1; p2 >= 0; --p2 ){
if( str[p2] > str[p1] )
break;
}
swap( str[p1], str[p2] );
for( ++p1, t = len - 1; p1 <= (p1 + t ) / 2; ++p1, --t )
swap( str[p1], str[t] );
cout << str << endl;
}
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw)
◆ From: 140.113.123.133