首页 > 学院 > 开发设计 > 正文

POJ 1426 Find The Multiple dfs or 暴力

2019-11-14 09:14:56
字体:
来源:转载
供稿:网友
Given a positive integer n, write a PRogram to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.InputThe input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.OutputFor each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.Sample Input
26190Sample Output
10100100100100100100111111111111111111
题意:
给出一个整数n,(1 <= n <= 200)。求出任意一个它的倍数m,要求m必须只由十进制的'0'或'1'组成。
思路:
要不是放在这个搜索专题里我不会用搜索去解的,我肯定会暴力去解的;其实搜索也是一种枚举啊,
我这里用了两种方法;
dfs:
每个数都会有答案,因为都必须是0 1 组成的十进制,我们就每次去dfs他们的十倍和十倍+1;
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int flag;void dfs(unsigned long long m,int x,int k){	if(flag||k>=19)//这里的flag标志位,找到了直接回溯返回	return ;	if(m%x==0)	{		flag=1;		printf("%I64u/n",m);		return ;	}	dfs(m*10,x,k+1);	dfs(m*10+1,x,k+1);	return ;	}int main(){	int n;	while(cin>>n&&n)	{		flag=0;		dfs(1,n,0);	}	return 0; } 
暴力:
将每个数存数组,然后根据存放位置的奇偶来决定是否+1,然后直到能整除!
#include<cstdio>#include<iostream>#define ll long long #define N 6*100010using namespace std;ll a[N];int n;int main(){	int i;	while(cin>>n&&n)	{		a[1]=1;//初始设置为1		int flag=0;		for(i=1;i<=N;i++)		{			a[i]=a[i/2]*10+i%2;//写几个数找一个规律,			if(a[i]%n==0)			{				printf("%lld/n",a[i]);				flag=1;				break;			}		}	}	return 0; } 

上一篇:cron表达式详解

下一篇:Unity 单例模式

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表