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\n");
	}
	else {
		printf("Yes\n");
	}
	for(i=j-1;i>=0;i--){
		printf("%d",sum[i]);
	}
	printf("\n");
} 

  

posted @ 2017-10-13 09:27  gaoren  阅读(500)  评论(0)    收藏  举报