贪心 HDOJ 4726 Kia'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>
 10 #include <algorithm>
 11 #include <cstring>
 12 #include <string>
 13 #include <cmath>
 14 using namespace std;
 15
 16 const int MAXN = 1e6 + 10;
 17 const int INF = 0x3f3f3f3f;
 18 char s1[MAXN], s2[MAXN];
 19
 20 int main(void)        //HDOJ 4726    Kia‘s Calculation
 21 {
 22     //freopen ("K.in", "r", stdin);
 23
 24     int t, cas = 0;
 25     int cnt1[11], cnt2[11], cnt3[11];
 26
 27     scanf ("%d", &t);
 28     while (t--)
 29     {
 30         scanf ("%s", &s1);
 31         scanf ("%s", &s2);
 32
 33         printf ("Case #%d: ", ++cas);
 34
 35         int len = strlen (s1);
 36         if (strcmp (s1, "0") == 0)
 37         {
 38             printf ("%s\n", s2);    continue;
 39         }
 40         else if (strcmp (s2, "0") == 0)
 41         {
 42             printf ("%s\n", s1);    continue;
 43         }
 44
 45         memset (cnt1, 0, sizeof (cnt1));
 46         memset (cnt2, 0, sizeof (cnt2));
 47         memset (cnt3, 0, sizeof (cnt3));
 48
 49         for (int i=0; i<len; ++i)
 50         {
 51             cnt1[s1[i]-‘0‘]++;    cnt2[s2[i]-‘0‘]++;
 52         }
 53
 54         int ii = 1, jj = 1, mx = -1;
 55         for (int i=1; i<=9; ++i)
 56         {
 57             if (cnt1[i] == 0)    continue;
 58             for (int j=1; j<=9; ++j)
 59             {
 60                 if (cnt2[j] == 0)    continue;
 61                 int tmp = (i + j) % 10;
 62                 if (tmp > mx)
 63                 {
 64                     mx = tmp;    ii = i;    jj = j;
 65                 }
 66             }
 67         }
 68         cnt1[ii]--;    cnt2[jj]--;
 69         if (!mx)
 70         {
 71             puts ("0");        continue;
 72         }
 73
 74         for (int i=9; i>=0; --i)
 75         {
 76             for (int j=0; j<=9; ++j)
 77             {
 78                 for (int k=0; k<=9; ++k)
 79                 {
 80                     if ((j+k)%10==i && cnt1[j] && cnt2[k])
 81                     {
 82                         int tmp = min (cnt1[j], cnt2[k]);
 83                         cnt1[j] -= tmp;    cnt2[k] -= tmp;
 84                         cnt3[i] += tmp;
 85                     }
 86                 }
 87             }
 88         }
 89
 90         printf ("%d", mx);
 91         for (int i=9; i>=0; --i)
 92         {
 93             for (int j=0; j<cnt3[i]; ++j)    printf ("%d", i);
 94         }
 95         puts ("");
 96     }
 97
 98
 99     return 0;
100 }

贪心 HDOJ 4726 Kia's Calculation

时间: 2024-12-15 06:53:37

贪心 HDOJ 4726 Kia's Calculation的相关文章

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

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

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) {

Kia&#39;s Calculation(贪心)

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

K - Kia&#39;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

HDOJ 4965 Fast Matrix Calculation

(AB)^n=A*(BA)^(n-1)^B Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 576    Accepted Submission(s): 297 Problem Description One day, Alice and Bob felt bored again, B

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

枚举+贪心 HDOJ 4932 Miaomiao&#39;s Geometry

题目传送门 1 /* 2 题意:有n个点,用相同的线段去覆盖,当点在线段的端点才行,还有线段之间不相交 3 枚举+贪心:有坑点是两个点在同时一条线段的两个端点上,枚举两点之间的距离或者距离一半,尽量往左边放,否则往右边放, 4 判断一下,取最大值.这题二分的内容少 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include <cmath> 10 using n

线段树+树状数组+贪心 HDOJ 5338 ZZX and Permutations

题目传送门 1 /* 2 题意:不懂... 3 线段树+树状数组+贪心:贪心从第一位开始枚举,一个数可以是循环节的末尾或者在循环节中,循环节(循环节内部是后面的换到前面,最前面的换到最后面).线段树维护最大值,树状数组维护区间是否是循环节,查找前面最左边不是循环节的可用二分.我还是云里雾里的,看懂了网上的解题报告但还是不是完全明白题意:( 4 详细解释:http://blog.csdn.net/qq_24451605/article/details/47173933 5 */ 6 /******