1.实现返回数组的上一个序列
#include<iostream>
#include<string>
#include<stdlib.h>
#include<algorithm>
using namespace std;
bool cmp(int ele1,int ele2){
return ele1>ele2;
}
bool next_turn(int data[],int len){
int temp;
for(int a=2;a<=len;a++){
for(int b=len-1;b>len-a;b--){
for(int c=b-1;c>=len-a;c--){
// cout<<data[c]<<"|"<<data[b]<<endl;
if(data[b]<data[c]){
temp=data[b];
data[b]=data[c];
data[c]=temp;
sort(data+c+1,data+len,cmp);
return true;
}
}
}
}
return false;
}
int main(){
int items[4]={4,3,2,1};
while(next_turn(items,4)){
for(int a=0;a<4;a++){
cout<<items[a];
}
cout<<endl;
}
return 0;
}
2.实现返回数组的下一个序列
#include<iostream>
#include<string>
#include<stdlib.h>
#include<algorithm>
using namespace std;
bool next_turn(int data[],int len){
int temp;
for(int a=2;a<=len;a++){
for(int b=len-1;b>len-a;b--){
for(int c=b-1;c>=len-a;c--){
if(data[b]>data[c]){
temp=data[b];
data[b]=data[c];
data[c]=temp;
sort(data+c+1,data+len);
return true;
}
}
}
}
return false;
}
int main(){
int items[4]={1,2,3,4};
while(next_turn(items,4)){
for(int a=0;a<4;a++){
cout<<items[a];
}
cout<<endl;
}
return 0;
}