• 1023. Have Fun with Numbers (20)


    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 file 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


    题目大意:求大整数加法的一个变种,一个大整数(N<20),乘以2后的的另一个大整数,判断其中一个大整数是否可以通过位数的变化得到另一个大整数。

    #include<iostream>
    #include<stdio.h>
    #include<cstring>
    using namespace std;
    #define max 22
    int number[max];
    int double_number[max];
    int main(){
    	char num[max];
    	scanf("%s",num);
    	memset(number,0,sizeof(number));
    	memset(double_number,0,sizeof(double_number));
    	int length = strlen(num);
    	int i,j=0,k=0;
    	for(i=length-1;i>=0;i--){
    		int value=num[i]-'0';
    		number[value]++;
    		double_number[i]=value*2;
    	}
    	int sum[max];
    	for(i=length-1,j=0;i>=0;i--,j++){
    		sum[j]=(double_number[i]+k)%10;
    		k=(double_number[i]+k)/10;
    	}
    	if(k!=0){
    		sum[j]=k;
    		j++;
    	}
    	int compare[max];
    	memset(compare,0,sizeof(compare));
    	for(i=0;i<j;i++){
    		compare[sum[i]]++;
    	}
    	int flag=0;
    	for(i=0;i<j;i++){
    		if(compare[i]!=number[i]){
    			flag = 1;
    			break;
    		}
    	}
    	if(flag == 1){
    		printf("No
    ");
    	}
    	else {
    		printf("Yes
    ");
    	}
    	for(i=j-1;i>=0;i--){
    		printf("%d",sum[i]);
    	}
    	printf("
    ");
    } 
    

      

  • 相关阅读:
    剑指Offer_#7_重建二叉树
    剑指Offer_#6_从尾到头打印链表
    剑指Offer_#5_替换空格
    剑指Offer_#4_二维数组中的查找
    Redis详解(十三)- Redis布隆过滤器
    Redis详解(十二)- 缓存穿透、缓存击穿、缓存雪崩
    Redis详解(十一)- 过期删除策略和内存淘汰策略
    Redis详解(十)- 集群模式详解
    Redis详解(九)- 哨兵(Sentinel)模式详解
    Redis详解(八)- 主从复制
  • 原文地址:https://www.cnblogs.com/grglym/p/7659481.html
Copyright © 2020-2023  润新知