Minimum non-"repeating number" (written question)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.