国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

POJ 1426 Find The Multiple dfs or 暴力

2019-11-11 07:48:21
字體:
供稿:網(wǎng)友
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
題意:
給出一個(gè)整數(shù)n,(1 <= n <= 200)。求出任意一個(gè)它的倍數(shù)m,要求m必須只由十進(jìn)制的'0'或'1'組成。
思路:
要不是放在這個(gè)搜索專題里我不會(huì)用搜索去解的,我肯定會(huì)暴力去解的;其實(shí)搜索也是一種枚舉啊,
我這里用了兩種方法;
dfs:
每個(gè)數(shù)都會(huì)有答案,因?yàn)槎急仨毷? 1 組成的十進(jìn)制,我們就每次去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標(biāo)志位,找到了直接回溯返回	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; } 
暴力:
將每個(gè)數(shù)存數(shù)組,然后根據(jù)存放位置的奇偶來決定是否+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;//初始設(shè)置為1		int flag=0;		for(i=1;i<=N;i++)		{			a[i]=a[i/2]*10+i%2;//寫幾個(gè)數(shù)找一個(gè)規(guī)律,			if(a[i]%n==0)			{				printf("%lld/n",a[i]);				flag=1;				break;			}		}	}	return 0; } 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 双峰县| 突泉县| 尚志市| 长沙县| 临江市| 科技| 汕头市| 抚松县| 穆棱市| 临洮县| 通榆县| 宽城| 深泽县| 东光县| 大化| 长宁县| 临桂县| 株洲县| 泽库县| 瓮安县| 商水县| 交口县| 葵青区| 庄河市| 兴业县| 布拖县| 太保市| 通渭县| 涿州市| 永春县| 竹山县| 阿拉善右旗| 苏州市| 清河县| 木里| 襄城县| 江达县| 磐安县| 安龙县| 乌兰察布市| 仁怀市|