CodeForces 214B

本文介绍了一种算法,该算法旨在从一组数字中构造出最大的整数,该整数能够同时被2、3和5整除。文章详细解释了算法的工作原理,包括如何选择数字以确保最终结果符合除法条件,特别是关于如何通过删除某些数字来解决模3余数的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

yxc loves math lessons very much, so he doesn't attend them, unlike wwl. But now yxc wants to get a good mark for math. For that Ms. lxh, his ACM teacher, gave him a new task. yxc 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.

Example
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 (You Will Win)

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.

题意 差不多 是 最多用 n个 数字组成一个最大的数,这个数可以被2 5 3 整除。以为着末尾数字必须为0,并且 各位之和为3的倍数。

题目卡在了  可以删除一个数,也可以删除两个数上。

如果 全部的和 取模3 余1 那么 删除一个取膜余1 的 或者 两个取模余2 的都可以解决问题。

如果 取模3 余2  那么 可以删除一个取模余2 或者 2个取模余1 的。

如果无法满足上述,倘若有0 输出0 没0的话输出-1

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main ()
{
    int n,i,j;
    int  a[15];
    memset(a,0,sizeof(a));
    cin >>n;
    int sum=0;
    for(i=0; i<n; i++)
    {
        cin >>j;
        a[j]++;
        sum+=j;
    }
    if(a[0]==0)
    {
        printf("-1\n");
    }
    else
    {
       int f=0;
       if(sum%3==0) f=1;
       else if(sum%3==1)
       {
         for(i=1;i<=9;i=i+3)
         {
            if(a[i]>=1)
             {
                a[i]--;
                 f=1;
                break;
             }
         }
         if(f==0)
         {
            for(i=2;i<=9;i=i+3)
            {
                if(a[i]>=1)
                {
                    a[i]--;
                    if(a[i]>=1)
                    {
                        a[i]--;
                        f=1;
                        break;
                    }
                    else if(a[i+3]>=1)
                    {
                        a[i+3]--;
                         f=1;
                        break;
                    }
                    else if(a[i+6]>=1)
                    {
                        a[i+6]--;
                         f=1;
                        break;
                    }
                    else
                        f=0;
                }
            }
         }
       }
        else if(sum%3==2)
       {
         for(i=2;i<=9;i=i+3)
         {
            if(a[i]>=1)
             {
                a[i]--;
                 f=1;
                break;
             }
         }
         if(f==0)
         {
            for(i=1;i<=9;i=i+3)
            {
                if(a[i]>=1)
                {
                    a[i]--;
                    if(a[i]>=1)
                    {
                        a[i]--;
                        f=1;
                        break;
                    }
                    else if(a[i+3]>=1)
                    {
                        a[i+3]--;
                         f=1;
                        break;
                    }
                    else if(a[i+6]>=1)
                    {
                        a[i+6]--;
                         f=1;
                        break;
                    }
                    else
                        f=0;
                }
            }
         }
       }
        if(f)
        {
            int z=1;
            int y=0;
            for(i=9; i>=0; i--)
            {
                while (a[i]--)
                {
                    if(i!=0)
                    {
                        printf("%d",i);
                        y=1;
                        z=0;
                    }
                    else
                    {
                        if(y==1)
                            printf("%d",i);
                    }

                }
            }
            if(z)
                printf("0\n");
        }

    }
    return 0;


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值