Merlininice’s Hometask
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.
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. ***
***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
Source
developing schools contest 5
比赛的一道简单题,可是对于我们不搞数论的来讲还是一道不简单的题,题意很简单,给你一串数据,求他们能组成的最大能同时被2,3,5整除的数,能被2,3,5整除那就是说能被他们的公倍数30整除,那么最后一位肯定是0,小的时候学过一种简单的算法,要求一个数能否被3整除,只要把它的各个位数加起来,能被3整除就行了!那么只要判断所有的位数加起来能否被3整除,那么就分3种情况。
1:被3:除余0 这种情况是最好的,能被3整除;
2:余1:,那么这个时候就只要从1,4,7中减掉一个最小的就行;
3:余2,这种就稍显麻烦,要从2,5,8中减一个最小的,没有的时候,还得从1,3,7中减两个最小的(原因是两个余一就是余二),最后从大到小输出就行了
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
int map[11];
void out()
{
int mark=false;
for(int i=9;i>=1;i--)
{
while(map[i]--)
{
printf("%d",i);
mark=true;
}
}
if(mark)//这个地方要注意,当只有0的时候,只要输出一个0
{
while(map[0]--)
{
printf("0");
}
}
else
{
printf("0");
}
printf("\n");
}
int main()
{
int t=0;
int n;
int x;
while(scanf("%d",&n)!=EOF)
{
int sum=0;
bool sign=false;
bool flag=false;
memset(map,0,sizeof(map));
for(int i=0;i<n;i++)
{
scanf("%d",&x);
if(x==0)
flag=true;
sum+=x;
map[x]++;
}
printf("Case #%d:\n",++t);
printf("%d : ",n);
if(flag==false)
{
printf("-1\n");
}
else
{
if(sum%3==0)
{
out();
}
else if(sum%3==1)
{
bool sign=false;
for(int i=1;i<=9;i+=3)
{
if(map[i]>0)
{
map[i]--;
sign=true;
break;
}
}
if(sign)
{
out();
}
else
printf("-1\n");
}
else
{
bool sign=false;
for(int i=2;i<=9;i+=3)
{
if(map[i]>0)
{
map[i]--;
sign=true;
break;
}
}
if(!sign)
{
for(int i=1;i<=9;i+=3)
{
if(map[i]>=2)
{
map[i]-=2;
sign=true;
break;
}
}
}
if(sign)
out();
else
printf("-1\n");
}
}
}
return 0;
}