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 == ) {
fb = ;
if (a == )
fa = ;
}
if (k == || (fa&&fb))return;
if (a%k == )dfs(a / k, b, k - );
if (b%k == )dfs(a, b/k, k - );
dfs(a, b, k - );
} int main() {
int a, b;
while (cin >> a >> b) {
if (a < b)swap(a, b);
fa = fb = ;
dfs(a, b, );
if (fa == && fb == ) cout << b << endl;
else cout << a << endl; }
}

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

  1. [ZJU 1003] Crashing Balloon

    ZOJ Problem Set - 1003 Crashing Balloon Time Limit: 2 Seconds      Memory Limit: 65536 KB On every J ...

  2. [ZOJ 1003] Crashing Balloon (dfs搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3 题目大意:给你a,b两个数,问当b由约数1到100组成时,a能否由其 ...

  3. 1003 Crashing Balloon

    考察DFS的应用,判断两个数的因子. #include <stdio.h> int f1,f2; void DFS(int m,int n,int k){ ){ f2=; ) f1=; } ...

  4. ZOJ 1003 Crashing Balloon

    #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using ...

  5. 【ZOJ1003】Crashing Balloon(DFS)

    Crashing Balloon Time Limit: 2 Seconds      Memory Limit: 65536 KB On every June 1st, the Children's ...

  6. ZOJ1003 Crashing Balloon

    Crashing Balloon Time Limit: 2 Seconds      Memory Limit: 65536 KB On every June 1st, the Children's ...

  7. 【Acm】算法之美—Crashing Balloon

    题目概述:Crashing Balloon On every  June 1st, the Children's Day, there will be a game named "crash ...

  8. 小鼠迷宫问题【sdut1157】【dfs,bfs综合题目】

    小鼠迷宫问题 Time Limit: 1500ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 小鼠a与小鼠b身处一个m×n的迷宫中,如图所示.每一个方格表示迷宫中 ...

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

    递归:就是出现这种情况的代码: (或者说是用到了栈) 解答树角度:在dfs遍历一棵解答树 优点:结构简洁缺点:效率低,可能栈溢出 递归的一般结构: void f() { if(符合边界条件) { // ...

随机推荐

  1. 8 -- 深入使用Spring -- 3...1 Resource实现类FileSystemResource

    8.3.1 Resource实现类------FileSystemResource:访问文件系统的资源的实现类 3.访问文件系统资源 Spring提供的FileSystemResource类用于访问文 ...

  2. POJ 3273 Monthly Expense(二分搜索)

    Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...

  3. c 编译和链接过程

    详解link  有 些人写C/C++(以下假定为C++)程序,对unresolved external link或者duplicated external simbol的错误信息不知所措(因为这样的错 ...

  4. Ansible 远程执行脚本

    1. 先在服务端创建一个 shell 脚本 [root@localhost ~]$ cat /tmp/test.sh #!/bin/bash echo "hello world" ...

  5. 搭建ntp服务器

    1.同步网络时间 先关闭掉ntp服务,使用ntpd同步网络时间. /etc/init.d/ntpd stop ntpdate 2.hk.pool.ntp.org 网络时间可以从http://www.p ...

  6. $ cd `dirname $0` 和PWD用法

      在命令行状态下单纯执行 $ cd `dirname $0` 是毫无意义的.因为他返回当前路径的".".这个命令写在脚本文件里才有作用,他返回这个脚本文件放置的目录,并可以根据这 ...

  7. 《转载》Tomcat内存设置详解

    原文地址:Java内存溢出详解 一.常见的Java内存溢出有以下三种: 1. java.lang.OutOfMemoryError: Java heap space ----JVM Heap(堆)溢出 ...

  8. windows内核情景分析之—— KeRaiseIrql函数与KeLowerIrql()函数

    windows内核情景分析之—— KeRaiseIrql函数与KeLowerIrql()函数 1.KeRaiseIrql函数 这个 KeRaiseIrql() 只是简单地调用 hal 模块的 KfRa ...

  9. Matlab练习——多项式和一元方程求解

    1. 一元函数 f(x) = x^3 + (x - 0.98)^2 / (x + 1.25)^3 - 5 * (x + 1 / x),求f(x) = 0 的根 %在函数文件中定义函数 function ...

  10. Python学习(22):模块

    转自 http://www.cnblogs.com/BeginMan/p/3183656.html 一.模块基础 1.模块 自我包含,且有组织的代码片段就是模块 模块是Pyhon最高级别的程序组织单元 ...