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

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 10
6, 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
 
Source
 
 
题意: 有2个合法的整数。 长度为 10^6。 数字的每一位都能移动, 但移动后的整数一定要是合法的, 即无前导零。 使得 A + B 最大
 

思路:贪心算法

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 (贪心算法)的更多相关文章

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

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

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

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

  3. ACM学习历程—HDU 4726 Kia's Calculation( 贪心&&计数排序)

    DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so carel ...

  4. hdu 4726 Kia's Calculation

    思路:刚开始想复杂了. 看解题报告后才知道这题挺简单的,看来还是要多训练啊!!! 单独处理首位的数字,不能为0.其他的就好处理了,从大到小依次找下去就可以了…… 代码如下: #include<i ...

  5. 贪心 HDOJ 4726 Kia's Calculation

    题目传送门 /* 这题交给队友做,做了一个多小时,全排列,RE数组越界,赛后发现读题读错了,囧! 贪心:先确定最高位的数字,然后用贪心的方法,越高位数字越大 注意:1. Both A and B wi ...

  6. hdu 1789 Doing HomeWork Again (贪心算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 /*Doing Homework again Time Limit: 1000/1000 MS ...

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

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

  8. HDU 1009 FatMouse' Trade (贪心算法)

    题意:就是老鼠要用猫粮换粮食,第i个房间一些东西,要用东西去换,可以不全换.问给定的猫粮最多能换多少粮食. 析:贪心算法.我们先算出来每个房间物品的平均价格是多少,肯定越低越好,并且如果能全换就全换, ...

  9. HDU-4726 Kia's Calculation 贪心

    题目链接:http://acm.hdu.edu.cn/userstatus.php?user=zhsl 题意:给两个大数,他们之间的加法法则每位相加不进位.现在可以对两个大数的每位重新排序,但是首位不 ...

随机推荐

  1. Delphi 中 COM 实现研究手记(一)

    前言 前些日子用 Delphi 写了一个 Windows 外壳扩展程序,大家知道 Windows 外壳扩展实际上就是 COM 的一种应用 -- Shell COM,虽然整个程序写得还算比较顺利,但写完 ...

  2. 在web page中使鼠标右击失效的几种方法

    这里主要介绍两种方法,一种是使用js来处理,还有一种是在html属性中设置. 方法一:js 1: <script language="javascript"> docu ...

  3. uboot: 理解uboot要看哪些书

    概览:

  4. 虚拟rethat联网问题

    近日在vmware虚拟了一台rethat的linux,但是使用桥连也上不了网! 宿主机是win7,由于在公司,是采用固定ip上网的,通过上网查资料,终于可以连接网络了: 步骤如下: 第一:修改vi / ...

  5. BZOJ 2101: [Usaco2010 Dec]Treasure Chest 藏宝箱( dp )

    dp( l , r ) = sum( l , r ) - min( dp( l + 1 , r ) , dp( l , r - 1 ) ) 被卡空间....我们可以发现 l > r 是无意义的 ...

  6. ContentProvider的一些总结

    ContentProvider中的URI, The URI that identifies the provider   一个特定的uri对应着唯一一个内容提供者, 谷歌官方文档里的说明, Query ...

  7. 【转】CentOS 6.5 生产环境优化指南

    原文链接:https://www.deepwebcn.com/82.html centos6.5 CentOS 6.5 系统安装之后并不能立即投入生产环境使用,常常需要先经过我们运维人员的优化才行.优 ...

  8. CSS长度单位及区别 em ex px pt in

    1.         css相对长度单位 Ø         em          元素的字体高度 Ø         ex           字体x的高度 Ø         px        ...

  9. Linux c c++ 开发调试技巧

    看到一篇介绍 linux c/c++ 开发调试技巧的文章,感觉挺使用,哪来和大家分享. 通向 UNIX 天堂的 10 个阶梯Author: Arpan Sen, 高级技术人员, Systems Doc ...

  10. 如何A掉未来程序改

    话说有这样一道神题:[集训队互测2015]未来程序·改. 大意是要求写一个简单的C++解释器!这里去掉了C++的许多特性,连简单的break和continue都没有了! 话说NOI被屠了之后,一时心血 ...