Have Fun with Numbers

                 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


题目的大概意思是给定一串不超过20位的数字,然后对其进行*2运算,如果结果的位数及其对应的数字个数都与原来相同的话,则输出Yes,否则输出No, 比如样例的1234567899是10位数,1,2,3,4,5,6,7,8均有1个,9有2个。输出的结果
2469135798,是10位数,同样1,2,3,4,5,6,7,8均有1个,9有2个,符合,所以输出Yes.

// 数字最大为20位的话超出了数值型数组的最大范围,所以考虑用字符型数组接受
//解题思路:用字符型数组接收,定义一个计算函数计算出值,然后再统计长度与个数匹配 

#include <iostream>
#include <string.h>
using namespace std;

void input(char a[]);

//输出 yes / no 
bool cal(char a[], int b[], int c[]);  
//输出计算结果 
void output(int b[], char a[]); 

int main()
{
	char a[21];
	int b[23];
	
	// 数字是由 1- 9构成的 
	int c[10] = {0};
	
	input(a);
	
	if (cal(a, b, c))  cout << "Yes";
	else cout << "No";
	
	cout << '\n';
	
	output(b, a);

} 

void input(char a[])
{
	cin >> a;
}

bool cal(char a[], int b[], int c[])
{
	int i, j, p, q, n = 0, m;
	//统计a数组中的数字数量 
	int d[10] = {0};
	
	i = strlen(a) - 1;
	
	//处理首位从个位数变成两位数
	int u;
	if (a[n] - '0' >= 5)	u = i+1;
	else   u = i;
	
	for ( ; i>=1; i--)
	{//将字符型数字转化为数值型 
		p = a[i]-'0';
		m = p*2 + n;
		//将*2得到的个位数脱离下来
		q = m % 10;
		n = (m - q)/10;
		//计算得到的个位数放进b中 
		b[u] = q;
		u--;
	}
	
	int sum, h; // h用于分解sum 
	
	sum = (a[i]-'0') * 2 + n; 
	
	if (sum >= 10)	
	{
		h = sum % 10;
		sum = (sum - h) / 10;
		b[u] = h;
		b[u-1] = sum;
		return false;
	}
	
	else  b[i] = sum;
	

	
	int total;
	// 对a数组数字的个数进行统计
	for (j=0; a[j] != '\0'; j++)
	{
		total = a[j]- '0';	 
		d[total]++;
	}
	 
	j = 0;
	
	for (j; j<strlen(a); j++)
	{
		total = b[j]; 	  
		c[total]++;
	}	
	
	//前提保证两个数组的长度相等,比较d,c两个数组,相等输出true, 否输出false 
	int x;	
	for (x=0; c[x]!='\0'; x++)
	{
		if (c[x] != d[x])	return false;
	}
	
	return true;
}

void output(int b[], char a[])
{
	int i=0, j;
	
	if (a[i]-'0' >= 5)	 j = strlen(a)+1;
	else   j = strlen(a);

	
	for (; i<j; i++)
		cout << b[i];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值