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. JAVA 解析TXT文本

    package file; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; im ...

  2. Executor

    一.为什么需要Executor?为了更好的控制多线程,JDK提供了一套线程框架Executor,帮助开发人员有效的进行线程控制.他们都在java.util.concurrent包中,是JDK并发包的核 ...

  3. Spring MVC之JSON数据交互和RESTful的支持

    1.JSON概述 1.1 什么是JSON JSON(JavaScript Object Notation,JS对象标记)是一种轻量级的数据交换格式.它是基于JavaScript的一个子集,使用了C.C ...

  4. Entity Framework Code First 在Object Join Linq查询时出现全表查询的语句。

    最近一个项目,使用微软的Entity Framework的ORM框架的项目,部署到现场后,出现了系统缓慢,多个客户端的内存溢出崩溃的问题. 打开了SQL Server Profiler(SQL Ser ...

  5. Ansible之tags介绍

    本节内容: tags介绍 一.tags介绍 我们每次改完配置文件,比如上一篇博客中的的apache.yml,没必要把整个playbook都运行一遍,只需要运行改变了的task.我们可以给task一个标 ...

  6. 插头DP学习笔记——从入门到……????

    我们今天来学习插头DP??? BZOJ 2595:[Wc2008]游览计划 Input 第一行有两个整数,N和 M,描述方块的数目. 接下来 N行, 每行有 M 个非负整数, 如果该整数为 0, 则该 ...

  7. LoadRunner中Action的迭代次数的设置和运行场景中设置

    LoadRunner中Action的迭代次数的设置和运行场景中设置 LoadRunner是怎么重复迭代和怎么增加并发运行的呢? 另外,在参数化时,对于一次压力测试中均只能用一次的资源应该怎么参数化呢? ...

  8. LoadRunner FAQ2

    LR中参数的定义和赋值 1.参数的赋值和取值 lr_save_string("hello world","param"); lr_eval_string(&qu ...

  9. 001 Anaconda的介绍与安装

    1.官网 www.continuum.io 2.ananconda的版本 同一个版本下对应一个python3与python2,在这里下载使用python 2.7的版本. 3.概述 Anaconda是一 ...

  10. 43:n个骰子的点数

    算法分析: 算法1.基于递归求色子点数,时间效率不高 现在我们考虑如何统计每一个点数出现的次数.要向求出n个骰子的点数和,可以先把n个骰子分为两堆:第一堆只有一个,另一个有n-1个.单独的那一个有可能 ...