Re: [問題] 不用<algorithm>怎麼寫啊?

看板CKEFGISC-5th作者 (演算法可以吃嗎?)時間23年前 (2003/05/17 01:24), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/3 (看更多)
※ 引述《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
文章代碼(AID): #-nHxEAI (CKEFGISC-5th)
文章代碼(AID): #-nHxEAI (CKEFGISC-5th)