C/C++教程

codeforces 1097B. Petr and a Combination Lock(1200)

本文主要是介绍codeforces 1097B. Petr and a Combination Lock(1200),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

链接:https://codeforces.ml/problemset/problem/1097/B

题意:判断能否用所给的数据凑出360的倍数或者0;

题解:

n才15,暴力dfs走起;

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int num[100005] = { 0 };
int ans = 0;
void dfs(int step, int n, int sum)
{
	/*cout << sum <<" " << " ";*/
	if (step == n+1)
	{
		cout << endl;
		if (sum == 0)
		{
		/*	cout << "1";*/
			ans = 1;
		}
		else if (sum % 360 == 0)
		{
			/*cout << '2';*/
			ans = 1;
		}
		return;
	}
	dfs(step + 1, n, sum + num[step]);


	dfs(step + 1, n, sum - num[step]);
}
int main()
{
	int t;
	cin >> t;
	for (int i = 1; i <= t; i++)
	{
		cin >> num[i];
	}
	dfs(1, t, 0);
	//cout << ans;
	if (ans==1)
		cout << "YES";
	else
		cout << "NO";
}

这篇关于codeforces 1097B. Petr and a Combination Lock(1200)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!