Forrest


私信TA

用户名:dotcpp0717441

访问量:1695

签 名:

等  级
排  名 145
经  验 7159
参赛次数 1
文章发表 79
年  龄 0
在职情况 教师
学  校 优学乐程
专  业

  自我简介:

解题思路: 搜索回溯计算数字序列,

注意事项: 在递归之后, 恢复res值以做下次计算
参考代码:

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e4 + 10;
int a[N];
int n,k,res;
bool ans = false;
void dfs(int x){
    for (int j = 1; j <= 2; j ++)
    {
        a[x] *= -1;
        res += a[x];
        if(x == n ){
            if(abs(res) % k == 0) ans = true;
            return;
        }
        else
            dfs(x + 1);
        res -= a[x];
    }
} 
int main()
{
    cin >> n >> k;
    for(int i = 1; i <= n; i ++) cin >> a[i];
    dfs(1);
    cout << (ans ? "YES" :  "NO");
    return 0;
}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区