codeforce-305A--Strange Addition (贪心)
来源:互联网 发布:辐射4 画面优化设置 编辑:程序博客网 时间:2024/05/16 10:49
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?
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).
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.
4100 10 1 0
40 1 10 100
32 70 3
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;}
- codeforce-305A--Strange Addition (贪心)
- CF 305A(Strange Addition-贪心+分类讨论)
- codeforces 305A Strange Addition (模拟)
- codeforces-305A Strange Addition
- CodeForces 305A Strange Addition(想法题)
- codeforces-305A Strange Addition(思维+模拟)
- CodeForces 305A Strange Addition(思维题)
- CF--518A--Strange Addition
- Strange Addition
- codeforces Round #184 Div.2 - A. Strange Addition
- CodeForce 492C(贪心)
- A strange lift (HDU1548)
- codeforce D. Innokenty and a Football League 贪心,模拟
- Codeforce 780D Innokenty and a Football League (贪心+模拟)
- CodeForce 3B Lorry(贪心)
- codeforce 3B. Lorry(贪心)
- codeforce 732e Sockets (贪心)
- Codeforce Round #438 C.Bus(贪心)
- JAVA开发的23种设计模式之 --- 原型模式
- 欢迎使用CSDN-markdown编辑器
- MyBatis项目查询数据库Could not find result map java.lang.String
- Window 通过cmd查看端口占用,杀死进程方法
- Spring环境下ActiveMQ的配置与应用(入门篇)
- codeforce-305A--Strange Addition (贪心)
- 数据通信基础
- Dataquest学习代码笔记
- hdu 6127 Hard challenge
- 16级暑假练习赛二--F
- hdu 6127-数学
- Hibernate一级缓存、二级缓存区别
- pandas入门——数据合并concat函数
- 检测到有潜在危险的 Request.Form 值