Find the Multiple

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.
 

这道题的大意是这样的:找到一个由0和1组成的十进制数字,使得这个数是所输入的数的倍数。

这道题呢其实是在吓人,它说是一百位,但实际上开个long long 就可以过了,看的就是你敢不敢去是。

知道题就是一个深搜的题,因为这个是0 1 串,所以我们从1开始搜,往10*k和10*k+1方向搜,保证能搜到全部 01 串,但是我们也要有个边界条件,那就是到了20位或者已经搜到了符合条件的数,那么我们就跳出。

#include<iostream> 
using namespace std;
int b;
unsigned long long ans;
bool f;
void search(unsigned long long a,int s){
	if (f) return;
	if (s >= 19) return;
	if(a%b==0){
		ans = a;
		f = 1;
		return;
	}
	else {
		for(int i=0;i<2;i++){//两个方向乘10加1或不加1
			search(a*10+i,s+1);
		}
	}
} 
int main(){
	while(cin>>b&&b!=0){
	search(1,0);
	cout<<ans<<endl;
	f=0;
	ans=0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值