四川省ACM竞赛(2013)---K - Kia's Calculation
来源:互联网 发布:vmware player安装mac 编辑:程序博客网 时间:2024/05/02 01:59
K - Kia's Calculation
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
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 ?
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 10 6, and without leading zeros.
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 10 6, and without leading zeros.
Output
For test case X, output "Case #X: " first, then output the maximum possible sum without leading zeros.
Sample Input
159583036
Sample Output
Case #1: 8984
AC CODE:
#include <iostream>#include <cstring>#define MAX 1000001using namespace std;char t1[MAX];char t2[MAX];void output(int a,int n){for(int i=0;i<n;i++)cout<<a;}int main(){int T;int count=0;cin>>T;while(T--){int s1[11]={0};int s2[11]={0};scanf("%s%s",t1,t2);int i,j;int len=0;for(i=0;t1[i];i++){len++;s1[ t1[i]-'0' ]++;}for(i=0;t2[i];i++){s2[ t2[i]-'0' ]++;}cout<<"Case #"<<++count<<": ";if(len==1){cout<<(t1[0]+t2[0]-'0'-'0')%10<<endl;continue;}int left=1;int right=8;int t=0;int count1,count2;for(i=1;i<10;i++){if(s1[i]==0)continue;for(j=1;j<10;j++){if(s2[j]==0)continue;if((i+j)%10>t){t=(i+j)%10;count1=i;count2=j;}}}if(t==0){cout<<0<<endl;continue;}cout<<t;s1[count1]--;s2[count2]--;int sum[11]={0};for(i=9;i>=0;i--) // 1234 5678{for(j=0;j<10;j++){if(s2[j]==0)continue;int t=i-j;if(t<0)t+=10;if(s1[t]==0)continue;if(t==2){//cout<<s2[7]<<endl;//cout<<j<<" "<<i<<endl;}if(s1[t]>s2[j]){sum[i] += s2[j];s1[t]-=s2[j];s2[j]=0;}else{sum[i] += s1[t];s2[j]-=s1[t];s1[t]=0;}}}for(i=9;i>=0;i--){output(i,sum[i]);}cout<<endl;}return 0;}
0 0
- 四川省ACM竞赛(2013)---K - Kia's Calculation
- Kia's Calculation(贪心)
- K - Kia's Calculation (贪心构造)
- HDU 4726 Kia's Calculation (贪心)
- Kia's Calculation
- HDU4726-Kia's Calculation
- hdu4726--(Kia's Calculation)
- hdu 4726 Kia's Calculation 数论(2013 ACM/ICPC Asia Regional Online —— Warmup2 1011)
- 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
- hdu4726 Kia's Calculation 贪心
- HDU 4726 Kia's Calculation
- HDU 4726 Kia's Calculation
- HDU 4726 Kia's Calculation
- Kia's Calculation UESTC - 836
- 判断账号密码是否规范
- asp.net Linq分页实现代码
- Linux编译avd goldfish内核,启动avd屏幕黑屏的解决方案
- 得了吧 亲 这都是借口
- 与学生管理系统的初次相遇
- 四川省ACM竞赛(2013)---K - Kia's Calculation
- 关于 Ubuntu 10.10系统(Linux) 安装 Qt Creator 的详细教程 Qt库的编译安装
- Android国际化的一点积累
- 【贪吃蛇,C++实现】,由简单游戏开始学习OOP,的编程新手
- 【contentprovider系列3】增加联系人信息
- iOS开发之:dispatch_async 与 dispatch_get_global_queue 的使用方法
- 解决问题方式的转变4
- ssh问题的解决
- atoi函数的使用