Merlininice’s Hometask

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

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

代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
#define N 100005
int a[N];

bool cmp(int x,int y)
{
   return x>y;  
}

int main()
{
   int n;
   int cnt=1;
   while(scanf("%d",&n)!=-1)
   {
     int sum=0;
  for(int i=0;i<n;i++)
  {
    scanf("%d",&a[i]);  
    sum+=a[i]; 
     }    
  printf("Case #%d:\n",cnt++);
  printf("%d : ",n);
  sort(a,a+n,cmp);
  if(sum==0)
  {
       printf("0\n");
       continue;
     }   
     if( a[n-1]!=0 )
     {
     printf("-1\n");
  continue;      
     }
     int flag=0;
     if(sum%3==0)
     {
      flag=1;
     for(int i=0;i<n;i++)
  printf("%d",a[i]);  
  printf("\n");
     }
    if(!flag)
    {
      for(int i=n-2;i>=0;i--)
   {
        if( (sum-a[i])%3==0 ) //删一个
     {
         flag=1;
      for(int j=0;j<n;j++)
      {
        if(j!=i)
     printf("%d",a[j]);    
      }    
      printf("\n");
      break; 
     }   
         }
  } 
   if(!flag)
   {
       for(int i=n-2;i>=0;i--)
    {
        if( (sum-a[i]-a[i-1])%3==0 ) //删两个;
     {
         flag=1;
      for(int j=0;j<n;j++)
      {
         if(j!=i&&j!=i-1)
      printf("%d",a[j]);   
            }    
      printf("\n");
      break;     
     }   
          }    
   }
   if(!flag)
   printf("-1\n");
   }
   return 0; 
}

链接:http://acm.hdu.edu.cn/diy/contest_showproblem.php?cid=16638&pid=1002