UVa 11371 Number Theory for Newbies (water ver.)

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值