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. MySQL 聚合函数与count()函数

    一.MySQL中的聚合函数 MySQL 5.7文档的章节:12.20.1 Aggregate (GROUP BY) Function “聚合/组合”函数(group (aggregate) funct ...

  2. 怎样创建一个canvas画布环境

    1. 由于canvas画布在网页中, 所以需要在html中添加canvas标签: <!DOCTYPE html> <html lang="en"> < ...

  3. PLSQL PL/SQL Developer Oracle 使用技巧 常用设置 卡顿问题 病毒防范( 附带:配置文件)

    相关工具版本: PL/SQL Developer: 9.0.4.1644 Oracle : Oracle Database 10g Enterprise Edition Release 10.2.0. ...

  4. Linux学习(一)-安装vm虚拟机以及如何在虚拟机上安装Centos系统

    (一)基本说明 学习Linux需要一个环境,我们需要创建一个虚拟机,然后在虚拟机上安装一个Centos系统来学习. 1)安装软件vm12; 2)通过vm12创建一个虚拟机空间; 3)在vm12创建好的 ...

  5. 【jekins】jenkins构建触发

    一.定时构建的语法 * * * * *(五颗星,中间用空格隔开)第一颗*表示分钟,取值0~59第二颗*表示小时,取值0~23第三颗*表示一个月的第几天,取值1~31第四颗*表示第几月,取值1~12第五 ...

  6. printf固定一行打印倒计时的实现

    @2019-07-15 [小记] #include<stdlib.h> #include <stdio.h> #include <time.h> #include ...

  7. sql 拼接字符串单条拆分多条

    SELECT * FROM ( SELECT A.WS_ID , B.NEXT_OPERATOR FROM ( SELECT WS_ID , [NEXT_OPERATOR] = CONVERT(XML ...

  8. Repeater POJ - 3768 (分形)

    Repeater POJ - 3768 Harmony is indispensible in our daily life and no one can live without it----may ...

  9. 【Java基础 项目实例--Bank项目5】Account 和 customer 对象等 继承、多态、方法的重写

    延续 Java基础 项目实例--Bank项目4 实验要求 实验题目 5: 在银行项目中创建 Account 的两个子类:SavingAccount 和 CheckingAccount 实验目的: 继承 ...

  10. [转]makefile学习

    原文: http://blog.fatedier.com/2014/09/08/learn-to-write-makefile-01/ -------------------------------- ...