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 题意:给两个大数,他们之间的加法法则每位相加不进位.现在可以对两个大数的每位重新排序,但是首位不 ...
随机推荐
- python类继承
面向对象的编程带来的主要好处之一是代码的重用,实现这种重用的方法之一是通过 继承 机制.继承完全可以理解成类之间的 类型和子类型 关系. 假设你想要写一个程序来记录学校之中的教师和学生情况.他们有一些 ...
- uva-11995 - I Can Guess the Data Structure!(栈,优先队列,队列,水题)
11995 - I Can Guess the Data Structure! There is a bag-like data structure, supporting two operation ...
- VS插件开发——格式化变量定义语句块
插件介绍 代码地址:https://github.com/sun2043430/vs2008_format_variable_define_plugin/ 在vs里,对选中的变量定义块进行格式化,效果 ...
- levelDB跳表实现
跳表的原理就是利用随机性建立索引,加速搜索,并且简化代码实现难度.具体的跳表原理不再赘述,主要是看了levelDB有一些实现细节的东西,凸显自己写的实现不足之处. 去除冗余的key template& ...
- c++,虚函数
1.在声明函数时,在最前加上virtual,则该函数就是函虚数,基类的虚函数被派生类继承后仍是虚函数.2.派生类中可以重写基类的虚函数.3.用指针访问重写的虚函数时,被访问的虚函数是指针指向的对象所属 ...
- javascript 学习随笔
var carname="Volvo"; var carname; 重新声明变量后变量的值不变,在这里还是volvo var carname="Volvo"; ...
- js window.onload事件
1.最简单的调用方式 直接写到html的body标签里面,如: ? 1 2 3 4 <html> <body onload="func()"& ...
- linux(Centos 6.3)学习笔记
一.系统分区 1,磁盘分区 使用分区编辑器(partition editor)在磁盘上划分几个逻辑部分.碟片一旦划分成 数个分区,不同类的目录与文件可以存储进不同 ...
- 解决Spring中singleton的Bean依赖于prototype的Bean的问题
在spring bean的配置的时候,可能会出现一个singleton的bean依赖一个prototype的bean.因为singleton的bean只有一次初始化的机会,所以他们的依赖关系页只有在初 ...
- AOP 笔记
http://blog.csdn.net/Intlgj/article/details/5671248 这篇文章里面介绍的非常好,值得阅读. 这里盗用里面的两张图片 [在没有AOP之前是这样的] [使 ...