当前位置:网站首页>用递归和循环两种方法解决华为4月20日机试第一题(100分)
用递归和循环两种方法解决华为4月20日机试第一题(100分)
2022-06-21 05:57:00 【前行_的路上】
题目:

方法一:dfs暴力搜索
#include <iostream>
using namespace std;
#include <vector>
int cnt = 0;
int sum = 0;
vector<int> nums =
{ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8 };
void dfs(int target, int err, int index) {
if (err >= 3) return;
if (sum == target) {
cnt++;
return;
}
if (sum > target) {
return;
}
for (int i = index; i < nums.size(); i++) {
sum += nums[i];
dfs(target, err, i + 1);
sum -= nums[i];
err++;
}
}
int main() {
vector<bool> mark(nums.size(), 0);
int target;
while (cin >> target) {
cnt = 0;
dfs(target, 0, 0);
cout << cnt << endl;
}
system("pause");
return 0;
}方法二:三层循环
#include <iostream>
using namespace std;
#include <vector>
vector<int> nums =
{ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8 };
//方法二 三层循环
int solve(int target) {
int cnt = 0;
for (int i = 0; i < nums.size(); i++) {
//只错一个
if (100 - nums[i] == target) {
cnt++;
}
for (int j = i + 1; j < nums.size(); j++) {
//只错两个
if (100 - nums[i] - nums[j] == target) {
cnt++;
}
for (int k = j + 1; k < nums.size(); k++) {
int z = 100 - nums[i] - nums[j];
//错了三个
for (int l = k; l < nums.size(); l++) {
z -= nums[l];
}
if (z == target) cnt++;
}
}
}
return cnt;
}
int main() {
vector<bool> mark(nums.size(), 0);
int target;
while (cin >> target) {
int cnt = solve(target);
cout << cnt << endl;
}
system("pause");
return 0;
}边栏推荐
- tf.AUTO_REUSE作用
- DP backpack summary
- El table circular upgrade
- 复制 代码生成器 生成的代码到idea中,运行后网址报错怎么解决
- Cross chain revelation: has your assets really been transferred in the process of cross chain?
- 代码中变量的重要性
- 实现杀菌、除臭、除异味多效集成的UVC杀菌灯
- Le premier caractère unique de la chaîne (4)
- Things to think about before using mysqldump
- 硬件探索——数字钟的设计与制作
猜你喜欢

数字式温度传感器工作原理以及测温原理分析
![[MYSQL] MYSQL文件结构,页与行记录](/img/e3/8b6c39f299679522d84cad64af7a42.png)
[MYSQL] MYSQL文件结构,页与行记录

Memorizing Normality to Detect Anomaly: Memory-augmented Deep Autoencoder for Unsupervised Anomaly D

Implementation and analysis of transformer model

After the code is generated by the code generator, the copy is completed, and the module is not displayed on the web page

397 linked list (206. reverse linked list & 24. exchange nodes in the linked list in pairs & 19. delete the penultimate node of the linked list & interview question 02.07. link list intersection & 142

Connection refused : no futher information : localhost/127.0.0.1:6379

基于注意力的seq2seq模型

Quartz. Net getting started

sqli-labs25
随机推荐
397 linked list (206. reverse linked list & 24. exchange nodes in the linked list in pairs & 19. delete the penultimate node of the linked list & interview question 02.07. link list intersection & 142
攻防世界 php_rce
【JVM】方法区
IDEA 使用记录
tf.QueueBase
How to select a game server with high stability and good defense performance
Research and Analysis on the current situation of wireless Internet of things sensor market in China and forecast report on its development prospect (2022)
Analog ambient light sensor chip used in backlight display of electronic products
Microbial ecological data analysis - redundancy analysis
Leetcode question brushing - (4) the first unique character in the string
【MYSQL】MySQL的SQL语句执行流程
Attention based seq2seq model
Eth Denver conference is a Web3 foam signal? See what investors and builders say respectively
kali快捷键和设置方式
Picture steganography: Method 1
牛客-TOP101-BM25
代码生成器文件运行出错:The server time zone value ‘�й���ʱ��‘ is unrecognized or represents more than one time
Attack and defense world PHP_ rce
Importance of variables in code
高德地图Loca 数据可视化 API 2.0的用法