程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> codeforces 214B - Hometask

codeforces 214B - Hometask

編輯:C++入門知識

B. Hometask time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?

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

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 single line print the answer to the problem. If such number does not exist, then you should print -1.

Sample test(s) input
1
0
output
0
input
11
3 4 5 4 5 3 5 3 4 4 0
output
5554443330
input
8
3 2 5 1 5 2 2 3
output
-1

Note

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.

解析:因為要求是同時2和5的倍數,所以一定要以0結尾,所以沒有0就一定會輸出-1。然後就是要求是3的倍數,如果sum%3==0,則全部輸出,若不是,由於sum%3=1或2,不是刪掉一個數就是兩個數,刪除一個數的情況較簡單,若是需要刪除兩個數,就是(1+1=2)和(2+2)%3=1的情況了。注意當sum=0時,只輸出一個0。

代碼:

#include 
#include 
#include 
#include 
using namespace std;

int main()
{
    int n,d[10];
    while(scanf("%d",&n)!=EOF)
    {
        int i,j,sum=0,a;
        memset(d,0,sizeof(d));
        for(i=0; i=2)
                    for(j=1,i=2; i<=8; i+=3)
                    while(d[i]&&j<=2) d[i]--,sum-=i,j++;
                else flag=1;
            }
            if(sum%3==2)
            {
                if(d[2]+d[5]+d[8])
                {
                    for(i=2; i<=8; i+=3)
                    if(d[i])   {d[i]--,sum-=i;break;}
                }
               else if(d[1]+d[4]+d[7]>=2)
                    for(j=1,i=1; i<=7; i+=3)
                    while(d[i]&&j<=2) d[i]--,sum-=i,j++;
               else flag=1;
            }

            if(!flag)
            {
                if(d[1]+d[2]+d[3]+d[4]+d[5]+d[6]+d[7]+d[8]+d[9])
                {
                    for(i=9; i>=0; i--)
                        while(d[i]--) printf("%d",i);
                    printf("\n");
                }
                else printf("0\n");
            }
            else printf("0\n");
        }

    }

    return 0;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved