HDU 4726 Kia's Calculation
来源:互联网 发布:电脑数据自动备份软件 编辑:程序博客网 时间:2024/05/17 08:24
题目链接:
https://www.nitacm.com/problem_show.php?pid=4600
题目:
Doctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 1234+9876, she will get 0. Ghee is angry about this, and makes a hard problem for her to solve:
Now Kia has two integers A and B, she can shuffle the digits in each number as she like, but leading zeros are not allowed. That is to say, for A = 11024, she can rearrange the number as 10124, or 41102, or many other, but 02411 is not allowed.
After she shuffles A and B, she will add them together, in her own way. And what will be the maximum possible sum of A “+” B ?
Input
The rst line has a number T (T <= 25) , indicating the number of test cases.
For each test case there are two lines. First line has the number A, and the second line has the number B.
Both A and B will have same number of digits, which is no larger than 106, and without leading zeros.
Output
For test case X, output “Case #X: ” first, then output the maximum possible sum without leading zeros.
Sample Input
1
5958
3036
Sample Output
Case #1: 8984
题意:
给你两个非常大的数(长度小于1e6),你可以对这两个数上的任意位置进行交换,没有交换次数的限制,然后让你求出这两个数能够组成的最大的和是多少,这里的加法和一般的加法有区别,for exmple:99999+11111=00000,对应的位置为和再 mod10。
题解:
用贪心的思想,先求出两个相加里面有几个9,几个8,几个7。。。。。。最后值得注意的就是前导0的处理。
代码:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define met(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3fconst int maxn = 1e6+1000;char s1[maxn],s2[maxn];int num1[10+10],num2[10+10];int num[maxn];int main(){ int t; int id=1; scanf("%d",&t); while(t--) { scanf("%s%s",s1,s2); int len=strlen(s1); met(num,0); met(num1,0); met(num2,0); for(int i=0; i<len;i++) num1[s1[i]-'0']++; for(int i=0;i<len;i++) num2[s2[i]-'0']++; printf("Case #%d: ",id++); if(len==1) { printf("%d\n",(s1[0]-'0'+s2[0]-'0')%10); continue; } int MAX=-inf; int pos_x=0; int pos_y=0; for(int i=1;i<=9;i++) for(int j=1;j<=9;j++) { if(num1[i]&&num2[j]&&(i+j)%10>MAX) { pos_x=i; pos_y=j; MAX=(i+j)%10; } } num1[pos_x]--; num2[pos_y]--; int length=0; num[length++]=MAX; for(int i=9;i>=0;i--) { for(int j=0;j<=9;j++) { if(num1[j]) { if(j<=i) { int k=i-j; int cnt=min(num1[j],num2[k]); num1[j]-=cnt; num2[k]-=cnt; while(cnt--) num[length++]=i; } else { int k=10+i-j; if(k>9) continue; int cnt=min(num1[j],num2[k]); num1[j]-=cnt; num2[k]-=cnt; while(cnt--) num[length++]=i; } } } } int k=0; for(k=0;k<length-1;k++) { if(num[k]!=0) { break; } } for(int i=k;i<length;i++) printf("%d",num[i]); printf("\n"); }}
- HDU 4726 Kia's Calculation
- HDU 4726 Kia's Calculation
- hdu 4726 Kia's Calculation
- HDU 4726 Kia's Calculation
- HDU 4726 Kia's Calculation
- HDU 4726 Kia's Calculation
- HDU 4726 Kia's Calculation
- HDU 4726 Kia's Calculation
- HDU 4726 Kia's Calculation
- HDU 4726 Kia's Calculation
- HDU 4726 Kia's Calculation 解题报告
- HDU 4726 Kia's Calculation (贪心)
- hdu 4726 Kia's Calculation(贪心)
- hdu 4726 Kia's Calculation (贪心)
- HDU 4726 Kia's Calculation(数学+贪心)
- HDU 4726 Kia's Calculation (贪心算法)
- hdu 4726 Kia's Calculation(贪心,好题)
- Kia's Calculation
- php连接mysql数据库
- python_类
- 产品经理之移动端产品设计-全栈工程师熊盼
- 机器学习和数据挖掘推荐书单
- 《Effective java 第2版》读书笔记--通用程序设计
- HDU 4726 Kia's Calculation
- 数据库表的拆分
- 简单算术表达式求值
- CodeForces 792A New Bus Route
- JAVA基础 数据类型长度
- Linux下编译安装OpenCV(基于C++,生成so库供python调用)
- 证券市场的法律法规体系
- Mapxtreme使用心得
- 太会算计人,往往越不幸