zoukankan      html  css  js  c++  java
  • pku 1426 Find The Multiple

    额,自己的代码比较水,用普通的方法做的,内存开得太大了

    View Code
    #include"stdio.h"
    #define maxn 11000000
    __int64 q[maxn];
    int n;
    void bfs()
    {
    int i,j;
    __int64 x;
    q[
    0]=1;
    i
    =0;j=1;
    while(i<j)
    {
    x
    =q[i];
    if(x%n==0)
    {
    printf(
    "%I64d\n",x);
    break;
    }
    x
    *=10;
    q[j
    ++]=x;
    q[j
    ++]=x+1;
    i
    ++;
    }
    }

    int main()
    {
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
    bfs();
    }
    return 0;
    }

    看下大牛的代码,做了预处理,中间还加了剪枝,BFS

    View Code
    #include <iostream>
    #include
    <queue>
    #include
    <string>
    #include
    <CSTDIO>
    using namespace std;
    string ans[210];
    bool mark[210];
    struct node
    {
    string ans;
    int mod;
    };
    string BFS(int n)
    {
    //memset(mark,0,sizeof(mark));
    queue<node>q;
    node s;
    s.ans
    ="1";
    s.mod
    =1;
    q.push(s);
    mark[
    1]=true;
    while(!q.empty())
    {
    node now
    =q.front(),temp=now;
    q.pop();
    int c=(now.mod*10+1)%n;
    int d=(now.mod*10)%n;
    if(d==0)
    {
    temp.ans
    +="0";
    return temp.ans;
    }
    if(c==0)
    {
    temp.ans
    +="1";
    return temp.ans;
    }
    if(!mark[d])//剪枝
    {
    mark[d]
    =1;
    temp.ans
    +="0";
    temp.mod
    =d;
    q.push(temp);
    }
    if(!mark[c])
    {
    mark[c]
    =1;
    now.ans
    +="1";
    now.mod
    =c;
    q.push(now);
    }
    }
    }
    int main()
    {
    int n;
    for(int i=1;i<=5;i++)
    {
    if(i&1)
    ans[i]
    =BFS(i);
    else
    ans[i]
    =ans[(i>>1)]+"0";//这步处理比较妙,因为这是special judge,只要是它的倍数即可
    }
    while (scanf("%d",&n)&&n)
    {
    cout
    <<ans[n]<<endl;
    }
    }
  • 相关阅读:
    C- c常见问题分析
    LEETCODE
    MPI之求和
    在VS2010配置MPI--win7下64位系统
    OpenMP之枚举排序
    OpenMP之数值积分(求圆周率Pi)(sections)
    OpenMP之求和(用section分块完成)
    64位WIN7下安装MPICH2
    Ubuntu下eclipse开发hadoop应用程序环境配置
    C语言字符串函数例子程序大全 – string相关
  • 原文地址:https://www.cnblogs.com/nanke/p/2126650.html
Copyright © 2011-2022 走看看