K - Kia's Calculation(贪心)

Kia‘s Calculation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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

算法:贪心

题解:根据题目意思来,我们只要将所有的数字出现的个数都记录一下(桶排),然后你就遍历依次相加取最大就行了。要注意的你需要单独判断第一个数,它不能有0,因为你的变化是不能把0变成第一位的,然后你还要注意的是,你的结果不能有前导0。

#include <iostream>
#include <cstdio>
#include <memory.h>

using namespace std;

int visa[15], visb[15];
char ans[1000007];
string a, b;

int main() {
    int T;
    int cas = 0;
    scanf("%d", &T);
    while(T--) {
        for(int i = 0; i < 10; i++) {
            visa[i] = visb[i] = 0;
        }
        cin >> a >> b;
        int lena = a.size();
        int lenb = b.size();
        for(int i = 0; i < lena; i++) {
            visa[a[i] - ‘0‘]++;
        }
        for(int i = 0; i < lenb; i++) {
            visb[b[i] - ‘0‘]++;
        }
        int posa, posb, maxx = -1;
        for(int i = 0; i < 10; i++) {    //找出第一个数
            for(int j = 0; j < 10; j++) {
                if(i != 0 && j != 0 && visa[i] && visb[j] && maxx < (i + j) % 10) {
                    maxx = (i + j) % 10;
                    posa = i;
                    posb = j;
                }
            }
        }
        int len = 0;
        printf("Case #%d: ", ++cas);
        if(maxx >= 0) {    //如果第一个数存在,则存储下来
            ans[len++] = maxx + ‘0‘;
            visa[posa]--;
            visb[posb]--;
        }
        for(int k = 9; k >= 0; k--) {        //寻找之后的数字,每次取最大
            for(int i = 0; i < 10; i++) {
                for(int j = 0; j < 10; j++) {
                    while(visa[i] > 0 && visb[j] > 0 && (i + j) % 10 == k) {
                        visa[i]--;
                        visb[j]--;
                        ans[len++] = k + ‘0‘;
                    }
                }
            }
        }
        int mark = 1;
        for(int i = 0; i < len; i++) {    //需要判断前导0
            if(mark && i == len - 1) {
                printf("%c", ans[i]);
            } else if(mark && ans[i] != ‘0‘) {
                mark = 0;
                printf("%c", ans[i]);
            } else if(!mark) {
                printf("%c", ans[i]);
            }
        }
        printf("\n");
    }
    return 0;
}

K - Kia's Calculation(贪心)

原文地址:https://www.cnblogs.com/buhuiflydepig/p/11392832.html

时间: 2024-10-10 22:18:58

K - Kia's Calculation(贪心)的相关文章

hdu 4726 Kia&#39;s Calculation(贪心)

题目链接:hdu 4726 Kia's Calculation 题目大意:给出两个数,然后两个数进行没有进位的加法,加数的各个位的数可以重新调整位置,但是不能有前导0的情况,要求加完之后的结果最大. 解题思路:从9开始配,直到0,但是因为9可能可以用0和9相加获得,所以一开始输出一个数,后面就可以统一操作. 0 9 9 55 55 0 #include <cstdio> #include <cstring> #include <algorithm> using name

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor 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 12

贪心 HDOJ 4726 Kia&#39;s Calculation

题目传送门 1 /* 2 这题交给队友做,做了一个多小时,全排列,RE数组越界,赛后发现读题读错了,囧! 3 贪心:先确定最高位的数字,然后用贪心的方法,越高位数字越大 4 5 注意:1. Both A and B will have same number of digits 两个数字位数相同 6 2. which is no larger than 10 6 不是大小,而是长度不超过1e6 7 */ 8 #include <cstdio> 9 #include <iostream&g

HDU 4726 Kia&#39;s Calculation (贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4726 思路:贪心,尽量先组大的数字,注意考虑前导零的情况 代码: #include <stdio.h> #include <string.h> const int N = 1000005; int t, v1[10], v2[10], ans[N]; char s1[N], s2[N]; void solve() { int n = strlen(s1); if (n == 1) {

HUST信心大涨迎省赛之《我要冲银牌》K——字符串——Kia&#39;s Calculation

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 1

Kia&#39;s Calculation(贪心)

http://acm.hdu.edu.cn/showproblem.php?pid=4726 大致题意:给两个长度小于10^6且相等的合法的正整数,你可以任意组合每个数中的数字,但不能有前导零.两个数相加的规则如题,相加不进位.问可以得到的A+B的最大值. 都看错题意了,一直以为数的大小是小于10^6,队友用了一个ms很高端的函数对字符串全排列,枚举求最大值.结果WA到死.其实是长度小于10^6,以后看题要细心再细心啊... 用数组记录每个数中每个数字的个数,每次都找从两个字符串中找和最大的,但

zoj 3715 K - Kindergarten Election

一个班有n个小朋友,要选一个班长,(n>=3),每个人不能投自己,给出每个人想要选的人,1号小朋友相当班长,如果一个小朋友不选自己 那么,自己可以给他bi个糖果让他选自己,那么请输出,最少花费多少个糖果 ,1号小朋友可以当上班长 开始的做法是贪心小的,那么问题很复杂,直接枚举小的是不符合条件的 后来想,枚举一号小朋友最后的票为k,那么其他人的票肯定小于等于k-1,如果有小朋友的选票高于k-1,那么先把投这个人的所有小朋友贿赂到小于k-1,按照需要的糖果数量 如果这个时候的选票大于k了,那么显然这

hdu 4550 贪心 思维题 不错

http://acm.hdu.edu.cn/showproblem.php?pid=4550 想了挺久,然后各种分类 终于AC,如果是现场,对自己没信心的话,估计还是要WA,,,,,,然后搜题解,发现人家都认为是简单题,看来我还是太弱了,牡丹江没有做出来K看来还是自己贪心和思维有问题 d是一个Deque 最朴素的算法是,如果当前的数<=d.front(),那么插入队列的前面,否则插入队列后面,但是有零所以需要单独处理,还是自己多举例找规律 我的策略: 1.记录0的个数zero,最小非零的数的个数

nyoj 1057寻找最大数(三) 贪心

#include <stdio.h> #include <string.h> int main(){ char temp,str[20]; int k,len,mark,flag,j; while(scanf("%s %d",str,&k)!=EOF){ len=strlen(str); if(k==0) { puts(str); continue; } for(int i=0;i<len;i++){ temp=str[i],flag=1; for