2013年第四届蓝桥杯CC++ A组国赛 —— 第四题:约数倍数选卡片

    xiaoxiao2022-06-25  198

    标题:约数倍数选卡片

    闲暇时,福尔摩斯和华生玩一个游戏:

    在N张卡片上写有N个整数。两人轮流拿走一张卡片。要求下一个人拿的数字一定是前一个人拿的数字的约数或倍数。例如,某次福尔摩斯拿走的卡片上写着数字“6”,则接下来华生可以拿的数字包括:

    1,2,3, 6,12,18,24 …

    当轮到某一方拿卡片时,没有满足要求的卡片可选,则该方为输方。

    请你利用计算机的优势计算一下,在已知所有卡片上的数字和可选哪些数字的条件下,怎样选择才能保证必胜!

    当选多个数字都可以必胜时,输出其中最小的数字。如果无论如何都会输,则输出-1。

    输入数据为2行。第一行是若干空格分开的整数(每个整数介于1~100间),表示当前剩余的所有卡片。 第二行也是若干空格分开的整数,表示可以选的数字。当然,第二行的数字必须完全包含在第一行的数字中。

    程序则输出必胜的招法!!

    例如: 用户输入: 2 3 6 3 6 则程序应该输出: 3

    再如: 用户输入: 1 2 2 3 3 4 5 3 4 5 则程序应该输出: 4

    资源约定: 峰值内存消耗 < 64M CPU消耗 < 1000ms

    请严格按要求输出,不要画蛇添足地打印类似:“请您输入…” 的多余内容。

    所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。

    注意: main函数需要返回0 注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。 注意: 所有依赖的函数必须明确地在源文件中 #include , 不能通过工程设置而省略常用头文件。

    提交时,注意选择所期望的编译器类型(千万不要混淆c和cpp)。

    Code

    /* ^....0 ^ .1 ^1^ .. 01 1.^ 1.0 ^ 1 ^ ^0.1 1 ^ ^..^ 0. ^ 0^ .0 1 .^ .1 ^0 .........001^ .1 1. .111100....01^ 00 11^ ^1. .1^ 1.^ ^0 0^ .^ ^0..1 .1 1..^ 1 .0 ^ ^ 00. ^^0.^ ^ 0 ^^110.^ 0 0 ^ ^^^10.01 ^^ 10 1 1 ^^^1110.1 01 10 1.1 ^^^1111110 010 01 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p created by VB_KoKing on 2019-05-19:20. /* Procedural objectives: Variables required by the program: Procedural thinking: Functions required by the program: Determination algorithm: Determining data structure: */ /* My dear Max said: "I like you, So the first bunch of sunshine I saw in the morning is you, The first gentle breeze that passed through my ear is you, The first star I see is also you. The world I see is all your shadow." FIGHTING FOR OUR FUTURE!!! */ #include <cstdio> #include <vector> #include <cstring> #include <iostream> #define N 10007 using namespace std; bool vis[N]; vector <int> get[N]; int num_0[N],num_1[N]; bool dfs(int num){ vis[num]=true; for(int i=get[num].size()-1;i>-1;i--){ int v=get[num][i]; if((!vis[v])&&dfs(v)){ vis[num]=false; return false; } } vis[num]=false; return true; } int main(){ int n,m; memset(vis,0,sizeof(vis)); while(~scanf("%d",&num_0[n])){ for(int i=0;i<n;i++){ if(num_0[i]%num_0[n]==0||num_0[n]%num_0[i]==0){ get[i].push_back(n);; get[n].push_back(i); } } n++; if(getchar()=='\n') break; } while(~scanf("%d",&num_1[m++])){ if(getchar()=='\n') break; } for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if((num_1[i]==num_0[j])&&(dfs(j))) return 0*printf("%d\n",num_0[j]); } } return 0*puts("-1"); } Alex 007 认证博客专家 机器学习 NLP TensorFlow 我是 Alex 007,一个热爱计算机编程和硬件设计的小白。为啥是007呢?因为叫 Alex 的人太多了,再加上每天007的生活,Alex 007就诞生了。如果你喜欢我的文章的话,给个三连吧!

    最新回复(0)