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 ...
随机推荐
- memcached基本操作指令
item执行命令: 第一行:Key Flags ExpirationTime BytesKey:Key 用于查找缓存值Flags:一个32位的标志值,客户机使用它存储关于键值对的额外信息Expirat ...
- glang flag
package main import ( "flag" "fmt" "github.com/golang/glog" ) /* 解析 fl ...
- C# 使用Emit实现动态AOP框架 (二)
目 录 C# 使用Emit实现动态AOP框架 (一) C# 使用Emit实现动态AOP框架 (二) C# 使用Emit实现动态AOP框架 (三) C# 使用Emit实现动态AOP框架 进阶篇之异常处 ...
- java怎么解除文件占用(Dom4j操作完xml后怎么关流)
一.背景 项目中要解析xml,由于Dom4j的诸多优点,我就用Dom4j解析xml,代码如下: public void readXML() { SAXReader reader = new SAXRe ...
- ubuntu目录结构(转)
/:根目录,一般根目录下只存放目录,不要存放文件,/etc./bin./dev./lib./sbin应该和根目录放置在一个分区中 /bin:/usr/bin:可执行二进制文件的目录,如常用的命令ls. ...
- SIP 3pcc
3PCC全称Third Party Call Control,中文即第三方电话呼叫控制,指的是由第三方控制者在另外两者之间建立一个会话,由控制者负责会话双方的媒体协商.3PCC是一种非常灵活的会话控制 ...
- C#中DateTime.Ticks
DateTime.Ticks:表示0001 年 1 月 1 日午夜 12:00:00 以来所经历的 100 纳秒数,即Ticks的属性为100纳秒(1Ticks = 0.0001毫秒). Unix时间 ...
- arcgis js 之 渔网工具(调用地图服务)
arcgis js 之 渔网工具(调用地图服务) 原理: 简历不同级别的网渔网图层,设置显示比例尺.然后发布服务,使用MapImageLayer接收. 过程: 1.在arcmap中用创建渔网工具将不同 ...
- Hexo NexT主题内加入动态背景
主题内新添加内容 _layout.swig 找到themes\next\layout\_layout.swig文件,添加内容:在<body>里添加: 1 2 3 <div class ...
- Vue介绍:vue导读1
一.什么是vue 二.如何在页面中使用vue 三.vue的挂载点 四.vue的基础指令 一.什么是vue 1.什么是vue vue.js十一个渐进式javascript框架 渐进式:vue从控制页面中 ...