poj1426(读懂题目真的很重要)

题目连接:http://poj.org/problem?id=1426

题意:找到一个只由0和1组成的数能把n整除;当初没读懂题,一直放了很久,最大位有20位,用无符号 __int64 正好20位;

思路:简单dfs,一个判断找到未找到开关k;因为由0 1组成,故dfs的方向为乘10和乘10加1;

笔记:无符号__int64的c输出为%I64u;用c++直接cout;题目很水,读题很重要,但这题拖了很久,还是记下来吧。

代码:

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 int k;
 5 void dfs(unsigned __int64 ans,int n,int index)
 6 {
 7     if(k==0)
 8     return;
 9     if(index==20)
10     return;
11     if(ans%n==0)
12     {
13         cout<<ans<<endl;
14         k=0;
15         return;
16     }
17     dfs(10*ans,n,index+1);
18     dfs(10*ans+1,n,index+1);
19 }
20 int main()
21 {
22     int n;
23     while(cin>>n&&n)
24     {
25         k=1;
26         dfs(1,n,1);
27     }
28     return 0;
29 }

猜你喜欢

转载自www.cnblogs.com/wwq-19990526/p/9152572.html