zoukankan      html  css  js  c++  java
  • codeforces 1097b(dfs)

    B. Petr and a Combination Lock
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Petr has just bought a new car. He's just arrived at the most known Petersburg's petrol station to refuel it when he suddenly discovered that the petrol tank is secured with a combination lock! The lock has a scale of 360360 degrees and a pointer which initially points at zero:

    Petr called his car dealer, who instructed him to rotate the lock's wheel exactly nn times. The ii-th rotation should be aiai degrees, either clockwise or counterclockwise, and after all nn rotations the pointer should again point at zero.

    This confused Petr a little bit as he isn't sure which rotations should be done clockwise and which should be done counterclockwise. As there are many possible ways of rotating the lock, help him and find out whether there exists at least one, such that after all nn rotations the pointer will point at zero again.

    Input

    The first line contains one integer nn (1n151≤n≤15) — the number of rotations.

    Each of the following nn lines contains one integer aiai (1ai1801≤ai≤180) — the angle of the ii-th rotation in degrees.

    Output

    If it is possible to do all the rotations so that the pointer will point at zero after all of them are performed, print a single word "YES". Otherwise, print "NO". Petr will probably buy a new car in this case.

    You can print each letter in any case (upper or lower).

    Examples
    input
    Copy
    3
    10
    20
    30
    
    output
    Copy
    YES
    
    input
    Copy
    3
    10
    10
    10
    
    output
    Copy
    NO
    
    input
    Copy
    3
    120
    120
    120
    
    output
    Copy
    YES
    
    Note

    In the first example, we can achieve our goal by applying the first and the second rotation clockwise, and performing the third rotation counterclockwise.

    In the second example, it's impossible to perform the rotations in order to make the pointer point at zero in the end.

    In the third example, Petr can do all three rotations clockwise. In this case, the whole wheel will be rotated by 360360 degrees clockwise and the pointer will point at zero again.

     题意:

    第一行输入n,接下来给出A1-n数,每个数可以表示顺时针或逆时针A1-n度,问是否可以使指针指在0度处

    思路:使用dfs搜索

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1e5+5;
    int n,a[20];
    bool dfs(int x,int s){
    if(x==n)
    return s%360==0?1:0;
    return dfs(x+1,s+a[x+1])||dfs(x+1,s-a[x+1]);//return a||b的意思就是如果a是true则返回a,否则返回b
    }
    int main(){
    cin>>n;
    for(int i=1;i<=n;i++)
    scanf("%d",a+i);
    printf(dfs(0,0)?"YES ":"NO ");
    }

     

  • 相关阅读:
    C# 模拟浏览器请求
    关于获取时间后,时间格式为几天前,几小时前格式转化
    关于通用的C#后台获取前台页面的标签的正则表达式
    关于getHTML()方法和getHtmlAjax()方法 GetHttpLength, 清除HTML标签
    性能测试术语
    聚合报告中90% Line涉及到百分位数的概念
    使用Windows的cmd运行(或通过批处理启动)Python项目(多目录项目)提示找不到模块的解决办法
    OSError: [WinError 6] 句柄无效的解决办法
    python中日志输出重复的解决办法
    截图方法get_screenshot_as_file()注意点
  • 原文地址:https://www.cnblogs.com/fanyu1/p/12519234.html
Copyright © 2011-2022 走看看