国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 华蓥市| 喀喇沁旗| 姜堰市| 柳州市| 育儿| 射阳县| 象山县| 丹阳市| 科技| 神木县| 阜宁县| 兴义市| 阳高县| 黄山市| 六安市| 托克逊县| 赤壁市| 昭觉县| 那坡县| 揭阳市| 琼海市| 石林| 瓮安县| 太湖县| 耒阳市| 临安市| 红安县| 阜新| 巴彦淖尔市| 山西省| 嵊州市| 睢宁县| 新昌县| 托里县| 当涂县| 凤山县| 东辽县| 华池县| 东兰县| 延安市| 晋城|