//給定任意一個正整數,求比這個數大且最小的“不重複數”“不重複數”的含義是相鄰兩位不相同,//例如1101是重複數,1231是不重複數。int GetMinNum(int nNum){char Temp[20];char OutTemp[20];int nLen;int i,j, k;int nTemp;int nCount =0;//先判斷一下nNumitoa(nNum, Temp, 10);nLen = strlen(Temp);for (i=0; i<nLen-1; i++){//處理輸入串裡是重複數的情況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';}}//889765else{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++;}}//1088765else{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++){//有重複,跳出迴圈。if (Temp[j]==Temp[j+1]){break;}}//沒有重複,返回i。if (j==nLen-1){return i;}}return 0;}
有興趣的朋友也可以把上面的函數進行拆分一下,確實有一點長,是吧。
int main(){int nTemp;int nRet;while(1){scanf("%d", &nTemp);if (nTemp==9999){break;}else{nRet = GetMinNum(nTemp);cout << nRet << endl;}}return 0;}