Re: [問題] 不用<algorithm>怎麼寫啊?
※ 引述《kxeme (演算法可以吃嗎?)》之銘言:
: 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 ){
len <= 1
: cout << "No Successor" << endl;
: continue;
: }
: for( p1 = len - 1; p1 >= 0; --p1 ){
len - 2 或許會好一點
: if( str[p1] < str[p1 + 1] )
: break;
: }
剛喵了一下 我覺得好像會有問題...有時間在check一下
: 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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 3 篇):