Integer Array Processing Algorithm (9) given any positive integer, evaluate the smallest "no-duplicated plural" (performance optimization) than this number [2014 Baidu pen question]

Source: Internet
Author: User


// Given any positive integer, the result of a positive integer that is greater than this number and the smallest "no-duplicated" and "no-duplicated" is that two adjacent digits are different. // For example, 1101 is the number of duplicates, 1231 is not a complex number. Int GetMinNum (int nNum) {char Temp [20]; char OutTemp [20]; int nLen; int I, j, k; int nTemp; int nCount = 0; // first determine nNumitoa (nNum, Temp, 10); nLen = strlen (Temp); for (I = 0; I <nLen-1; I ++) {// process the number of duplicates in the input string if (Temp [I] = Temp [I + 1]) {if (I = 0) {// 998765if (Temp [I] = '9') {strcpy (OutTemp, "10"); for (j = 1; j <nLen; j ++) {OutTemp [j + 1] = (j % 2) + '0' ;}}// 889765 else {OutTemp [0] = Temp [I]; outTemp [1] = Temp [I] + 1; for (j = 2; j <nLen; j ++) {OutTemp [j] = (j % 2) + '0' ;}} else {for (k = 0; k <I; k ++) {OutTemp [k] = Temp [k];} // 10998765if (Temp [I] = '9') {OutTemp [I-1] = Temp [I-1] + 1; if (I> 2) {if (OutTemp [I-1] = OutTemp [I-2]) {Temp [I-1] + 1 ;}for (j = I; j <nLen; j ++) {OutTemp [j] = (nCount % 2) + '0'; nCount ++ ;}} // 1088765 else {OutTemp [I] = Temp [I]; OutTemp [I + 1] = Temp [I + 1] + 1; for (j = I + 2; j <nLen; j ++) {OutTemp [j] = (nCount % 2) + '0'; nCount ++ ;}} return atoi (OutTemp );}} For (I = nNum + 1; I ++) {itoa (I, Temp, 10); nLen = strlen (Temp); for (j = 0; j <nLen-1; j ++) {// repeated, jump out of the loop. If (Temp [j] = Temp [j + 1]) {break ;}}// no duplicates. I is returned. If (j = nLen-1) {return I;} return 0 ;}

If you are interested, you can split the above functions. It is really a little long, right.

int main(){int nTemp;int nRet;while(1){scanf("%d", &nTemp);if (nTemp==9999){break;}else{nRet = GetMinNum(nTemp);cout << nRet << endl;}}return 0;}



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.