ZJU-1003 Crashing Balloon dfs,

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3

题意(难以描述):A,B两个人从1~100选数乘起来比谁的大(不能选重复的或者对方选的)。数小的人如果发现数大的人在撒谎,则他可以获胜。(当然都没撒谎大数赢,都撒谎了也是大数赢233)而他判断大数撒谎的方法就是找到自己选的一个数,是构成大数所必须的。/*给你两个数,由1~100选取不重复的数乘起来得到的。如果小的那个数包含了某个大数必须包含的因子,则*/

题解:从1到100枚举构成AB的方法,然后按这个逻辑判断:(A>B)

if能构成B {

    if能构成A:cout<<A;

    else cout<<B;

      }

  else cout<<B;

练搜索题时看到的一段超简单代码。

判断超简单:

将三个cout用两个flag来简化:具体实现如下。

搜索超简单:

从100 到 1 不断除 A或除 B 能除进 就按除掉的数继续dfs;

dfs结构很漂亮:结尾用dfs代替了循环,开头判断即使结束递归。

ac代码

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
int fa, fb;
void dfs(int a, int b, int k) {
    if (b == 1) {
        fb = 1;
        if (a == 1)
            fa = 1;
    }
    if (k == 1 || (fa&&fb))return;
    if (a%k == 0)dfs(a / k, b, k - 1);
    if (b%k == 0)dfs(a, b/k, k - 1);
    dfs(a, b, k - 1);
}

int main() {
    int a, b;
    while (cin >> a >> b) {
        if (a < b)swap(a, b);
        fa = fb = 0;
        dfs(a, b, 100);
        if (fa == 0 && fb == 1) cout << b << endl;
        else cout << a << endl;

    }
}

原文地址:https://www.cnblogs.com/SuuT/p/8585087.html

时间: 2024-08-05 19:53:55

ZJU-1003 Crashing Balloon dfs,的相关文章

1003 Crashing Balloon

考察DFS的应用,判断两个数的因子. 1 #include <stdio.h> 2 3 int f1,f2; 4 5 void DFS(int m,int n,int k){ 6 if(n==1){ 7 f2=1; 8 if(m==1) 9 f1=1; 10 } 11 if(f1&&f2||k==1) 12 return; 13 if(m%k==0) 14 DFS(m/k,n,k-1); 15 if(n%k==0) 16 DFS(m,n/k,k-1); 17 DFS(m,n,k

zoj 1003 Crashing Balloon

#include<stdio.h> #include<algorithm> using namespace std; int fa,fb; void dfs(int a,int b,int k) { if(b==1) { fb=1; if(a==1) fa=1; } if(k==1||(fa&&fb)) return ; if(a%k==0) dfs(a/k,b,k-1); if(b%k==0) dfs(a,b/k,k-1); dfs(a,b,k-1); } int

【ZOJ1003】Crashing Balloon(DFS)

Crashing Balloon Time Limit: 2 Seconds      Memory Limit: 65536 KB On every June 1st, the Children's Day, there will be a game named "crashing balloon" on TV.   The rule is very simple.  On the ground there are 100 labeled balloons, with the num

ZOJ1003 Crashing Balloon

Crashing Balloon Time Limit: 2 Seconds      Memory Limit: 65536 KB On every June 1st, the Children's Day, there will be a game named "crashing balloon" on TV.   The rule is very simple.  On the ground there are 100 labeled balloons, with the num

TOJ-1307 Crashing Balloon

On every June 1st, the Children's Day, there will be a game named "crashing balloon" on TV. The rule is very simple. On the ground there are 100 labeled balloons, with the numbers 1 to 100. After the referee shouts "Let's go!" the two

ZOJ1003: Crashing Balloon

Time Limit: 2 Seconds Memory Limit: 65536 KB On every June 1st, the Children's Day, there will be a game named "crashing balloon" on TV.   The rule is very simple.  On the ground there are 100 labeled balloons, with the numbers 1 to 100.  After

递归,回溯,DFS,BFS的理解和模板【摘】

递归:就是出现这种情况的代码: (或者说是用到了栈) 解答树角度:在dfs遍历一棵解答树 优点:结构简洁缺点:效率低,可能栈溢出 递归的一般结构: 1 void f() { 2 if(符合边界条件) { 3 /////// 4 return; 5 } 6 7 //某种形式的调用 8 f(); 9 } 回溯:递归的一种,或者说是通过递归这种代码结构来实现回溯这个目的.回溯法可以被认为是一个有过剪枝的DFS过程.解答树角度:带回溯的dfs遍历一棵解答树回溯的一般结构: 1 void dfs(int

FS,FT,DFS,DTFT,DFT,FFT的联系和区别 数字信号处理

DCT变换的原理及算法 文库介绍 对于初学数字信号处理(DSP)的人来说,这几种变换是最为头疼的,它们是数字信号处理的理论基础,贯穿整个信号的处理. 学习过<高等数学>和<信号与系统>这两门课的朋友,都知道时域上任意连续的周期信号可以分解为无限多个正弦信号之和,在频域上就表示为离散非周期的信号,即时域连续周期对应频域离散非周期的特点,这就是傅里叶级数展开(FS),它用于分析连续周期信号. FT是傅里叶变换,它主要用于分析连续非周期信号,由于信号是非周期的,它必包含了各种频率的信号,

什么时候用DFS,什么时候用BFS?

二维数组的题目,N小于20的,适用DFS.而一般 N<= 200,N<=1000这种,一定不可能用DFS去做.而且并不只是整个题目不能用DFS,其中的每一步也不能使用DFS. BFS的基本步骤 1.将初始点(一个或多个)加入一个集合尾 2.从集合头取出点,判断初始点的周边点,将符合条件的点加入队列 3.重复2操作,直至集合为空.(一般每个点只加入队列一次) 一般来说用DFS解决的问题都可以用BFS来解决. DFS(深搜的同时考虑回溯) bfs=队列,入队列,出队列:dfs=栈,压栈,出栈 bf