Kia's Calculation
来源:互联网 发布:java线程池实现 编辑:程序博客网 时间:2024/04/25 13:05
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
Now Kia has two integers
After she shuffles +
The first line has a number
For each test case there are two lines. First line has the number
Both
For test case Case #X:
first, then output the maximum possible sum without leading zeros.
1
5958
3036
Case #1: 8984
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
const int N=2000010;
int num1[N],num2[N];
int sum[N];
int a[20],b[20];
string s,s1;
int main()
{
int t;
cin>>t;
for(int i=1;i<=t;i++)
{
cin>>s>>s1;
int l=s.length();
for(int j=0;j<l;j++)
{
num1[j]=s[j]-'0';
num2[j]=s1[j]-'0';
}
if(l==1)
{
printf("Case #%d: %d\n",i,(num1[0]+num2[0])%10);
continue;
}
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int j=0;j<l;j++)
{
a[num1[j]]++;
b[num2[j]]++;
}
int x=0,y=0;
int tt=-1;
for(int j=1;j<=9;j++)//最高位
for(int k=1;k<=9;k++)
if(a[j]&&b[k]&&((j+k)%10)>tt)
{
x=j;y=k;tt=(x+y)%10;
}
a[x]--;b[y]--;
int cnt=0;
sum[cnt++]=(x+y)%10;
for(int j=9;j>=0;j--)//下
for(int k=0;k<=9;k++)//上
{
if(a[k])
{
if(k<=j)//先找大的从9开始
{
int flag=j-k;
int sign=min(a[k],b[flag]);//计数最少的选
a[k]-=sign;
b[flag]-=sign;
while(sign--)
sum[cnt++]=j;
}
int flag=10+j-k;
if(flag>9) continue;
//j<k时的结果
int sign=min(a[k],b[flag]);
a[k]-=sign;
b[flag]-=sign;
while(sign--)
sum[cnt++]=j;
}
}
printf("Case #%d: ",i);
int ss=0;
while(ss<cnt-1&&sum[ss]==0) ss++;//剔除最高位为0;例如000时
for(int j=ss;j<cnt;j++)
printf("%d",sum[j]);
cout<<endl;
}
return 0;
}
- Kia's Calculation
- HDU4726-Kia's Calculation
- hdu4726--(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(贪心)
- hdu4726 Kia's Calculation 贪心
- HDU 4726 Kia's Calculation
- HDU 4726 Kia's Calculation
- HDU 4726 Kia's Calculation
- Kia's Calculation UESTC - 836
- HDU 4726 Kia's Calculation
- HDU 4726 Kia's Calculation
- HDU 4726 Kia's Calculation 解题报告
- HDU 4726 Kia's Calculation (贪心)
- [后缀数组]poj3261 Milk Patterns
- Kia's Calculation UESTC - 836
- C语言:C语言的一些基本概念
- TextView使用Spannable设置复合文本
- JsRender 循环内部引用父级数据
- Kia's Calculation
- project_workflow_01
- ZOJ_3732 Graph Reconstruction HDU4797 Havel-Hakimi
- bootstrap导出table科学计数法
- linux和vi指令集
- svn提交提示乱码
- Android Webp 完全解析 缩小apk的大小!
- css基础知识
- 8.多进程图像的引出——CPU管理的直观想法