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. 查找最小的k个元素

    题目:输入n个整数,输出其中最小的k个. 例如输入1,2,3,4,5,6,7和8这8个数字,则最小的4个数字为1,2,3和4. 两种思路,无非就是时间与空间的妥协. 限制空间的时候要对原数组进行排序, ...

  2. VC生成lib的_stdcall函数名与mingw生成的不一致

    Qt Creator在Windows系统中,怎样链接VC生成的动态链接库 这个问题曾经困扰了我一整天.我想的是按照VC中的方法,增加include文件,增加lib文件,然后编译即可.谁知链接时总是出现 ...

  3. Windbg调试命令详解(2)

    转载注明>>  [作者:张佩][原文:http://blog.csdn.net/blog_index] 2. 符号与源码 符号与源码是调试过程中的重要因素,它们使得枯燥生硬的调试内容更容易 ...

  4. BCB6.0是垃圾的二十条理由

    我用的BCB6.0和windows2000 都是公司配的正版,我也有多年的开发经验. 1. IDE常常出现非法操作,有时重起动还会出错,须要重装BCB. 2. 自己主动完毕和智能提示功能超慢,慢到能够 ...

  5. 微软阵营稳定的好消息:.NET开源、Visual Studio 自由

    今天各个IT社区,头版头条说的是微软.NET开源了.宇宙中最好的IED–Visual Studio Community 2013将免费提供给用户的消息. <宇宙中最强大的开发环境免费了! > ...

  6. BZOJ-1007-水平可见直线-HN2008

    描写叙述 在xoy直角坐标平面上有n条直线L1,L2,-Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则Li为被覆盖的. 比如,对于直线: L1:y=x; L2:y=- ...

  7. vs2013内置IISExpress相关问题

    问题描述,以前做的程序迁移到vs2013后出现500.22问题. HTTP 错误 500.22 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP.NET 设 ...

  8. BZOJ 1977: [BeiJing2010组队]次小生成树 Tree( MST + 树链剖分 + RMQ )

    做一次MST, 枚举不在最小生成树上的每一条边(u,v), 然后加上这条边, 删掉(u,v)上的最大边(或严格次大边), 更新答案. 树链剖分然后ST维护最大值和严格次大值..倍增也是可以的... - ...

  9. 浙江工商大学15年校赛I题 Inversion 【归并排序求逆序对】

    Inversion Time Limit 1s Memory Limit 131072KB Judge Program Standard Ratio(Solve/Submit) 15.00%(3/20 ...

  10. 二、Mongo命令初识

    简单介绍mongo的一些基本命令 1.   连接与登陆mongo 在命令行输入“mongo”命令即可登陆Mongo数据库(PS:默认讨论被信任的环境,也就是不需要用户名和密码进行登陆). 查看当前所使 ...