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 it digits, the difference between the number we get and the Given number would a Lways is divisible by 9. For example, if the given number is 123, we'll rearrange the digits to get 321. The difference = 321-123 = 198, which is a multiple of 9 (198 = 9x22).
We can prove this fact fairly easily, but since we are do not have 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 LE Ading Zeros. You are should then show Thata-b are a multiple of 9, by expressing it as 9XK, and Wherek is an integer. The sample output for the correct output format.
Sample Input
123
2468
Sample Output
321-123 = 198 = 9 *
8642-2468 = 6174 = 9 * 686
Long long, grave-Egg!
Complete code:
/*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));///This is 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;
}
Author: csdn Blog Synapse7
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/