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

首頁 > 學院 > 開發設計 > 正文

POJ 1426 Find The Multiple dfs or 暴力

2019-11-11 07:26:03
字體:
來源:轉載
供稿:網友
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; } 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 榆中县| 苏州市| 夏津县| 彭山县| 双桥区| 繁峙县| 宁乡县| 吉隆县| 察雅县| 廉江市| 武宣县| 濮阳市| 台前县| 长兴县| 扎鲁特旗| 织金县| 山东省| 扎兰屯市| 天门市| 肃南| 韶关市| 陆川县| 南溪县| 永福县| 八宿县| 大竹县| 河池市| 滨州市| 响水县| 普兰店市| 郑州市| 宽城| 巴里| 武威市| 镇坪县| 乐昌市| 安龙县| 名山县| 公安县| 日土县| 广南县|