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 ...
随机推荐
- Python 装饰&生成&迭代器
Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...
- Android 官方下拉刷新 SwipeRefreshLayout
0.build.gradle compile 'com.android.support:support-v4:23+' 1.布局文件 <android.support.v4.widget.Swi ...
- 批量Insert
oracle INSERT ALL ,) ,) ,) FROM DUAL
- Git FLS的使用
克隆git地址后,一些文件内容被隐藏. 显示如下: version https://git-lfs.github.com/spec/v1oid sha256:xxxxxxxxxxxxxxxxxxxxx ...
- python selenium4 模拟点击+拖动+保存验证码 测试对象+以验证码的返回ID保存命名 58同城验证码
#!/usr/bin/python # -*- coding: UTF-8 -*- # @Time : 2019/12/5 17:30 # @Author : shenghao/10347899@qq ...
- 前端 vue/react 或者 js 导入/导出 xlsx/xls (带样式)表格的功能
第一种导出表格的功能: yarn add xlsx script-loader file-saver xlsx-style 效果展示 xlsx-style的bug修复:node_module/xlsx ...
- fragment概念理解
fragment概念理解知识,fragment概念理解图片 fragment概念理解内容,fragment概念理介绍,fragment概念理正文 Fragment是Android honeycomb ...
- DX使用随记--GroupControl
1. 创建按钮: (1)添加引用:Imports DevExpress.XtraEditors.ButtonsPanelControl (2)添加按钮语句:GroupControl1.CustomHe ...
- SIFT算法相关资料
SIFT算法相关资料 一.SIFT算法的教程.源码及应用软件1.ubc:DAVID LOWE---SIFT算法的创始人,两篇巨经典经典的文章http://www.cs.ubc.ca/~lowe/ 2. ...
- RobHess的SIFT代码解析步骤三
平台:win10 x64 +VS 2015专业版 +opencv-2.4.11 + gtk_-bundle_2.24.10_win32 主要参考:1.代码:RobHess的SIFT源码 2.书:王永明 ...