Merlininice’s Hometask 2,3,5的最大公倍数

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

转载于:https://www.cnblogs.com/hebozi/archive/2012/08/12/2635347.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值