Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- VD1 cho 1 hoan vi n chu cai A,B,C,...
- tìm hoán vi sat sau
- ACDEB, n=5
- ACEDB
- ----------------------------------------
- VD2 So lon ke tiep
- Cho so n chu so x
- tim so sat sau cua x
- x= 762018294321
- y= 762018312249
- ----------------------------------------
- VD3 To hop chap k cua n= abcde
- n=5,k=2 abcde
- x=bd
- y=?
- */
- /*
- Name: Next.CPP
- Copyright:
- Author:
- Date: 23/07/18 18:27
- Description:Find the next object
- Next Big Number
- Next Permutation
- Algorithm
- */
- #include<iostream>
- #include<algorithm>
- #include<fstream>
- using namespace std;
- const int MN=1000;
- string s;
- const char*fn="NEXT.INP";
- void Read(){
- ifstream f(fn);
- f>>s;
- f.close();
- }
- bool NextNum(){
- Read();
- cout<<s;
- int i,j,n=s.length();
- cout<<"\n\tlen= "<<n;
- // 1.Tim diem gay i
- for(i=n-2;i>=0;i--){
- if(s[i]<s[i+1]){
- break;
- }
- }
- if(i<0) return false;
- // 2.Tim diem vuot j
- for(j=n-1;j>i;j--){
- if(s[j]>s[i]){
- break;
- }
- }
- char c=s[i];
- s[i]=s[j];
- s[j]=c;
- //Reverse s[i+1...n-1]
- i++;
- j=n-1;
- while(i<j){
- c=s[i];
- s[i]=s[j];
- s[j]=c;
- i++;
- j--;
- }
- //987654321
- return true;
- }
- int main(){
- if(NextNum()){
- cout<<"\nSolution:"<<s;
- }else{
- cout<<"\nNo Solution";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement