Evaluate the problem of "No duplicated plural", which is larger than the specified number

Source: Internet
Author: User

Given any positive integer, the minimum number is greater than this number. The meaning of the number is that two adjacent digits are different. For example, 1101 is the number of duplicates, and 1201 is not a complex number.

 

#include <stdio.h>#include <stdlib.h>#include <errno.h>#define NUMBER_STR_MAX_LENGTH (128)unsigned find(unsigned);int main(void){    unsigned num = 0;    printf("Please input Unsigned Number : ");    if (scanf("%u", &num) != 1) {        perror("input error!");        return 1;    }    printf("%u\n", find(num));    return 0;    }void str_reverse(unsigned char *str,unsigned length){    unsigned char *prv = NULL;    unsigned char *cur = NULL;    unsigned char swap = '\0';    for (prv = str, cur = str + (length-1)            ; prv < cur; ++prv, --cur){        swap = *prv;        *prv = *cur;        *cur = swap;    }}unsigned find(unsigned number){    unsigned char number_str[NUMBER_STR_MAX_LENGTH] = {'\0'};    unsigned char *prv = NULL;    unsigned char *cur = NULL;    unsigned char *finded = NULL;    unsigned int number_length = 0;    unsigned int result = 0;      int carry = 0;    ++number;    number_length = sprintf(number_str, "%u", number);    str_reverse(number_str, number_length);    for (cur = number_str + (number_length-1), finded = number_str            ; cur > finded - 1 ; --cur ){            if (prv != NULL && *prv == *cur){            finded = cur;            carry = 1;            do {                *cur += carry;                carry = (*cur - '0') / 10u;                *cur = (*cur-'0') % 10u + '0';                ++cur;            } while (carry != 0 && cur < (number_str + number_length));            if (carry > 0){                *cur = '0' + carry ;                ++number_length;            }        }        prv = cur;    }    /* set reset as 010101 */    for (carry = 0; cur > number_str - 1; --cur){        *cur = carry + '0';        carry ^= 1u;    }    str_reverse(number_str, number_length);    sscanf(number_str,"%u",&result);    return result;}

 

Summary:

Ideas

 

Related Article

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.