UVa 11371 Number Theory for Newbies (water ver.)
2013-11-17 23:15
423 查看
11371 - Number Theory for Newbies
Time limit: 1.000 secondshttp://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 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; }
相关文章推荐
- UVa 11371 - Number Theory for Newbies
- UVa 11371 - Number Theory for Newbies
- UVa 11044 Searching for Nessy (water ver.)
- HDU 1266 Reverse Number (water ver.)
- 数学专项number_theory:UVa 10622
- 数学专项number_theory:UVa 11768
- 数学专项number_theory:UVa 10539
- UVa 10370 Above Average (water ver.)
- UVa 11875 Brick Game (water ver.)
- UVa 11727 Cost Cutting (water ver.)
- UVa 10019 Funny Encryption Method (water ver.)
- 数学专项number_theory:UVa 756
- 数学专项number_theory:UVa 11105
- UVa 11150 Cola (water ver.)
- 数学专项number_theory:UVa 718
- UVa 514 Rails (water ver.)
- UVa 12289 One-Two-Three (water ver.)
- UVa 457 Linear Cellular Automata (water ver.)
- UVa 621 Secret Research (water ver.)
- 数学专项number_theory:UVa 294