UVa 11371 Number Theory for Newbies (water ver.)

来源:互联网 发布:孙杨牙齿 知乎 编辑:程序博客网 时间:2024/05/19 03:20

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 andb formed by rearranging the digits ofn, such thata-b is maximum.a andb should NOT have leading zeros. You should then show thata-b is a multiple of 9, by expressing it as 9 ×k, wherek is an integer. See the sample output for the correct output format.

Sample Input

1232468

Sample Output

321 - 123 = 198 = 9 * 228642 - 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;}