Topic:
Give a definition: For an integer, if there are two adjacent digits on the same number, then it is called "duplicate number"; now given a positive integer n, the smallest non-"duplicate number" is not less than N.
Ideas:
Assuming that the number of inputs is n, it makes m=n,
Take the minimum two digits of M, B, respectively,
Judge whether the a==b, if the description is the number of repetitions, then recursive call n=m+1; Consider the special case, ab=99, the yield after 100 is still not repeat number, at this time should be recursive call n=m+2;
If a!=b, then move forward one, that is, M=M/10, until m/10=0, and finally return N.
This idea can also be achieved by non-recursive return, see the code.
Code:
#include <iostream>using namespace Std;int calnonrepetitionnum (int n) {if (n<10) return n+1; int A, B; int base=1; int m=n; while (M/10) {b=m%10; a=m/10%10; base*=10; if (a==b) {if (a==9) return Calnonrepetitionnum ((m+2) *BASE/10); else return Calnonrepetitionnum ((m+1) *BASE/10); } m/=10; } return n;} int calnonrepetitionnum_2 (int n) {if (n<10) return n+1; int a,b,m; int base; BOOL Flag=true; while (flag) {flag=false; M=n; base=1; while (M/10) {a=m%10; b=m/10%10; base*=10; if (a==b) {if (a==9) {n= (m+2) *BASE/10; Flag=true; Break } else{n= (m+1) *BASE/10; Flag=true; Break } } M=M/10; }} return n;} int main () {cout << calnonrepetitionnum () << Endl; cout << calnonrepetitionnum_2 () << Endl; return 0;}
Minimum non-"repeating number" (written question)