UVa 11371 - Number Theory for Newbies
来源:互联网 发布:js转化为字符串 编辑:程序博客网 时间:2024/06/10 11:14
題目:給你一個數字n,將裡面每位的數重新組合形成a,b,使得a-b最大且是9的倍數。
分析:數論。題目要求a,b和n的位數相同,不能有前導0。
定理1:交換一個數字中的某兩個位的數,形成的新數組和原數字之差是9的倍數;
證明1:設數字為abc..i..j...xwz,其中每个字母代表一个位,对应值可以相同,
那么随意交换两位i,j得到的新数字为abc..j..i..xwz,做差为9..90..0 *(i-j),
所以一定是9的倍数,得证。
通過上面定理可以繼續證明,任意交換任意位數字形成的新數字和原數字的差是9的倍數;
所以取a為最大組合,即遞減序,b為遞增序即可;
但是,這裡要求位數相同,所以b取首尾不是0的數字的最小值,先取遞增序,
然後,交換第一個非0數和首位的1即可。
說明:╮(╯▽╰)╭。
#include <algorithm>#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <cmath>using namespace std;bool cmp1(char a, char b){return a < b;}bool cmp2(char a, char b){return a > b;}int main(){char buf[31];while (gets(buf)) {int len = strlen(buf);long long A = 0LL, B = 0LL;sort(buf, buf+len, cmp1);if (buf[0] == '0') {for (int i = 0; i < len; ++ i)if (buf[i] != '0') {swap(buf[i], buf[0]);break;}}for (int i = 0; i < len; ++ i)A = A*10LL + buf[i]-'0';sort(buf, buf+len, cmp2);for (int i = 0; i < len; ++ i) B = B*10LL + buf[i]-'0'; printf("%lld - %lld = %lld = 9 * %lld\n",B,A,B-A,(B-A)/9LL);} return 0;}
0 0
- UVa 11371 - Number Theory for Newbies
- UVa 11371 Number Theory for Newbies (water ver.)
- UVa 11064-Number Theory
- UVa 11064 - Number Theory
- UVA 11064 -- Number Theory
- UVA, 11064 Number Theory
- Number theory
- Number theory
- Installing LAMP On Ubuntu For Newbies
- 20 Useful Commands for Linux Newbies
- Shell Script Execution Guidelines for Newbies
- 20 Useful Commands for Linux Newbies
- Number Theory 4.1 Divisibility
- Number Theory 4.2 Primes
- 【索引】Maths - Number Theory
- HDU 4215 Number Theory?
- Number Theory Template
- acdream Number theory
- 244 16进制的简单运算
- Count the Colors
- 今天开始写博客
- 堆排序
- MySQL的binlog数据如何查看
- UVa 11371 - Number Theory for Newbies
- 抓包之安卓app内请求
- 《opencv入门教程》(迭代器遍历)
- 错排问题--十二金钗
- SQL 合并两个结果集 一个倒序,一个正序
- 用mysql编译:Java Web开发实战经典(基础篇)课后题答案 060501_〖第05章:JSP基础语法〗_第01题
- POJ1113(Wall) && Hdu1392(Surround the Trees) (计算几何--凸包)
- mysql相关时间函数
- /dev/fb0入门练习(linux FrameBuffer)