0711编程之美找符合条件的整数

xiaoxiao2021-02-28  98

#include<iostream> #include<queue> using namespace std; int x(int n,int m) { int temp = n*m; while (temp) { if (temp % 10 >= 2) return 0; temp=temp/10; } return 1; } int main711()//解法1 { int n; while (cin >> n) { int m; for (m = 1; m++;) if (x(n, m)) { cout << n << "," << m*n << endl; break; } } return 0; } int main()//利用深度优先搜索 解法2 { int n; while (cin >> n) { queue<int>a; a.push(1); while (!a.empty()) { int temp = a.front(); a.pop(); if (temp%n == 0) { cout << n << "," << temp << endl; break; } a.push(temp * 10); a.push(temp * 10+1); } } return 0; }
转载请注明原文地址: https://www.6miu.com/read-70644.html

最新回复(0)