程序设计基础63 数学问题之大整数乘法

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

一,知识点

1,本题错误的主要原因在于乘法操作的尾巴处理上,我的后续处理没有做好。即对于超出乘数的数位没有很好地进行进位的操作。类似地对于其他的运算操作也别忘了要进行后续处理。

二,我的代码

bign mul(bign num) {
	int carry = 0;
	bign ans;
	for (int i = 0; i < num.len; i++) {
		int temp = num.d[i] * 2 + carry;
		carry = temp / 10;
		ans.d[i] = temp % 10;
		ans.len++;
	}
	return ans;
}

三,标准代码

bign mul(bign num) {
	int carry = 0;
	bign ans;
	for (int i = 0; i < num.len; i++) {
		int temp = num.d[i] * 2 + carry;
		carry = temp / 10;
		ans.d[i] = temp % 10;
		ans.len++;
	}
	while (carry != 0) {
		ans.d[ans.len++] = carry % 10;
		carry /= 10;
	}
	return ans;
}

猜你喜欢

转载自blog.csdn.net/qq2285580599/article/details/84581118