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 题意:给两个大数,他们之间的加法法则每位相加不进位.现在可以对两个大数的每位重新排序,但是首位不 ...
随机推荐
- 转: 模块化开发框架seajs简介
JavaScript模块化开发库之SeaJSSeaJS由国内的牛人lifesinger开发.目前版本是1.1.1,源码不到1500行,压缩后才4k,质量极高.这篇会讲述SeaJS的一些基本用法,不会面 ...
- QRadionButton 圆点样式
QRadioButton::indicator { width: 13px; height: 13px;} QRadioButton::indicator::unchecked { ...
- python 安装ssh和Scrapy
在Python中没有专用的SSH模块,这需要手动的安装模块才行.Python中使用SSH需要用到OpenSSH,而OpenSSH依赖于paramiko模块,paramiko模块又依赖于pycrypto ...
- 浙江大学PAT上机题解析之2-11. 两个有序链表序列的合并
已知两个非降序链表序列S1与S2,设计函数构造出S1与S2的并集新非降序链表S3. 输入格式说明: 输入分2行,分别在每行给出由若干个正整数构成的非降序序列,用-1表示序列的结尾(-1不属于这个序列) ...
- 《Python学习手册》
读书笔记请见Github博客:http://wuxichen.github.io/Myblog/reading/2014/10/04/LearningPython.html
- 【Cavali风格/优质羊毛混纺面料/高密抗静电里衬/撞色拼皮/立领/绿色/便装单西】玛萨玛索男装网购商城
[Cavali风格/优质羊毛混纺面料/高密抗静电里衬/撞色拼皮/立领/绿色/便装单西]玛萨玛索男装网购商城 Cavali风格/优质羊毛混纺面料/高密抗静电里衬/撞色拼皮/立领/绿色/便装单西
- sqlserver 分页查询总结
sqlserver2008不支持关键字limit ,所以它的分页sql查询语句将不能用mysql的方式进行,幸好sqlserver2008提供了top,rownumber等关键字,这样就能通过这几个关 ...
- ios 调用webservice整理
资料地址:http://blog.sina.com.cn/s/blog_a30ee5f701016yn3.html 学iphone开发有一段时间了,对于我这个新手来说,学习过程中,遇到不少问题,尤其是 ...
- UVA 531 - Compromise(dp + LCS打印路径)
Compromise In a few months the European Currency Union will become a reality. However, to join th ...
- 基于visual Studio2013解决C语言竞赛题之0411公约数和公倍数
题目 解决代码及点评 求最大公约数和最小公倍数,方法已经在题目中有提示,分析代码实现如下: /* 题目: 输入两个正整数 m和 n,求其最大公约数和最小公倍数. */ #includ ...