题目链接:http://acm.hdu.edu.cn/userstatus.php?user=zhsl

  题意:给两个大数,他们之间的加法法则每位相加不进位。现在可以对两个大数的每位重新排序,但是首位不能为0,求两个数相加最大的和。

  从最高位开始,考虑和最大贪心,每次从两个大数中找两位a和b,max=(a+b)%10的和最大。如果max有多种组成情况,对于a+b<10的所有情况,我们任意选择一组就可以了。对于存在a'+b'>10的组合情况,如果有a+b<10的组合情况,我们就选择a+b<10的组合情况,因为a'和b'都是大于max的,留下来会更优,否则随便选择一组a'+b'>10的组合情况。。

 //STATUS:C++_AC_453MS_2260KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e60;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End char sa[N],sb[N];
int a[N],b[N],cnta[],cntb[],ans[];
int T,n; int main(){
// freopen("in.txt","r",stdin);
int i,j,ca=,t,k,ok;
scanf("%d",&T);
while(T--)
{
scanf("%s%s",sa,sb);
n=strlen(sa);
mem(cnta,);mem(cntb,);
for(i=;i<n;i++){
cnta[sa[i]-'']++;
cntb[sb[i]-'']++;
}
mem(ans,);
int hig=,x,y;
for(i=;i<=;i++){
for(j=;j<=;j++){
if(cnta[i]&&cntb[j]&&(i+j)%>hig){
hig=(i+j)%;
x=i;y=j;
}
}
}
printf("Case #%d: ",ca++);
if(hig==){
printf("0\n");
continue;
}
cnta[x]--,cntb[y]--;
for(t=;t>=;){
ok=;
for(i=;i<=;i++){
if(cnta[i]&&cntb[(t-i+)%]){
ok=;
cnta[i]--;cntb[(t-i+)%]--;
ans[t]++;
break;
}
}
if(!ok)t--;
} printf("%d",hig);
for(i=;i>=;i--){
while(ans[i]--)printf("%d",i);
}
putchar('\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(贪心)

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

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

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

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

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

  5. hdu 4726 Kia's Calculation

    思路:刚开始想复杂了. 看解题报告后才知道这题挺简单的,看来还是要多训练啊!!! 单独处理首位的数字,不能为0.其他的就好处理了,从大到小依次找下去就可以了…… 代码如下: #include<i ...

  6. 贪心 HDOJ 4726 Kia's Calculation

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

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

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

  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. android actionbar标题栏

    在android的actionBar中,actionBar的视图是固定的,左边是程序的图标和title,右边是添加的menuItem,如果想要定制actionbar中的view就要自定义视图. 首先要 ...

  2. 12个目标跟踪方面的资料12 Tracking

    Goal Tracking Template - FEMA.gov Goal Tracking Template Set a weekly or biweekly deadline to report ...

  3. Linux 线程属性函数总结

    1.初始化一个线程对象的属性 int pthread_attr_init(pthread_attr_t *attr); 返回值:若是成功返回0,否则返回错误的编号 形 参: attr 指向一个线程属性 ...

  4. HDU 4686 Arc of Dream(矩阵)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意: 思路: #include <iostream>#include <cs ...

  5. poj 3101 Astronomy (java 分数的最小公倍数 gcd)

    题目链接 要用大数,看了别人的博客,用java写的. 题意:求n个运动周期不完全相同的天体在一条直线上的周期. 分析:两个星球周期为a,b.则相差半周的长度为a*b/(2*abs(a-b)),对于n个 ...

  6. java 语言里 遍历 collection 的方式

    我来简单说一下吧,一般有2种方法来遍历collection中的元素,以HashSet为例子HashSet hs=new HashSet();hs.add("hello");hs.a ...

  7. uva1639 Candy

    组合数,对数. 这道题要用到20w的组合数,如果直接相乘的话,会丢失很多精度,所以用去对数的方式实现. 注意指数,因为取完一次后,还要再取一次才能发现取完,所以是(n+1)次方. double 会爆掉 ...

  8. ASP.NET MVC 传值方法ViewData与ViewBag的区别

    一.介绍 在Asp.net MVC 3 web应用程序中,我们会用到ViewData与ViewBag,对比一下: ViewData ViewBag 它是Key/Value字典集合 它是dynamic类 ...

  9. Java Web编程的主要组件技术——Hibernate入门

    参考书籍:<J2EE开源编程精要15讲> Hibernate是对象/关系映射(ORM,Object/Relational Mapping)的解决方案,就是将Java对象与对象关系映射到关系 ...

  10. 2014-LAMP兄弟连视频下载地址汇总

    linux 兄弟连2014年新版Linux视频教程百度网盘下载 http://pan.baidu.com/s/1kTsjVfx http://pan.baidu.com/s/1sjJf2OX 兄弟连2 ...