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;}
- UVa 11371 Number Theory for Newbies (water ver.)
- UVa 11371 - Number Theory for Newbies
- UVa 11044 Searching for Nessy (water ver.)
- UVa 10082 WERTYU (water ver.)
- UVa 10696 f91 (water ver.)
- UVa 79 ClockHands (water ver.)
- UVa 11340 Newspaper (water ver.)
- UVa 514 Rails (water ver.)
- UVa 11150 Cola (water ver.)
- UVa 11064-Number Theory
- UVa 11064 - Number Theory
- UVA 11064 -- Number Theory
- UVA, 11064 Number Theory
- HDU 1266 Reverse Number (water ver.)
- UVa 10300 Ecological Premium (water ver.)
- UVa 414 Machined Surfaces (water ver.)
- UVa 490 Rotating Sentences (water ver.)
- UVa 445 Marvelous Mazes (water ver.)
- 如何查看和修改Oracle数据库服务器端的字符集
- Android 手机上安装并运行 Ubuntu 12.04
- allegro中Autosilk top, Silkscreen top 和Assembly top三个什么区别
- Project Euler 4
- 百度地图API地点搜索-获取经纬度
- UVa 11371 Number Theory for Newbies (water ver.)
- Project Euler 5
- 自行获取区域经纬度的工具
- Project Euler 7
- UIScrollView-----简介
- Project Euler 10
- WebService第三步 - 项目开发实战1
- 【黑马程序员】 学习笔记 - Java正则表达式
- gcc中-pthread和-lpthread的区别