题目链接:http://codeforces.com/problemset/problem/305/A

题目意思:给出一个序列,需要从中选择一些数,这些数需要满足:任意的两个数中每一位至少有一个数满足该位有0。例如,406, 10(可以看成010)是符合条件的; 406, 11 就不符合条件,因为个位上这两个数都不包含0。

一开始想得太过复杂:把整型的数保存到字符数组中,再用strstr函数找出是否有0的存在,但是这样做根本不能解决问题,因为不能得出0的具体位置。并且不能判断出两个数中的不同位至少有一个有0这个问题;后来甚至想到用位运算,看了下tutorial,原来又把问题想复杂了。

其实,解决的方法不难。首先看数据范围,最大是100,最小是0。这两个数肯定是要保存下来的,因为它和任何一个在区间(0, 100)中的数相加都符合每一位至少有一个数有0。接着考虑个位上的数,即(0,10),只需要保存一个,还有[10, 100)中被10整除的数,也是只需要保存一个。举个例子,比如8,30, 100, 0,两两组合都符合至少两个数中满足某一位是0的(不够位的,可以在前面添0,8可以看成008或者08)。至于[10, 100)中不能被10整除的数有一种情况也是需要只保存一个的,就是当序列中只有0或100或两个同时都有。

特别要注意一个比较容易遗忘的细节,很阴险的说~~~当序列中只有一个数的时候,无论是什么,都需要输出。隐含是满足条件的!!!卑鄙!!!

 #include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std; const int maxn = + ;
int ans[maxn]; int main()
{
int i, k, tmp, tmp1, cnt, cnt1, flag, flag1, flag2, flag3;
while (scanf("%d", &k) != EOF)
{
// freopen("in.txt", "r", stdin);
flag = flag1 = flag2 = flag3 = ;
cnt1 = cnt= ;
for (i = ; i < k; i++)
{
scanf("%d", &tmp);
if ((tmp == || tmp == ) && cnt1 <= ) // 0 or 100,cnt1不用也可以,只是为了严谨,防止序列中有多个重复的0或100
{
ans[cnt++] = tmp;
cnt1++;
flag = ;
}
else if (tmp >= && tmp < ) // [10, 100)
{
if (tmp % == && !flag1) // 10的倍数
{
ans[cnt++] = tmp;
flag1 = ;
}
else if (!flag2) // 不是10的倍数
{
tmp1 = tmp; // 暂时保留,因为有可能不需要输出
flag2 = ;
}
}
else if (tmp > && tmp < && !flag3) // (0, 10)
{
ans[cnt++] = tmp;
flag3 = ;
}
}
if (flag && !flag1 && !flag3 && k != )
{
if (flag2) // 这个条件十分关键
ans[cnt++] = tmp1;
}
if (flag2 && !flag && !flag1 && !flag3)
printf("1\n%d\n", tmp);
else
{
printf("%d\n", cnt);
for (i = ; i < cnt; i++)
printf("%d ", ans[i]);
printf("\n");
}
}
return ;
}

codeforces A. Strange Addition 解题报告的更多相关文章

  1. Codeforces Round 665 赛后解题报告(暂A-D)

    Codeforces Round 665 赛后解题报告 A. Distance and Axis 我们设 \(B\) 点 坐标为 \(x(x\leq n)\).由题意我们知道 \[\mid(n-x)- ...

  2. Codeforces Round 662 赛后解题报告(A-E2)

    Codeforces Round 662 赛后解题报告 梦幻开局到1400+的悲惨故事 A. Rainbow Dash, Fluttershy and Chess Coloring 这个题很简单,我们 ...

  3. Codeforces Round #277.5 解题报告

    又熬夜刷了cf,今天比正常多一题.比赛还没完但我知道F过不了了,一个半小时贡献给F还是没过--应该也没人Hack.写写解题报告吧= =. 解题报告例如以下: A题:选择排序直接搞,由于不要求最优交换次 ...

  4. timus 1175. Strange Sequence 解题报告

    1.题目描述: 1175. Strange Sequence Time limit: 1.0 secondMemory limit: 2 MB You have been asked to disco ...

  5. codeforces B. Simple Molecules 解题报告

    题目链接:http://codeforces.com/problemset/problem/344/B 题目意思:这句话是解题的关键: The number of bonds of an atom i ...

  6. codeforces 591A. Wizards' Duel 解题报告

    题目链接:http://codeforces.com/problemset/problem/591/A 题目意思:其实看下面这幅图就知道题意了,就是Harry 和 He-Who-Must-Not-Be ...

  7. codeforces 582A. GCD Table 解题报告

    题目链接:http://codeforces.com/problemset/problem/582/A 网上很多题解,就不说了,直接贴代码= = 官方题解: http://codeforces.com ...

  8. codeforces 581C. Developing Skills 解题报告

    题目链接:http://codeforces.com/problemset/problem/581/C 题目意思:给出 n 个数:a1, a2, ..., an (0 ≤ ai ≤ 100).给出值 ...

  9. codeforces 577B. Modulo Sum 解题报告

    题目链接:http://codeforces.com/problemset/problem/577/B 题目意思:就是给出 n 个数(a1, a2, ..., an) 和 m,问能不能从这 n 个数中 ...

随机推荐

  1. Yii2结合webuploader实现图片上传

    js中 uploader = WebUploader.create({ // 自动上传. auto : true, // swf文件路径 swf : 'webuploader/Uploader.swf ...

  2. java2集合框架的一些个人分析和理解

    Java2中的集合框架是广为人知的,本文打算从几个方面来说说自己对这个框架的理解. 下图是java.util.Collection的类图(基本完整,有些接口如集合类均实现的Cloneable.Seri ...

  3. BZOJ-3226 校门外的区间 线段数+拆点(类似的思想)

    shabi题....bzoj关键字检查freopen??可怕,,1A的卡了一小时.... 3226: [Sdoi2008]校门外的区间 Time Limit: 10 Sec Memory Limit: ...

  4. 【poj1962】 Corporative Network

    http://poj.org/problem?id=1962 (题目链接) 时隔多年又一次写带权并查集. 题意 n个节点,若干次询问,I x y表示从x连一条边到y,权值为|x-y|%1000:E x ...

  5. [NOIP2009] 普及组

    多项式输出 模拟 /*by SilverN*/ #include<algorithm> #include<iostream> #include<cstring> # ...

  6. 洛谷P1136 迎接仪式

    题目描述 LHX教主要来X市指导OI学习工作了.为了迎接教主,在一条道路旁,一群Orz教主er穿着文化衫站在道路两旁迎接教主,每件文化衫上都印着大字.一旁的Orzer依次摆出“欢迎欢迎欢迎欢迎……”的 ...

  7. Linux Rootkit Learning

    目录 . 学习Rootkit需要了解的基础知识 . 挂钩(HOOKING) . 直接内核对象操作 . LSM框架(Linux Security Module)于LKM安全 . rootkit检测技术及 ...

  8. maven 热部署成功案列

    首先配置tomcat-user.xml,这个文件是在tomcat的conf文件夹下面 在</tomcat-users>前添加这段 <role rolename="admin ...

  9. UVA 1149 Bin Packing

    传送门 A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the sa ...

  10. POJ 1789Truck History(pirme)

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22648   Accepted: 8781 De ...