HDU 4726 Kia's Calculation (贪心算法)
Kia's Calculation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 513 Accepted Submission(s): 142
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 ?
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.
5958
3036
import java.io.*;
import java.util.*; public class Main {
BufferedReader bu;
PrintWriter pw;
int n;
int[] a = new int[12];
int[] b = new int[12]; public static void main(String[] args) throws IOException {
new Main().work();
} void work() throws IOException {
bu = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new OutputStreamWriter(System.out), true);
n = Integer.parseInt(bu.readLine());
for (int p = 1; p <= n; p++) { String s1 = bu.readLine();
String s2 = bu.readLine(); Arrays.fill(a, 0);
Arrays.fill(b, 0); for (int i = 0; i < s1.length(); i++) {
a[s1.charAt(i) - '0']++;
} for (int i = 0; i < s2.length(); i++) {
b[s2.charAt(i) - '0']++;
}
//获取第一个最大的数字
int t = getFirst();
pw.print("Case #"+p+": ");
pw.print(t);
if (t == 0) {//如果第一个数字为0,则后面的数字,都为0
pw.println();
continue;
}
// 获取后面的数字
for (int i = 9; i >= 0; i--) {
int ans = 0;
for (int j = 0; j <= 9; j++) {
if ((i - j >= 0) && a[j] != 0 && b[i - j] != 0) {
int m = Math.min(a[j], b[i - j]);
ans += m;
a[j] -= m;
b[i - j] -= m;
}
if ((10 + i - j <= 9) && a[j] != 0 && b[10 + i - j] != 0) {
int m = Math.min(a[j], b[10 + i - j]);
ans += m;
a[j] -= m;
b[10 + i - j] -= m;
}
}
for (int j = 1; j <= ans; j++) {
pw.print(i);
}
}
pw.println();
}
}
//获取第一个数字
int getFirst() {
int i, j;
for (i = 9; i >= 1; i--) { for (j = 1; j <= 9; j++) {
if ((i - j > 0) && a[j] != 0 && b[i - j] != 0) {
a[j]--;
b[i - j]--;
break;
}
if ((10 + i - j <= 9) && a[j] != 0 && b[10 + i - j] != 0) {
a[j]--;
b[10 + i - j]--;
break;
}
}
if (j <= 9)
break; }
return i;
}
}
HDU 4726 Kia's Calculation (贪心算法)的更多相关文章
- HDU 4726 Kia's Calculation(贪心)
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 4726 Kia's Calculation(贪心构造)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4726 题意:给出两个n位的数字,均无前缀0.重新排列两个数字中的各个数,重新排列后也无前缀0.得到的两 ...
- ACM学习历程—HDU 4726 Kia's Calculation( 贪心&&计数排序)
DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so carel ...
- hdu 4726 Kia's Calculation
思路:刚开始想复杂了. 看解题报告后才知道这题挺简单的,看来还是要多训练啊!!! 单独处理首位的数字,不能为0.其他的就好处理了,从大到小依次找下去就可以了…… 代码如下: #include<i ...
- 贪心 HDOJ 4726 Kia's Calculation
题目传送门 /* 这题交给队友做,做了一个多小时,全排列,RE数组越界,赛后发现读题读错了,囧! 贪心:先确定最高位的数字,然后用贪心的方法,越高位数字越大 注意:1. Both A and B wi ...
- hdu 1789 Doing HomeWork Again (贪心算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 /*Doing Homework again Time Limit: 1000/1000 MS ...
- K - Kia's Calculation (贪心)
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 1009 FatMouse' Trade (贪心算法)
题意:就是老鼠要用猫粮换粮食,第i个房间一些东西,要用东西去换,可以不全换.问给定的猫粮最多能换多少粮食. 析:贪心算法.我们先算出来每个房间物品的平均价格是多少,肯定越低越好,并且如果能全换就全换, ...
- HDU-4726 Kia's Calculation 贪心
题目链接:http://acm.hdu.edu.cn/userstatus.php?user=zhsl 题意:给两个大数,他们之间的加法法则每位相加不进位.现在可以对两个大数的每位重新排序,但是首位不 ...
随机推荐
- QT学习 之 QwtPlot(数学绘图)
QT对于统计图像.函数图像等的绘制是没有相关组件的帮助的,只有利用手工绘制图片. QwtPlot是用来绘制二维图像的widget,继承自QFrame 和 QwtPlotDict.不过严格的说来,它只是 ...
- 地精排序(Gnome Sort) 算法
gnome应该是最简单排序的排序算法吧!Gnome Sort,这是该算法的作者命名的,O(n*n)时间复杂度,O(1)空间复杂度,属于稳定的排序算法.算法的思想是每趟循环找到第一个逆序的元素,把它和在 ...
- cocos2dx进阶学习之CCDirector
继承关系 CCDirecotor -> CCObject, TypeInfo 处理主窗口消息,管理何时.何种方式执行场景. 经常被翻译成导演,负责管理整个游戏的进程推动和周边支持. 成员 inl ...
- 禁用 Windows Azure 网站中的 ARR 实例关联
编辑人员注释: 本博客文章由 Windows Azure 网站团队的项目经理 Erez Benari 撰写. 在 Windows Azure 网站中设置网站的多个实例是横向扩展网站的绝佳方式,Azur ...
- IT第六天 - eclipse快捷操作、万年历项目的编写、菱形的打印输出、代码简化
IT第六天 上午 小项目 1.程序提前结束的退出标志 2.登录用户的模拟,给出适当的提示信息 3.根据要求,寻找规律,然后编写程序 Eclipse的使用 1.快捷键的使用 下午 中小项目 1.九九乘法 ...
- wiki oi 1044 拦截导弹
题目描述 Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某 ...
- IIS express 7.5 配置和多网站执行
iis express7.5 支持xp 以上的操作系统,能够解决xp.iis的问题. 首先先下载安装iisexpress7.5地址是id=1038">点击打开链接下载完毕点击安装就可以 ...
- iOS开发之自定义导航栏返回按钮右滑返回手势失效的解决
我相信针对每一个iOS开发者来说~除了根视图控制器外~所有的界面通过导航栏push过去的界面都是可以通过右滑来返回上一个界面~其实~在很多应用和APP中~用户已经习惯了这个功能~然而~作为开发者的我们 ...
- Oracle中的单引号问题
SELECT '<a href="javascript:void(0)" onclick="openWyl('''||a.aac001 FROM ac01 a; S ...
- CentOS服务器下对mysql的优化
原文链接: CentOS服务器下对mysql的优化 一.mysql的优化思路 mysql的优化分为两方面: 1. 服务器使用前的优化 2. 服务使用中的优化 二.mysql的基础优化步骤 1. 硬件级 ...