B. Merlininice’s Hometask

/*题意:求给定数字中任意个数字组成能同时被2、3、5整除的最大数。
 *解释:其实只要这个数能被30(2、3、5的最小公倍数)整除就行。
 *     能被30整除就一定能被10整除,所以所有数字里面至少有一个0.
 *     然后组成这个数的其它位上的数字和应该是3的倍数。
 *     题目说有10万个数字,但这里只需要一个容量为10的一维数组记录0-9出现的次数即可。
 *处理:首先将所有数字和sum求出来。
 *     然后看sum的值,sum的结果只有三种:
 *     1、sum%3==0,不用说直接按照9-0的顺序输出。
 *     2、sum%3==1,减掉1、4、7中存在的最小的,当然如果没有的话,那就一定不行了。
 *     3、sum%3==2,减掉2、5、8中存在的最小的,如果不存在这些数字,减掉1、4、7中的最小的两个组合
 *     4、最后判断。

 *说明:其实下面的代码在处理sum%3==2的时候不完整,我们只考虑了1、4、7中相同两个数的组合,理论上应该是这三个中的价值最小的两个组合,考虑顺序应该是11、14、44、

 *17、47、77共6种情况。
/

B. Merlininice’s Hometask

Time Limit : 5000/2000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 46   Accepted Submission(s) : 22

Font: Times New Roman | Verdana | Georgia

Font Size:

Problem Description

Merlininice dosen’t love math lessons, so he always ditches math class. But as the final exam is coming, now Merlininice is really regret his actions and wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Merlininice want to solve the task immediately. Can you help this poor guy?
You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.
Each digit is allowed to occur in the number the same number of times it occurs in the set.

Input

Input contains multiple test cases. For each test case: a single line contains a single integer n (1≤n≤100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space.

Output

On a first line print “Case #k:”, k is the k-th case. And on the second line, first print the number n, following “:”, and the the answer to the problem. If such number does not exist, then you should print -1.

***HINT: In the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. ***

Sample Input

1
0
11
3 4 5 4 5 3 5 3 4 4 0
8
3 2 5 1 5 2 2 3

Sample Output

Case #1:
1 : 0
Case #2:
11 : 5554443330
Case #3:
8 : -1

Author

Yuna

Source

developing schools contest 5


 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdio>
 5 using namespace std;
 6 int main()
 7 {
 8     int i,j,n,x,Case=0,b[10];
 9     while(cin>>n)
10     {
11         int flag=0,sum=0;
12         for(i=0;i<10;i++)
13         {
14             b[i]=0;
15         }
16         for(i=0;i<n;i++)
17         {
18             scanf("%d",&x);
19             if(x==0) flag=1;
20             sum+=x;
21             ++b[x];
22         }
23         printf("Case #%d:\n%d : ",++Case,n);
24         if(flag==0)
25         {
26             printf("-1\n");
27             continue;
28         }
29         int ind=sum%3;
30         if(ind==1)
31         {
32             for(i=1;i<=7;i+=3)
33             {
34                 if(b[i]&&(sum-i)%3==0)
35                 {
36                     sum-=i;
37                     b[i]--;
38                     break;
39                 }
40             }
41         }
42         else if(ind==2)
43         {
44             for(i=2;i<=8;i+=3)
45             {
46                 if(b[i]&&(sum-i)%3==0)
47                 {
48                     sum-=i;
49                     b[i]--;
50                     break;
51                 }
52             }
53             if(sum%3)
54             {
55                 for(i=1;i<=7;i+=3)
56                 {
57                     if(b[i]>=2&&(sum-2*i)%3==0)
58                     {
59                         sum-=2*i;
60                         b[i]-=2;
61                         break;
62                     }
63                 }
64             }
65         }
66         if(sum%3)
67         {
68             printf("-1\n");
69             continue;
70         }
71         int xn=0;
72         for(j=9;j>0;j--)
73         {
74             while(b[j]--)
75             {
76                 xn=1;
77                 printf("%d",j);
78             }
79         }
80         if(xn)
81         {
82             while(b[0]--)
83             {
84                 printf("0");
85             }
86         }
87         else
88         {
89             printf("0");
90         }
91         printf("\n");
92     }
93     return 0;
94 }

 

posted @ 2012-08-13 09:29  zyh123101  阅读(144)  评论(0编辑  收藏  举报