POJ1426 Find The Multiple
The Find multiple
2023-09-11 14:22:51 时间
一开始用queue超时了,可能是因为太慢了?
queue方法:
#include<cstdio> #include<iostream> #include<string> #include<cstring> #include<cmath> #include<stdlib.h> #include<algorithm> #include<vector> #include<queue> #include<stack> using namespace std; void bfs(int n) { queue<long long>q; while(!q.empty()) { q.pop(); } q.push(1); while(!q.empty()) { long long current =q.front(); q.pop(); if(current%n==0) { cout<<current<<endl; return; } q.push(current*10); q.push(current*10+1); } } int main() { int n; while(cin>>n) { if(n==0) break; bfs(n); } return 0; }
用数组模拟队列的方法:
#include<iostream> using namespace std; long long que[9999999]; void bfs(int n) { int head=1,tail=1; que[tail++]=1; while(head<tail) { long long now=que[head++];//每次往后一个,相当于Pop了。 if(now%n==0) { cout<<now<<endl; return; } que[tail++]=now*10; que[tail++]=now*10+1; } } int main() { int n; while(scanf("%d",&n),n!=0) { bfs(n); } return 0; }
相关文章
- java错误:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Bu
- [Javascript] Broadcaster + Operator + Listener pattern -- 26. Share the Same Broadcaster Values Across Multiple Listeners
- [AngularJS] Store the entry url and redirect to entry url after Logged in
- [Angular 8] Calculate and Measure Performance budgets with the Angular CLI
- Error: String or binary data would be truncated. The statement has been terminated
- 【例 7-12 UVA - 1343】The Rotation Game
- 成功解决ERROR: Could not find a version that satisfies the requirement xgboost (from versions: none) ERR
- 成功解决MSB8020 The build tools for v141 (Platform Toolset = ‘v141‘) cannot be found. To build using the
- 成功解决ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() o
- 已解决stderr: ERROR: Could not find a version that satisfies the requirement gradio==3.23
- The current user does not have write permissions to the target environment.
- UVa - 1025 - A Spy in the Metro
- chroumium electron apply_all_patches.py patch git error WindowsError: [Error 2] The system cannot find the file specified
- The old instructions for getting the code
- `Warning: The default method for RunUMAP has changed from calling Python UMAP via reticulate to the
- Error:The supplied javaHome seems to be invalid. I cannot find the java executable. Tried location:
- Ubuntu下交叉编译make menuconfig时出错:Unable to find the ncurses libraries or the required header files.