Problem Description
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
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int a[],b[];
char A[],B[];
int ans[]; int main()
{
int T,iCase = ;
scanf("%d",&T);
getchar();
while(T--)
{
iCase++;
scanf("%s%s",A,B);
int n = strlen(A);
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(int i = ; i < n; i++)
{
a[A[i] - '']++;
b[B[i] - '']++;
}
int x = , y = ;
int ttt = -;
//求出首位数字
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
if(a[i] && b[j] && ((i+j)%) > ttt )
{
x = i;
y = j;
ttt = (x+y)%;
}
a[x]--;
b[y]--;
int cnt = ;
ans[cnt++] = (x+y)%;
//求出其它位结果保存到ans数组中
for(int p = ; p >= ; p--)
{
for(int i = ; i <= ; i++)
if(a[i])
{
if(i <= p)
{
int j = p-i;
int k = min(a[i],b[j]);
a[i] -= k;
b[j] -= k;
while(k--) ans[cnt++] = p;
}
int j = + p - i;
if(j > )continue;
int k = min(a[i],b[j]);
a[i] -= k;
b[j] -= k;
while(k--) ans[cnt++] = p;
}
}
printf("Case #%d: ",iCase);
int s = ;
while(s < cnt- && ans[s] == )s++;
for(int i = s; i < cnt; i++) printf("%d",ans[i]);
printf("\n");
}
return ;
}

贪心

Kia's Calculation(HDU 4267)的更多相关文章

  1. HDU 4726 Kia's Calculation (贪心算法)

    Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...

  2. HDU 4726 Kia's Calculation(贪心构造)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4726 题意:给出两个n位的数字,均无前缀0.重新排列两个数字中的各个数,重新排列后也无前缀0.得到的两 ...

  3. 2道acm编程题(2014):1.编写一个浏览器输入输出(hdu acm1088);2.encoding(hdu1020)

    //1088(参考博客:http://blog.csdn.net/libin56842/article/details/8950688)//1.编写一个浏览器输入输出(hdu acm1088)://思 ...

  4. HDU 4726 Kia's Calculation(贪心)

    Kia's Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. K - Kia's Calculation(贪心)

    Kia's Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. 2012年长春网络赛(hdu命题)

    为迎接9月14号hdu命题的长春网络赛 ACM弱校的弱菜,苦逼的在机房(感谢有你)呻吟几声: 1.对于本次网络赛,本校一共6名正式队员,训练靠的是完全的自主学习意识 2.对于网络赛的群殴模式,想竞争现 ...

  7. Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5064 题目意思:给出n个数:a1, a2, ..., an,然后需要从中找出一个最长的序列 b1, b ...

  8. 2013 多校联合 F Magic Ball Game (hdu 4605)

    http://acm.hdu.edu.cn/showproblem.php?pid=4605 Magic Ball Game Time Limit: 10000/5000 MS (Java/Other ...

  9. (多线程dp)Matrix (hdu 2686)

    http://acm.hdu.edu.cn/showproblem.php?pid=2686     Problem Description Yifenfei very like play a num ...

随机推荐

  1. 「Windows MFC 」「Edit Control」 控件

    「Windows MFC 」「Edit Control」 控件

  2. c++ fstream中seekg()和seekp()的用法

    转自:http://blog.sina.com.cn/s/blog_679f85d40100mysi.html 先说一下C语言中fseek()的功能: 函数原型:int fseek(FILE *fp, ...

  3. vim plugin 原理

    vim 个性化设置与功能扩展均通过 script 来实现,这种 script 又叫 plugin.plugin 是 vim 的核心与精髓. 最常用的配置文件 vimrc,也是一种 plugin.换句话 ...

  4. 今天收到报警邮件,提示网站502 bad gateway,

    今天收到报警邮件,提示网站502 bad gateway, 输入网站url后果然无法打开: 登录服务器查看nginx进程正常: 查看fastcGI进程已经停止运行了: 问题找到后就该查找是什么原因产生 ...

  5. jQuery 快速入门教程

    内容目录 jQuery 入门 什么是jQuery 如何使用jQuery jQuery的运行原理 如何选择jQuery版本 ready() 准备就绪时执行代码 jQuery 核心:选取元素 使用jQue ...

  6. Python3 高级特性

    切片 L[0:3]表示,从索引0开始取,直到索引3为止,但不包括索引3.即索引0,1,2,正好是3个元素. 如果第一个索引是0,还可以省略: >>> L =['Michael', ' ...

  7. SQLite批量插入,修改数据库 zt

    SQLiteConnection sqConnection = dataProvider.GetDbConnection(); sqConnection.Open(); SQLiteCommand s ...

  8. Swift开发之 ---- Swift宏定义

    swift中没有了#Define这种宏定义了,可以用let来声明常量来取代,判断当前系统版本 let IS_IOS7 = (UIDevice.currentDevice().systemVersion ...

  9. Xcode6 Xcode7 Xcode 官方链接 --备用

    Xcode 6 官方下载链接: http://adcdownload.apple.com//wwdc_2014/xcode_6_beta_ie8g3n/xcode_6_beta.dmg   Xcode ...

  10. [原博客] POI系列(4)

    正规.严谨.精妙. -POI BZOJ 1531 : [POI2005]Bank notes 裸的背包,可以二进制拆分一下.一个物品比如说有n个,可以拆成 1,2,4,8,16...个. OJ上没有样 ...