学习C++从娃娃抓起!记录下在学而思小猴编程学习过程中的题目,记录每一个瞬间。侵权即删,谢谢支持!
附上汇总贴:小猴编程C++ | 汇总-CSDN博客
【题目描述】
小猴是一个热爱数学的猴子,他住在一个神秘的数字世界里。这个世界里,所有的数字都是由 0 和 1 组成的。小猴最近对数字倍数产生了浓厚的兴趣,他想知道在这个世界里,仅由 0 和 1 构成的十进制数字中,最小的 x 的倍数的数字是多少?
【输入】
输入第一行,一个正整数 q 表示询问次数
接下来 q 行,每行一个正整数 xi,表示第 i 次询问的数字
【输出】
输出共 q 行, 其中第 i 行表示第 i 个询问的答案
【输入样例】
3
5
27
527
【输出样例】
10
1101111111
1101110111
【代码详解】
#include <bits/stdc++.h>
using namespace std;
struct node {
int r;
string x;
};
int n;
bool vis[10005];
string bfs(int x)
{
memset(vis, 0, sizeof(vis));
queue<node> q;
node t = {1, "1"};
q.push(t);
vis[1]=1;
while (!q.empty()) {
node f = q.front();
q.pop();
if (f.r==0) {
return f.x;
}
if (vis[f.r*10%x]==0) {
node t1 = {f.r*10%x, f.x+"0"};
q.push(t1);
vis[f.r*10%x]=1;
}
if (vis[(f.r*10+1)%x]==0) {
node t2 = {(f.r*10+1)%x, f.x+"1"};
q.push(t2);
vis[(f.r*10+1)%x]=1;
}
}
}
int main()
{
cin >> n;
while (n--) {
int x;
cin >> x;
if (x==1) cout << 1 << endl;
else {
cout << bfs(x) << endl;
}
}
return 0;
}
【运行结果】
3
5
10
27
1101111111
527
1101110111