poj1426
bfs暴力
通过bfs搜索每个可能性,从1开始,通过使用110和110+1 来遍历最后一位为1或者0的所有情况;
使用stl的队列会超时,所以使用数组模拟队列。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #include <iostream> #include <queue> using namespace std; #define maxn 10000000 long long q[maxn];
long long bfs(int n) { int front = 0; int rear = 0; q[rear++] = 1; for(;;) { long long f = q[front++]; if(0 == f % n) return f; else { q[rear++] = f * 10; q[rear++] = f * 10 + 1; } } }
int main() { int n; while(cin >> n && n) cout << bfs(n) <<endl; return 0; }
|