Find The Multiple(数学+搜索)

来源:互联网 发布:淘宝秒杀器官方下载 编辑:程序博客网 时间:2024/06/05 02:18
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.
Input
The 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.
Output
For 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
26190
Sample Output
10100100100100100100

111111111111111111

思路1 dfs :因为结果是n的倍数,所以必须有一个因子x,x*n = m;那么模拟乘法,留下最后一位,只不过最后一位只能是0或1,维护剩余的进位dat,当dat = 1 时,乘法就算完了。具体就是先对n处理一下,把后面的0去掉,并存起来。 for(i : 0 - 9) ,如果n*i+dat个位是 1 或 0,满足题意, 那么dat/=10,并且记录一下个位的值。例如:n = 6,则第一步:dat = 0, dat+ n*5 末尾是0 更新 dat = 3保留 ans【k++】 = 0,第二步,dat= 3 dat+n*3末位是1,更新dat = 2 第三步回到上个例子;注意要剪枝,dat不可重复出现不然死循环。

#include <cstdio>#include <queue>#include <map>#include <climits>#include <cstring>#include <iostream>using namespace std;int ans[150];//记录最终结果map <int,int> a; // 标记dat是否有过.int n;bool ok;void dfs(int dat,int k){    if(ok || k > 50) return; // K不要取到100,可能会超内存,本来想分梯度,但是改了下50,竟然就过了    if(dat == 1)    {        ans[k] = dat;        for(int i = k; i >=0; i--)            printf("%d",ans[i]);        printf("\n");        ok = 1;        return;    }    for(int i = 0; i <= 9; i++)    {        if(((dat+n*i) % 10 ) / 2 == 0)        {            int tmp;            ans[k] = (dat+n*i) % 10;            tmp = (dat+n*i)/10;            if(a[tmp]) continue;            a[tmp] = 1;            dfs(tmp,k+1);            a[tmp] = 0;        }    }}int main(){    while(scanf("%d",&n)&&n)    {        a.clear();        a[0] = 1,ok=0;        int k = 0;        while((n%10) == 0)        {            ans[k++] = 0;            n/=10;        }        if(n == 1)        {            ans[k] = 1;            for(int i = k; i >=0; i--)                printf("%d",ans[i]);            printf("\n");            continue;        }        dfs(0,k);    }    return 0;}


后来见了个代码,直接bfs n 多花点时间应该也能过,试了试真过了,真是蒙了。

#include<cstdio>#include<vector>using namespace std;int find;void dfs(unsigned long long t,int n,int k){       if(find)return;       if(t%n==0)       {            printf("%I64u\n",t);            find=1;            return;       }       if(k==19)   return ;       dfs(t*10,n,k+1);       dfs(t*10+1,n,k+1);}int main(){    int n;    while(scanf("%d",&n),n)    {         find=0;         dfs(1,n,0);    }    return 0;}



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 找不到自己在哪个平台借过钱怎么办 九游账号绑定手机之前绑定的怎么办 九游充过钱的游戏忘了游戏名怎么办 百度网盘密码忘了申诉不了怎么办 手机号被别人注册了百度账号怎么办 快手被盗找回时出来重置密码怎么办 魅族账号密码和密保都忘记了怎么办 vivo账号的密保问题忘了怎么办 oppo账号密保问题忘了怎么办 小米手机刷了机忘了账号密码怎么办 忘了小米账号的密码是多少怎么办 千牛账号在手机上被限制登录怎么办 违规的千牛账号被限制登录了怎么办 苹果id和锁屏密码忘记了怎么办 感应门的编程密码忘记了怎么办 交易猫买的号被找回了怎么办 uc上我的小说看不了怎么办 微信零钱忘记密码没有银行卡怎么办 九游平台冻结提不了现怎么办 计算机考试报名登录名忘记了怎么办 云顶扑克提现怎么提不出来怎么办 微信正在下载一直0kb怎么办 守望先锋运行时出现意外错误怎么办 信用卡暂停使用怎么办还能恢复吗 新刷乳胶漆墙面一碰一个坑怎么办 夏天开空调冻着了头疼打喷嚏怎么办 桑蚕丝衣服被沐浴露退了色怎么办 空间被别人知道了密码登录了怎么办 三星手机显示解析包出现问题怎么办 三星手机下载解析包出现问题怎么办 两万的流动大棚给整坏了怎么办 劲舞团抽奖领了一样的衣服怎么办 win系统ps界面字体太小怎么办 任何网页都变成监控登录界面怎么办 微信启动录音的尝试被拒绝怎么办 微信传到电脑的文件打不开怎么办 转转网账号出租时遇到防沉迷怎么办 如果买了王者号结果有防沉迷怎么办 苹果手机一直显示验证失败怎么办呢 安装包证书异常导致安装失败怎么办 qq飞车忘了几区的怎么办