poj 1426 Find The Multiple(DFS或打表)

大概题意就是找出一个1~200间的一个数字n,找出一个只由1,0组成的数m,使m被n整除。

第一种 思路是dfs,搜到一种满足题意的答案就ok了。

用的时间比较多。

用unsigned long long 解决了大数问题。

代码:

#include<stdio.h>

int n;
bool flag;

void dfs(unsigned long long a,int dep)
{
    if(flag||dep==19)
        return ;
    if(a%n==0)
    {
        flag=1;
        printf("%I64u\n",a);
        return ;
    }
    else
    {
        dfs(a*10,dep+1);
        dfs(a*10+1,dep+1);
    }
    return ;
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        flag=0;
        dfs(1,0);
    }
    return 0;
}

第二种思路是打表,不用考虑大数问题:

因为数据比较少,所以考虑把所有数都打出来。

首先打表的代码:

#include<stdio.h>

int n;
bool flag;
unsigned long long max;

FILE *fout = fopen ("num.txt", "w");

void dfs(unsigned long long a,int dep)
{
    if(flag||dep==19)
        return ;
    if(a%n==0)
    {
        flag=1;
        fprintf(fout,"\"%I64u\",",a); //打出两百个数的表
        if(a>max)
            max=a;
        return ;
    }
    else
    {
        dfs(a*10,dep+1);
        dfs(a*10+1,dep+1);
    }
    return ;
}

int main()
{
    max=0;
    for(n=1; n<=200; n++)
    {
        flag=0;
        dfs(1,0);
    }
    fprintf(fout,"\n%I64u",max);//寻找最大一个数有多少位
    return 0;
}

0s杀的代码:

#include<stdio.h>

char num[][20]={"1","10","1000000000000000011","100","10","1000000000000000110","1000000000000000111","1000","1000000000011111111","10","100000000000000001","1000000000000001100","1000000000000001","10000000000000010","1000000000000000110","10000","10000000000000101","1000000000111111110","1000000000000001101","100","10000000000000101","1000000000000000010","1000000000000010111","1000000000000011000","100","10000000000000010","1000000000101111111","100000000000000100","1000000000000000111","1000000000000000110","1000000000000110011","100000","1000000000000101111","100000000000001010","10000000000000010","1000000001111111100","100000000000000011","10000000000000110","10000000000000101","1000","1000000000000010111","100000000000001010","1000000000000010101","100000000000000100","1000000000111111110","100000000000001110","1000000000000101","1000000000000110000","1000000000000011101","100","10000000000000101","100000000000000100","1000000000000000011","1000000001101111110","1000000000000000010","1000000000000001000","1000000000000011","1000000000000001010","10000000000000011","1000000000000001100","1000000000000011","100000000001110010","1000000001011101111","1000000","10000000000000010","1000000000000111110","100000000000011","1000000000000010100","100000000000011","10000000000000010","1000000000000100101","1000000011111111000","1000000000001","1000000000000000110","1000000000000001100","100000000000001100","1000000000000001","100000000000001010","100000000000110001","10000","1000000000111111101","1000000000000110110","100000000000001101","1000000000000010100","100000000000001010","100000000000010010","100000000000000101","1000000000000001000","1000000000000011011","1000000000111111110","1000000000000001","1000000000000011100","1000000000001111001","10000000000001010","10000000000000110","1000000000001100000","100000000001111","1000000000000111110","1101111111111111111","100","1000000000000000001","100000000000001010","100000000000000001","1000000000000001000","100000000000001010","100000000010000110","10000000000001001","1000000001111111100","1000000000001000011","1000000000000000010","100000000000000011","100000000000110000","1000000000000111101","10000000000000110","100000000000001110","10000000000000100","1000000001101111101","100000000000000110","10000000000000101","1000000000000011000","1000000000000101111","10000000000000110","1000000001100011001","1000000000011100100","1000","1000000001111011110","1000000000000110101","10000000","10000000000001001","10000000000000010","10000000001101","1000000000010111100","1000000000001010001","1000000000000110","1000000001101111110","100000000000111000","1000000000001","1000000000000110","1000000000001001111","100000000000000100","1000000000000101","1000000000011110","1000000000000001","1000000111111110000","1000000000000001010","10000000000010","100000000000011111","1000000000000010100","10000000000001111","1000000000000001100","1000000000010111001","1000000000000011000","1000000000111111101","10000000000000010","100000000001110010","1000000000000010100","1000000000000101","1000000000001100010","1000000000000000011","100000","1000000000001101001","100000000111111110","1000000000011011101","1000000000001110100","1000000000000111110","1000000000000011010","1000000000001001101","10000000000011000","1000000000001100111","100000000000001010","1000000000111111011","1000000000000100100","1000000000000010111","1000000000000001010","100000000000000100","100000000000010000","10000000000000011","100000000000001110","1000000000000100111","1000000001111111100","10000000000000111","10000000000000010","1000000000000011","100000000000011000","1000000000000000110","10000000001101110","100000000001001001","100000000000010100","1000000010111010111","10000000000000110","1000000000011111101","1000000000011000000","1000000010000100001","1000000000011110","100000000000001010","100000000010010100","100000000000111001","1111111111111111110","1000000000101111001","1000"};

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        printf("%s\n",num[n-1]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值