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

来源:互联网 发布:辐射4 画面优化设置 编辑:程序博客网 时间:2024/05/16 10:49
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 digit0 in this place. For example, Vasya can sum numbers505 and 50, but he cannot sum1 and 4.

Vasya has a set of k distinct non-negative integersd1, 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 integersd1, 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 printn 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
4100 10 1 0
Output
40 1 10 100 
Input
32 70 3
Output
22 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;}


原创粉丝点击