集训-求能被2,3,5整除的数

B. Merlininice’s HometaskFont: 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

思路:能被2,5整除的数末尾数字是0,能被3整除的数是各个位相加之和能被3整除。找能被3整除的数方法:

1:   s%3;  2:  (s-a[i])%3; 3: (s-a[i]-a[i+1])%3;

#include <stdio.h>
#include <algorithm>
using namespace std;
int num[1000000];
int main( )
{
  int n;
  int test;
  int s;    test=0;
  while(~scanf("%d",&n))
  {
    int flag=0;
    s=0;

    for( int i=0;i<n;i++)
    {
      scanf("%d",&num[i]);
      if(num[i]==0) flag=1;//保证至少有一个零
      s+=num[i];
    }
   
    sort(num,num+n);
    printf("Case #%d:\n",++test);
    if(!flag) {printf("%d : -1\n",n);continue;}
    if(s==0) {printf("%d : 0\n",n);continue;}//注意此步,若不进行处理,输入6个0将输出6个0,而不是 1个0
    flag=0;
    printf("%d : ",n);

  //找能被3整除的数
    if(s%3==0)
    {
      
      for( int i=n-1;i>=0;i--)
      {
        printf("%d",num[i]);
      }
    }
    else
    {
      int f;
       for(int  i=1;i<n;i++)//i=0测试数据对但WA
       {
         if((s-num[i])%3==0)
         {
           flag=1;
           f=i;
           break;   
         }
       }
       if(flag)
       {
         for( int i=n-1;i>=0;i--)
         if(i!=f)
          printf("%d",num[i]);
       }
       else
       {
          for( int i=1;i<n;i++)//i=0测试数据对但WA
           if((s-num[i]-num[i+1])%3==0)
           {
              f=i;
              flag=1;
               break;
           }
           if(flag)
          { 
   for( int i=n-1;i>=0;i--)
            {
     if(i!=f&&i!=f+1)
              printf("%d",num[i]);
      }
    }
            else printf("-1");
         
       }
    }
    puts("");
  }
  return 0;
}

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

你可能感兴趣的:(集训-求能被2,3,5整除的数)