当前位置:网站首页>找数字(DFS)
找数字(DFS)
2022-04-23 01:21:00 【OldLeft】
给一个数 n,让你找出一个只由 0,1 组成的十进制数 m,要求这个正整数 m 可以被 n 整除。
输入格式
输入一个整数 n (1≤n<200)。
输出格式
对于输入整数 n 的每一个值,输出 m 的相应值,保证有一个数字长度小于 19 位的数字。如果有一个给定值 n 有多个解,其中任何一个都是可以接受的。
本题答案不唯一,符合要求的答案均正确
样例输入
2
样例输出
10
思路:
在这里我们可以直接想到的就是枚举出所有的字符串,然后查看哪一个字符串是满足条件的。
比如 1 10 11 …
首先可以肯定的是,第一个字符是1。因为第一位为0是不符合数学定义的。
然后我们有两个选择,给这个字符串添加1或者添加0。(这里为了计算方便,我们使用一个整数代表这个字符串)
接下来我们要注意递归出口,和最优性剪枝就行了。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
bool f;
void dfs(int cnt,ll num){
if(f){
// 递归出口1 找到了一种可行性答案。
return;
}
if(cnt>=19){
// 递归出口2 字符串的长度超多 19。
return;
}
if(num%n==0){
// 最优性剪枝 当已经找到了一种可行性解的时候,就没有必要继续搜索了
f=true;
cout<<num<<endl;
return;
}
dfs(cnt+1,num*10);// num* 10+0 这个意思是在一个数字后面添加一个0 例如:12 -> 120
dfs(cnt+1,num*10+1);// 这里和上面的思想一样,这个时候想必你应该学会了扩展吧 ^ _ ^ 。比如把一个字符串变成一个整数
}
int main(){
cin>>n;
dfs(1,1);
return 0;
}
版权声明
本文为[OldLeft]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43833610/article/details/115771914
边栏推荐
- Five commonly used order receiving platforms recommended by programmers
- Linked list dynamic header insertion
- GBase 8s 备份介绍
- Gbase 8s检查点简介
- 再谈被动安全 教你看懂中保研碰撞测试的评级报告
- IMX6ULL裸机开发之硬件IIC分析及配置过程
- Research and application of power monitoring system in sports training center
- [JS] realize the export of PDF from the specified area of the web page
- Practice and exploration of knowledge map visualization technology in meituan
- 世界读书日:18本豆瓣评分9.0以上的IT书值得收藏
猜你喜欢

Hardware IIC analysis and configuration process of imx6ull bare metal development

Smart business card applet business card details page function implementation key code

How does zhiting connect Xiaomi smart speakers?

Research and application of power monitoring system for employee apartment building construction project

Examples of branch and loop statements

Generating class diagram with EA reverse engineering code

代码实现发邮件---sendemails
![[the first contact between Android engineers and smart home products ③] the specific implementation of smartconfig one key distribution network on the hardware side | the specific implementation of es](/img/24/00f0b035f623c4fca2c0f3a5c0af5b.png)
[the first contact between Android engineers and smart home products ③] the specific implementation of smartconfig one key distribution network on the hardware side | the specific implementation of es

gin框架的学习--golang
![[Ethernet switching security] - switch flow control / DHCP snooping / IP source guard](/img/cc/bbabc5621b8485d04ffa7d4d5fad2e.png)
[Ethernet switching security] - switch flow control / DHCP snooping / IP source guard
随机推荐
gin -get请求的小示例2-Handle处理post请求
“自虐神器”一夜爆火:用手柄控制自己的脸,代码自取,后果自负
[Ethernet switching security] - switch flow control / DHCP snooping / IP source guard
Innovative practice of short video content understanding and generation technology in meituan
[interview skills] how to face an interview without a leading group
安全用电管理平台在靖边博物馆安全用电管理系统的应用
GBASE 8s并发控制之封锁类型
Let's talk about passive safety again. I'll teach you to understand the rating report of China Insurance Research Institute collision test
Yunrong technology joined the dragon dragon dragon community to help the digital transformation of the financial industry
世界读书日:18本豆瓣评分9.0以上的IT书值得收藏
Five commonly used order receiving platforms recommended by programmers
Live broadcast software | IPTV live broadcast software | TV live broadcast | tvplayer IPTV easyplayer | youwo live broadcast | customized development of super live broadcast software
Introduction to gbase 8s storage structure and space management
12 years of testing predecessors give you some suggestions for learning software testing
【Android工程师与智能家居产品的第一次接触③】SmartConfig一键配网在硬件端的具体实现|ESP8266一键配网在Arduino的具体实现|玉念聿辉
Gbase 8s检查点简介
Configuration of imx6ull bare metal development analysis and configuration process of elcdif lighting RGB LCD
The origin explanation and use example of image pre training model
轮转法分片
DCB "first brother" xianruida, what does it rely on to raise the value curve?