codeforce-305A--Strange Addition (贪心)

博客介绍了如何解决Codeforce-305A问题,其中涉及一个数学运算规则:只有当两个数在每一位上至少有一个为0时,它们才能相加。Vasya需要从给定的k个非负整数中选择一组,使得这组数中的任意两个数都可以根据规则相加。博主提出了一种贪心策略,以找到能够相加的数的最大集合,并给出了AC代码实现。

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

A. Strange Addition
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

Unfortunately, Vasya can only sum pairs of integers (a, b), such that for any decimal place at least one number has digit 0 in this place. For example, Vasya can sum numbers 505 and 50, but he cannot sum 1 and 4.

Vasya has a set of k distinct non-negative integers d1, d2, ..., dk.

Vasya wants to choose some integers from this set so that he could sum any two chosen numbers. What maximal number of integers can he choose in the required manner?

Input

The first input line contains integer k (1 ≤ k ≤ 100) — the number of integers.

The second line contains k distinct space-separated integers d1, d2, ..., dk (0 ≤ di ≤ 100).

Output

In the first line print a single integer n the maximum number of the chosen integers. In the second line print n distinct non-negative integers — the required integers.

If there are multiple solutions, print any of them. You can print the numbers in any order.

Examples
Input
4
100 10 1 0
Output
4
0 1 10 100 
Input
3
2 70 3
Output
2
2 70 

题意:给一个数组,求出符合题中运算规则的数的最大集合,集合中任意两个数都能发生运算,运算要求是两个数必须每个位上都有0才能运算;比如2和3不能,因为最低位没有0,10和3看成10和03,这样各位和十位都有0,可以运算,另外单个数符合运算,10和20不符合要求(输入的数<=100),若有多种解,输出其中任意一个;

思路:稍微分析会发现,集合中最多有4个数,因为不可能出现两个个位数,也不会出现两个十位数,最多的情况是出现0和100,这两个数是可以无条件加进集合,因为他们两和任何一个数都能运算,我采用的贪心策略是优先考虑下于10的数,如果存在,则可以加入加入集合(想想为什么),对位>=10的非整十数,如果存在整十数或则存在小于10的数则不加入他们,如果两者都不存在,有非整十数则加入一个,否则不加入,具体看代码(仔细想想):

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
int a[105],b[105];
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        sort(a+1,a+n+1);
        int cnt=0,flag=0,_flag=0;
        if(a[1]==0)b[++cnt]=0;//如果最小的为0,则加入集合
        for(int i=1;i<=n;i++)
        {
            if(a[i]!=0&&a[i]%10==0&&a[i]!=100)//判断是否有整十数
            {
                b[++cnt]=a[i];
                flag=1;
                break;
            }
        }
        for(int i=1;i<=n;i++)//小于10的数(不包括0)有则加入一个
        {

            if(a[i]>=1&&a[i]<=9)
            {
                b[++cnt]=a[i];
                _flag=1;
                break;
            }
        }
        if(!flag&&!_flag)//如果整十数和小于10的数都没有,则考虑加入非整十数
        {
            for(int i=1;i<=n;i++)
            {
                if(a[i]>=10&&a[i]<=99&&a[i]%10!=0)
                {
                    b[++cnt]=a[i];
                    break;
                }
            }
        }

        if(a[n]==100)//如果最大的为100,则加入集合
            b[++cnt]=100;
        sort(b+1,b+cnt+1);
        printf("%d\n",cnt);
        for(int i=1;i<=cnt;i++)
            printf("%d%c",b[i],i==cnt?'\n':' ');
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值