Find The Multiple——广搜+打表

标题 Find The Multiple

在这里插入图片描述

分析

因为要求十进制数字中只能由0 1,这个数字从1 10 11 110 111…可看出来,均可从前一个数字*10 or *10+1得出;
那么就通过广度搜索遍历得出数字,然后再判断是否符合整除条件即可。
为了避免超时,采用打表方式进行先行计算。
坑:
最开始使用c++的编译环境,

代码

#include<iostream>
#include<queue>
using namespace std;
int n;
long long a[205];
void bfs() {
	queue<long long> Q;
	for(int i=1; i<=201; i++) {
		Q.push(1);
		while(!Q.empty()) {
			long long b=Q.front();
			if(b%i==0) {
				a[i]=b;
				break;
			}
			Q.pop();
			Q.push(b*10);//+0
			Q.push(b*10+1);//+1
		}
	}
}
int main() {
	bfs();
	while(cin>>n&&n) {
		cout<<a[n]<<endl;
	}
}
### C++ `string` 类的 `find()` 函数使用方法 #### 查找单个字符 当需要在一个字符串中查找某个特定字符的位置时,可以调用 `find()` 方法并传入要查找的字符作为参数。如果找到了该字符,则返回其首次出现的位置;如果没有找到,则返回 `string::npos`。 ```cpp #include <iostream> #include <string> int main() { std::string str = "hello world"; size_t position = str.find('w'); if (position != std::string::npos) { std::cout << "Character 'w' found at index: " << position << '\n'; } else { std::cout << "Character not found\n"; } } ``` #### 查找子串 除了查找单个字符外,`find()` 还支持查找整个子串。只需传递目标子串即可获得其起始位置的信息: ```cpp #include <iostream> #include <string> int main() { std::string str = "The quick brown fox jumps over the lazy dog."; size_t position = str.find("fox"); if (position != std::string::npos) { std::cout << "'fox' starts from index: " << position << '\n'; } else { std::cout << "Substring not found.\n"; } } ``` #### 设置起点进行查找 有时可能希望从某一处开始而不是从头到尾遍历整个字符串来寻找匹配项。此时可以通过向 `find()` 提供第二个参数——即搜索范围内的初始索引来实现这一点: ```cpp #include <iostream> #include <string> int main() { std::string str = "This is an example sentence with multiple occurrences of word."; size_t first_occurrence = str.find("word"); size_t second_occurrence = str.find("word", first_occurrence + 1); if (second_occurrence != std::string::npos) { std::cout << "Second occurrence of 'word' begins at index: " << second_occurrence << '\n'; } else { std::cout << "No more occurrences after the first one.\n"; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MORE_77

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值