CF 305A——Strange Addition——————【暴力加技巧】
Unfortunately, Vasya can only sum pairs of integers (a, b), such that for any decimal place at least one number has digit 0in 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?
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).
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.
4
100 10 1 0
4
0 1 10 100
3
2 70 3
2
2 70
题目大意:刚开始没读懂题,还以为是找含有0的数字,选出的这些数字最多有一个不含0。然后错了,又重新读了题目,发现题意理解错了。题意是说从这些数中挑出可以任意相加的数,这里的相加必须是对应位最少有一个0。如果是10、1那么结果就有2个满足,如果是12、1就只有一个满足,结果最少要输出一个数字。
解题思路:因为数据范围比较小,所以用a,b,c,d,e分别代表0、只有1位且不为0(如9)、有两位且能被10整除(如20)、有两位且不能被10整除(25)、100。分析发现,对于25这种数,如果有9,则不能有25;如果有10则不能有25。这里只要特判就好了。其他的直接判断有就直接加上就好。
#include<bits/stdc++.h> using namespace std; int main(){ int n,i,j,k,a,b,c,d,e,tmp,num; int ord[120]; while(scanf("%d",&n)!=EOF){ a=b=c=d=e=-1; for(i=0;i<n;i++){ scanf("%d",&tmp); if(tmp==0){ a=tmp; }else if(tmp<10&&tmp>0){ b=tmp; }else if(tmp%10==0&&tmp!=100){ c=tmp; }else if(tmp!=100&&tmp%10!=0){ d=tmp; }else{ e=tmp; } } num=0; if(a!=-1){ ord[num++]=a; } if(c!=-1) ord[num++]=c; if(e!=-1) ord[num++]=e; if(b!=-1){ ord[num++]=b; } if(b==-1&&c==-1&&d!=-1){ ord[num++]=d; } printf("%d\n",num); printf("%d",ord[0]); for(i=1;i<num;i++){ printf(" %d",ord[i]); }printf("\n"); } return 0; }