您的位置:首页 > 其它

UVa 11371 Number Theory for Newbies (water ver.)

2013-11-17 23:15 423 查看

11371 - Number Theory for Newbies

Time limit: 1.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2366

Given any positive integer, if we permute its digits, the difference between the number we get and the given number will always be divisible by 9. For example, if the given number is 123, we may rearrange the digits to get 321. The difference = 321 - 123
= 198, which is a multiple of 9 (198 = 9 × 22).

We can prove this fact fairly easily, but since we are not having a maths contest, we instead try to illustrate this fact with the help of a computer program.

Input and Output

Each line of input gives a positive integer n (≤ 2000000000). You are to find two integersa and
b formed by rearranging the digits ofn, such that
a-b is maximum.a and
b should NOT have leading zeros. You should then show thata-b is a multiple of 9, by expressing it as 9 ×k, where
k is an integer. See the sample output for the correct output format.

Sample Input

123
2468


Sample Output

321 - 123 = 198 = 9 * 22
8642 - 2468 = 6174 = 9 * 686


要用long long啊坟蛋!

完整代码:

/*0.015s*/

#include<bits/stdc++.h>
using namespace std;

char a[15], b[15];

int main()
{
	int len, i;
	long long diff;
	while (gets(a))
	{
		len = strlen(a);
		sort(a, a + len, greater<char>());
		memcpy(b, a, sizeof(a));///这就是b了
		for (i = 0; i < len; ++i) a[i] = b[len - 1 - i];
		for (i = 0; !(a[i] & 15); ++i)
			;
		swap(a[0], a[i]);
		diff = atoll(b) - atoll(a);
		printf("%s - %s = %lld = 9 * %lld\n", b, a, diff, diff / 9);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: