Kia's Calculation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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 106, 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

算法:贪心

题解:根据题目意思来,我们只要将所有的数字出现的个数都记录一下(桶排),然后你就遍历依次相加取最大就行了。要注意的你需要单独判断第一个数,它不能有0,因为你的变化是不能把0变成第一位的,然后你还要注意的是,你的结果不能有前导0。

#include <iostream>
#include <cstdio>
#include <memory.h> using namespace std; int visa[], visb[];
char ans[];
string a, b; int main() {
int T;
int cas = ;
scanf("%d", &T);
while(T--) {
for(int i = ; i < ; i++) {
visa[i] = visb[i] = ;
}
cin >> a >> b;
int lena = a.size();
int lenb = b.size();
for(int i = ; i < lena; i++) {
visa[a[i] - '']++;
}
for(int i = ; i < lenb; i++) {
visb[b[i] - '']++;
}
int posa, posb, maxx = -;
for(int i = ; i < ; i++) { //找出第一个数
for(int j = ; j < ; j++) {
if(i != && j != && visa[i] && visb[j] && maxx < (i + j) % ) {
maxx = (i + j) % ;
posa = i;
posb = j;
}
}
}
int len = ;
printf("Case #%d: ", ++cas);
if(maxx >= ) { //如果第一个数存在,则存储下来
ans[len++] = maxx + '';
visa[posa]--;
visb[posb]--;
}
for(int k = ; k >= ; k--) { //寻找之后的数字,每次取最大
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
while(visa[i] > && visb[j] > && (i + j) % == k) {
visa[i]--;
visb[j]--;
ans[len++] = k + '';
}
}
}
}
int mark = ;
for(int i = ; i < len; i++) { //需要判断前导0
if(mark && i == len - ) {
printf("%c", ans[i]);
} else if(mark && ans[i] != '') {
mark = ;
printf("%c", ans[i]);
} else if(!mark) {
printf("%c", ans[i]);
}
}
printf("\n");
}
return ;
}

K - Kia's Calculation(贪心)的更多相关文章

  1. K - 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/userstatus.php?user=zhsl 题意:给两个大数,他们之间的加法法则每位相加不进位.现在可以对两个大数的每位重新排序,但是首位不 ...

  3. 贪心 HDOJ 4726 Kia's Calculation

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

  4. HDU 4726 Kia's Calculation (贪心算法)

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

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

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

  6. Kia's Calculation hdu4726

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

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

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

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

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

  9. Kia's Calculation(HDU 4267)

    Problem Description Doctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is ...

随机推荐

  1. bzoj2152 聪聪可可 (树形dp)

    大意: 给定树, 随机选两点, 求两点距离是3的倍数的概率. 树形dp入门水题, 枚举每个点作为lca时的答案即可. #include <iostream> #include <qu ...

  2. oracle基本查询01

    /*数据库-----> 数据库实例----->表空间[逻辑单位]------>数据文件[物理单位] 通常情况下oracle数据库只会有一个实例ORCL 新建一个项目: MYSQL:创 ...

  3. Angular6如何引入jQuery-knob

    Angular6如何引入jQuery-knob 1.概述 Angular6引入jQuery变得异常简单,请参考https://blog.csdn.net/qq_35321405/article/det ...

  4. 使用Enablebuffering多次读取Asp Net Core 3.0 请求体 读取Request.Body流

    原文:使用Enablebuffering多次读取Asp Net Core 请求体 使用Enablebuffering多次读取Asp Net Core 请求体 1 .Net Core 2.X时代 使用E ...

  5. winfrom 保存图片

    private void btnSave_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); ...

  6. Idea+Maven部署打包JavaFX项目遇到的坑

    用Idea写了一个JavaFX项目,创建artifacts,build artifacts,运行build出来的exe可执行文件时总是遇到 class not found的错误,如下图 一开始根据提示 ...

  7. JDK + Tomcat 安装 + 制作自定义镜像【第 2 篇 Tomcat】

    [第 1 篇 JDK]:https://www.cnblogs.com/del88/p/11842387.html[第 2 篇 Tomcat]:https://www.cnblogs.com/del8 ...

  8. 图片样式加hover特效

    之前有个尴尬的事情发生,我不知道如何将文字放在图片右边,我想了个麻烦且愚蠢的办法,后来才知道只需要将图片居左就可以达到效果.....不说了看下面 需要实现的效果: 很简单, <img src=& ...

  9. asp.net 简单的身份验证

    1 通常我们希望已经通过身份验证的才能够登录到网站的后台管理界面,对于asp.net 介绍一种简单的身份验证方式 首先在webconfig文件中添加如下的代码 <!--身份验证--> &l ...

  10. Shell中比较判断

    一.shell判断数组中是否包含某个元素:ary=(1 2 3)a=2if [[ "${ary[@]}" =~ "$a" ]] ; then    echo & ...