Carry large numbers string processing &&

Have Fun with Numbers


Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!

Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.

Input Specification:

Each input contains one test case. Each case contains one positive integer with no more than 20 digits.

Output Specification:

For each test case, first print in a line "Yes" if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or "No" if not. Then in the next line, print the doubled number.

Sample Input:

1234567899
 

Sample Output:

Yes
2469135798

Title understood by analyzing the maximum number of bits is 20 bits, and even the largest long long type case will explode, and therefore to adopt be treated with a string . 
On the code:
#include<stdio.h>
#include<string.h>

int main(void){
    char num1[21],num2[21];
    scanf("%s",num1);
    char doublenum[21];
    char tmp;
    strcpy(num2,num1);
    int len = strlen (num2);
     // for each digital multiplier after the 2 stored in an array num2 
    int carrybit = 0 ; // store the bits into each operand
char midch;

    // string handling key
for ( int I len = . 1 ; I> = . 1 ; i-- ) {
      // the algorithm according to the procedure given in column artificial calculated by the equation midch
=num2[i]; num2 [I] = ((num2 [I] - ' 0 ' ) * 2 + carrybit)% 10 + ' 0 ' ; // first by 2 plus the carry generated by the previous operation carrybit = ((midch- ' 0 ' ) * 2 + carrybit) / 10 ; // calculate this time the carry, then careful not num2 [i], because num2 [i] has changed Error: carrybit = ((num2 [ i] - '0') * 2 + carrybit) / 10
  } 
  Num2 [
0 ] = (num2 [ 0 ] - ' 0 ' ) * 2 + carrybit + ' 0 ' ; // the highest level do not have to carry, if the most significant bit is greater than 9, the following will be given treatment
IF (num2 [ 0 ]> ' 9 ' ) {// If the multiplier and the number of digits after the original 2 do not match, it is determined no direct printf("No\n"); printf("%d%d",(num2[0]-'0')/10,(num2[0]-'0')%10); printf("%s",&num2[1]); }else{ strcpy(doublenum,num2);
    
//冒泡排序num1 num2 for(int i=0;i<len-1;i++){ for(int j=0;j<len-i-1;j++) if(num2[j]>num2[j+1]){ tmp=num2[j]; num2 [j] = num2 [j + 1 ]; num2[j+1]=tmp; } } for(int i=0;i<len-1;i++){ for(int j=0;j<len-i-1;j++) if(num1[j]>num1[j+1]){ tmp=num1[j]; num1 [j] = num1 [j + 1 ]; num1[j+1]=tmp; } }
if(strcmp(num1,num2)==0){ printf("Yes\n"); printf("%s",doublenum); }else{ printf("No\n"); printf("%s",doublenum); } } return 0; }

Topic Source:

https://pintia.cn/problem-sets/17/problems/263

Guess you like

Origin www.cnblogs.com/WhiteThornZzf/p/12331447.html