解题思路: 搜索回溯计算数字序列,
注意事项: 在递归之后, 恢复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 人评分
WU-C语言程序设计教程(第三版)课后习题11.11 (C++代码)(想学链表的可以看看)浏览:1358 |
【计算球体积】 (C语言代码)浏览:1103 |
字符逆序 (C语言代码)浏览:646 |
简单的a+b (C语言代码)浏览:531 |
输入输出格式练习 (C语言代码)浏览:850 |
C语言训练-自守数问题 (C语言代码)浏览:748 |
简单的事情 (C语言代码)浏览:638 |
C语言程序设计教程(第三版)课后习题11.3 (C语言代码)浏览:643 |
1231题解(注意理解“输入多个测试实例”)浏览:789 |
老王赛马 (C++代码)浏览:905 |