Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
不用STL的非递归方法递归的我也不会写。。。搜了下搜了个非递归的,还没看明白,先把代码写了出来,求强人讲解下为啥可以这么写- - 算法见:http://blog.sina.com.cn/s/blog_4e4148700100czp2.html #include <iostream> #include <string.h> #include <algorithm> using namespace std; int cmp(char a,char b){ return a<b; } char S[201]; int sz; bool Create(){ bool flag=0; int pos1,pos2; for(int i=sz-2 ;i>=0;i--){ if(S[i]<S[i+1]){ pos1=i; flag=1; break; } } if(flag==0){ return 0; } for(int i=sz-1;i>=pos1;i--){ if(S[i]>S[pos1]){ pos2=i; break; } } int tmp; tmp=S[pos1]; S[pos1]=S[pos2]; S[pos2]=tmp; int cnt=0; char t; char sto[201]; for(int i=pos1+1;i<sz;i++){ sto[cnt]=S[i]; cnt++; } int cc=1; for(int i=cnt-1;i>=0;i--){ S[pos1+cc]=sto[i]; cc++; } return 1; } int main() { cin>>S; sz=strlen(S); sort(S,S+sz,cmp); cout<<S<<endl; while(Create()){ cout<<S<<endl; } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator