Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. /*
  2. VD1 cho 1 hoan vi n chu cai A,B,C,...
  3. tìm hoán vi sat sau
  4. ACDEB, n=5
  5. ACEDB
  6. ----------------------------------------
  7. VD2 So lon ke tiep
  8. Cho so n chu so x
  9. tim so sat sau cua x
  10. x= 762018294321
  11. y= 762018312249
  12. ----------------------------------------
  13. VD3 To hop chap k cua n= abcde
  14. n=5,k=2 abcde
  15. x=bd
  16. y=?
  17. */
  18.  
  19. /*
  20. Name: Next.CPP
  21. Copyright:
  22. Author:
  23. Date: 23/07/18 18:27
  24. Description:Find the next object
  25. Next Big Number
  26. Next Permutation
  27. Algorithm
  28. */
  29. #include<iostream>
  30. #include<algorithm>
  31. #include<fstream>
  32. using namespace std;
  33. const int MN=1000;
  34. string s;
  35. const char*fn="NEXT.INP";
  36. void Read(){
  37. ifstream f(fn);
  38. f>>s;
  39. f.close();
  40. }
  41. bool NextNum(){
  42. Read();
  43. cout<<s;
  44. int i,j,n=s.length();
  45. cout<<"\n\tlen= "<<n;
  46. // 1.Tim diem gay i
  47. for(i=n-2;i>=0;i--){
  48. if(s[i]<s[i+1]){
  49. break;
  50. }
  51. }
  52. if(i<0) return false;
  53. // 2.Tim diem vuot j
  54. for(j=n-1;j>i;j--){
  55. if(s[j]>s[i]){
  56. break;
  57. }
  58.  
  59. }
  60. char c=s[i];
  61. s[i]=s[j];
  62. s[j]=c;
  63. //Reverse s[i+1...n-1]
  64. i++;
  65. j=n-1;
  66. while(i<j){
  67. c=s[i];
  68. s[i]=s[j];
  69. s[j]=c;
  70. i++;
  71. j--;
  72. }
  73. //987654321
  74. return true;
  75. }
  76. int main(){
  77. if(NextNum()){
  78. cout<<"\nSolution:"<<s;
  79. }else{
  80. cout<<"\nNo Solution";
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement