Interesting algorithms: The implementation of returning non-duplicated numbers

Source: Internet
Author: User
Bingway original works. for reprinting, please indicate the author and source. If two consecutive digits are not the same in decimal format, it is called "No-duplicated plural ". For example, 105, 164, and 198 are "not duplicated and plural", but 11, 100, and 122 are not. The following uses a long type (long-type number A) to return the smallest "no-duplicated number" greater than ". 1 Static   Long Gnext ( Long A)
2 {
3 Long N = A;
4 Int [] =   New   Int [ 100 ];
5 Int CNT =   0 ; // Number of digits
6 While (N >   0 )
7 {
8 A [CNT ++ ] = ( Int ) (N %   10 );
9 N /=   10 ; // Enter several digits to execute several times
10 }
11 Long TMP =   0 ; // Store temporary values
12 For ( Int J = CNT - 1 ; J > =   0 ; J -- ) // Cyclic Value
13 {
14 TMP = TMP *   10   + A [J]; // High Value
15 If (A [J] = A [J +   1 ]) // Compare whether two numbers are equal
16 {
17 TMP = TMP +   1 ; // If the comparison result is equal, 1 is added.
18 }
19
20 }
21 If (CNT >   2 | TMP> 90 ) /// /The number of input digits is greater than two.
22 {
23 CNT = 1 ;
24 For ( Int I = CNT; I >   0 ; I -- )
25 {
26 If (A [I] = A [I -   1 ]) // Compare whether two numbers are equal
27 {
28 TMP = TMP +   1 ;
29 }
30 }
31 }
32 Return TMP;
33 }
34 Static   Long Getnext ( Long A)
35 {
36 Return Gnext ( +   1 );
37 }

Call getnext (), which is very simple and will not be written.

Main function entry

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.