K - Kia's Calculation(贪心)
Kia's Calculation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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 ?
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.
5958
3036
算法:贪心
题解:根据题目意思来,我们只要将所有的数字出现的个数都记录一下(桶排),然后你就遍历依次相加取最大就行了。要注意的你需要单独判断第一个数,它不能有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(贪心)的更多相关文章
- K - Kia's Calculation (贪心)
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU-4726 Kia's Calculation 贪心
题目链接:http://acm.hdu.edu.cn/userstatus.php?user=zhsl 题意:给两个大数,他们之间的加法法则每位相加不进位.现在可以对两个大数的每位重新排序,但是首位不 ...
- 贪心 HDOJ 4726 Kia's Calculation
题目传送门 /* 这题交给队友做,做了一个多小时,全排列,RE数组越界,赛后发现读题读错了,囧! 贪心:先确定最高位的数字,然后用贪心的方法,越高位数字越大 注意:1. Both A and B wi ...
- HDU 4726 Kia's Calculation (贪心算法)
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 4726 Kia's Calculation(贪心)
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Kia's Calculation hdu4726
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- ACM学习历程—HDU 4726 Kia's Calculation( 贪心&&计数排序)
DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so carel ...
- HDU 4726 Kia's Calculation(贪心构造)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4726 题意:给出两个n位的数字,均无前缀0.重新排列两个数字中的各个数,重新排列后也无前缀0.得到的两 ...
- Kia's Calculation(HDU 4267)
Problem Description Doctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is ...
随机推荐
- docker启动mysql 自定义配置文件
命令行如下: docker run --name mysql56 -p : -v /home/mysql56/data:/var/lib/mysql -v /home/mysql56/conf:/et ...
- 关于EF数据迁移的个人总结 简单有效
有用的拿走,没用的嘴下留情!
- liunx pip安装
方法一 wget https://bootstrap.pypa.io/get-pip.py python get-pip.py 方法二 wget https://pypi.python.org/pac ...
- 多线程--volatile
在解释volatile关键字之前,先说说java的指令重排以及代码的执行顺序. 指令重排: public void sum(){ int x = 1; int y = 2; int x = x + 1 ...
- 微信小程序开发(八)获取手机ip地址
// succ.wxml <view>手机IP:{{motto.query}}</view> // succ.js var app = getApp() Page({ data ...
- 抽象类能使用 final 修饰吗?(未完成)
抽象类能使用 final 修饰吗?(未完成)
- 9种Java单例模式详解(推荐)
单例模式的特点 一个类只允许产生一个实例化对象. 单例类构造方法私有化,不允许外部创建对象. 单例类向外提供静态方法,调用方法返回内部创建的实例化对象. 懒汉式(线程不安全) 其主要表现在单例类在外 ...
- SCU 4442 party 二分图最大点权独立集
每个青蛙喝黑茶或者红茶或者都可以喝 M个矛盾关系 有矛盾的不能喝同种茶 但你可以花费Wi使得这个青蛙消除所有矛盾 把矛盾当作边 青蛙当作点 如果这两个青蛙只喝不同的一种茶就不建边 题目中保证了不存在奇 ...
- tsung压力测试环境部署详细步骤(内附安装包)
操作系统: Redhat 6.3.Redhat6.5 .centos7.4(这些版本已验证过) tsung版本: tsung-1.6.0 下载地址: 链接: https://pan.baidu.com ...
- paperpass论文检测平台
推荐大家一个靠谱的论文检测平台.重复的部分有详细出处以及具体修改意见,能直接在文章上做修改,全部改完一键下载就搞定了.怕麻烦的话,还能用它自带的降重功能.哦对了,他们现在正在做毕业季活动, 赠送很多免 ...