(PAT 1023) 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

解题思路:

题目的意思是,输入一个数字,将其乘以2倍,观察加倍前数字各位数字出现的次数与加倍后各位出现的次数是否相等,如果相等

那么输出Yes,否则输出No

比如 1234567899 1-8各出现1次,9出现了2次, 2469135798 1-8也是各出现1次,9出现了2次

利用数组模仿大整数运算,将这个数自己与自己相加

然后用两个数组统计加倍前数字出现的次数与加倍后数字出现的次数,比较即可

这题如果两个数字位数不同,那么就直接否定掉(因为可能因为0的出现而误判)

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
//大整数运算法
int mhashTable[10] = { 0 };
int douHashTable[10] = { 0 };
struct mBignum {
	int d[1000];
	int len;
	mBignum() {
		fill(d, d + 1000, 0);
		len = 0;
	}
};
mBignum mchange(char str[]) {
	mBignum res;
	int mlen = strlen(str);
	for (int i = 0; i < mlen; ++i) {
		res.d[i] = str[mlen-1-i] - '0';
	}
	res.len = mlen;
	return res;
}
//大整数加法
mBignum mbigAdd(mBignum a, mBignum b) {
	mBignum c;
	int carry = 0;
	for (int i = 0; i < a.len || i < b.len; ++i) {
		int temp = a.d[i] + b.d[i] + carry;
		c.d[c.len++] = temp % 10;
		carry = temp / 10;
	}
	if (carry != 0) {
		c.d[c.len++] = carry;
	}
	return c;
}

int main() {
	bool isdoubled = true;
	char str[1000];
	scanf("%s", str);
	mBignum bigA = mchange(str);
	mBignum bigC = mbigAdd(bigA, bigA);

	for (int i = 0; i < bigA.len; ++i) {
		mhashTable[bigA.d[i]]++;
	}
	for (int i = 0; i < bigC.len; ++i) {
		douHashTable[bigC.d[i]]++;
	}
	if (bigA.len != bigC.len) {  //如果位数不同的话,直接否定
		isdoubled = false;
	}
	else {
		for (int i = 1; i <= 9; ++i) {
			if (mhashTable[i] != douHashTable[i]) {
				isdoubled = false;
				break;
			}
		}
	}

	if (isdoubled) {
		printf("Yes\n");
	}
	else {
		printf("No\n");
	}
	for (int i = bigC.len - 1; i >= 0; --i) {
		printf("%d", bigC.d[i]);
	}

	system("PAUSE");
	return 0;
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值