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

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

POJ 1426 Find The Multiple dfs or 暴力

2019-11-11 06:41:47
字體:
來源:轉載
供稿:網友
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; } 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灵璧县| 桦甸市| 绥德县| 翼城县| 静乐县| 浦江县| 淮安市| 石泉县| 常德市| 海阳市| 方山县| 青龙| 鱼台县| 海宁市| 马公市| 卓尼县| 鄂伦春自治旗| 辉南县| 明光市| 泗阳县| 济阳县| 鄢陵县| 英德市| 开阳县| 隆昌县| 龙游县| 东光县| 和政县| 奉新县| 东丽区| 云阳县| 兴城市| 扎囊县| 长治市| 邹平县| 潞城市| 图木舒克市| 长兴县| 安平县| 辛集市| 岑溪市|