Codeforces——Hello 2019(待更新)

A. Gennady and a Card Game

Gennady owns a small hotel in the countryside where he lives a peaceful life. He loves to take long walks, watch sunsets and play cards with tourists staying in his hotel. His favorite game is called "Mau-Mau".

To play Mau-Mau, you need a pack of 52 cards. Each card has a suit (Diamonds — D, Clubs — C, Spades — S, or Hearts — H), and a rank (2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, or A).

At the start of the game, there is one card on the table and you have five cards in your hand. You can play a card from your hand if and only if it has the same rank or the same suit as the card on the table.

In order to check if you'd be a good playing partner, Gennady has prepared a task for you. Given the card on the table and five cards in your hand, check if you can play at least one card.

Input

The first line of the input contains one string which describes the card on the table. The second line contains five strings which describe the cards in your hand.

Each string is two characters long. The first character denotes the rank and belongs to the set {2,3,4,5,6,7,8,9,T,J,Q,K,A}. The second character denotes the suit and belongs to the set {D,C,S,H}.

All the cards in the input are different.

Output

If it is possible to play a card from your hand, print one word "YES". Otherwise, print "NO".

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

代码:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
int main()
{
    string s,t[5];
    bool a=false;
    cin>>s;
    for(int i=0;i<5;i++)
    {
        cin>>t[i];
        if(s[0]==t[i][0]||s[1]==t[i][1])
        {
            a=true;
            break;
        }
    }
    if(a) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    return 0;
}

B. Petr and a Combination Lock

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 360 degrees and a pointer which initially points at zero:

Petr called his car dealer, who instructed him to rotate the lock's wheel exactly n times. The i-th rotation should be ai 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 n (1≤n≤15) — the number of rotations.

Each of the following n lines contains one integer ai (1≤ai≤180) — the angle of the i-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).

分析:累加或累减的dfs

代码:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
int n,a[20];
int main()
{
    bool dfs(int x,int y);
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    if(dfs(0,0)) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    return 0;
}

bool dfs(int x,int y)
{
    if(x==n) return !(y%360);
    if(dfs(x+1,y+a[x])) return true;
    if(dfs(x+1,y-a[x])) return true;
    return false;
}

猜你喜欢

转载自blog.csdn.net/baoza_w/article/details/85834293