Kia's Calculation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 83    Accepted Submission(s): 16

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
 
Source
 
Recommend
zhuyuanchen520
 

想了很久,

最后其实就是贪心构造。

最高位特殊处理。

然后后面就是不断尽量构造和大的

 /* ***********************************************
Author :kuangbin
Created Time :2013-9-11 12:30:33
File Name :2013-9-11\1011.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; int a[];
int b[]; char A[],B[];
int num1[],num2[];
int ans[]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
int iCase = ;
scanf("%d",&T);
while(T--)
{
iCase++;
scanf("%s%s",A,B);
int n = strlen(A);
for(int i = ;i < n;i++)
{
num1[i] = A[i] - '';
num2[i] = B[i] - '';
}
if(n == )
{
printf("Case #%d: %d\n",iCase,(num1[]+num2[])%);
continue;
}
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(int i = ;i < n;i++)
{
a[num1[i]] ++;
b[num2[i]] ++;
}
int x = , y = ;
int ttt = -;
for(int i = ;i <= ;i++)
for(int j = ;j <= ;j++)
if(a[i] && b[j] && ((i+j)%) > ttt )
{
x = i;
y = j;
ttt = (x+y)%;
}
a[x]--;
b[y]--;
int cnt = ;
ans[cnt++] = (x+y)%; for(int p = ;p >= ;p--)
{
for(int i = ;i <= ;i++)
if(a[i])
{
if(i <= p)
{
int j = p-i;
int k = min(a[i],b[j]);
a[i] -= k;
b[j] -= k;
while(k--)
ans[cnt++] = p;
}
int j = + p - i;
if(j > )continue;
int k = min(a[i],b[j]);
a[i] -= k;
b[j] -= k;
while(k--)
ans[cnt++] = p;
}
}
printf("Case #%d: ",iCase);
int s = ;
while(s < cnt- && ans[s] == )s++;
for(int i = s;i < cnt;i++)
printf("%d",ans[i]);
printf("\n");
}
return ;
}

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) T ...

  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. K - Kia's Calculation (贪心)

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

  7. HDU-4726 Kia's Calculation 贪心

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

  8. hdu 4726(贪心)

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

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

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

随机推荐

  1. 【前端vue开发】vue开发watch检测的使用

    <span style="color:#006600;"><div id="app"> <input type="tex ...

  2. 如何用纯CSS布局两列,一列固定宽度,另一列自适应?

    大家都知道好多网站都是左右布局的,很多公司在笔试和面试环节也常常问这个问题.一个去网易的师兄说14年腾讯面试的时候问过这个问题,网易在笔试和面试时候也问过这个问题,还有很多互联网公司也都涉及到这个问题 ...

  3. github后端开发面试题大集合(三)

    作者:小海胆链接:https://www.nowcoder.com/discuss/3616来源:牛客网 13.软件架构相关问题: 什么情况下缓存是没用的,甚至是危险的? 为什么事件驱动的架构能提高可 ...

  4. Codeforces 580D Kefa and Dishes(状态压缩DP)

    题目链接:http://codeforces.com/problemset/problem/580/D 题目大意:有n盘菜每个菜都有一个满意度,k个规则,每个规则由x y c组成,表示如果再y之前吃x ...

  5. SQL Server中删除表中重复数据

    方法一:利用游标,但要注意主字段或标识列 declare @max integer,@id integer open cur_rows fetch cur_rows into @id,@max beg ...

  6. **IOS自动完成(搜索自动提示)功能实现

    UISearchBar搜索AutoComplete下拉列表搜索提示 http://www.codeios.com/thread-10685-1-1.html 介绍:     在搜索框上加入下拉列表.在 ...

  7. Hadoop案例(十)WordCount

    WordCount案例 需求1:统计一堆文件中单词出现的个数(WordCount案例) 0)需求:在一堆给定的文本文件中统计输出每一个单词出现的总次数 1)数据准备:Hello.txt hello w ...

  8. 【LOJ】 #2025. 「JLOI / SHOI2016」方

    题解 有什么LNOI啊,最后都是JLOI罢了 一道非常--懵逼的统计题 当然是容斥,所有的方案 - 至少有一个点坏掉的正方形 + 至少有两个点坏掉的正方形 - 至少有三个点坏掉的正方形 + 至少有四个 ...

  9. 【POJ】1067.取石子游戏

    题解 这道题让我对SG函数有了更深刻的理解,这是道打表找规律题 我们打出来SG函数似乎是 1 2必败 3 5必败 4 7必败 6 10必败 8 13必败 哇我找到规律了-- 然而,我显然不会通项 后来 ...

  10. hdoj1863 畅通工程(Prime || Kruskal)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1863 思路 最小生成树问题,使用Prime算法或者Kruskal算法解决.这题在hdoj1233的基础 ...