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;
    }
    }
  • 相关阅读:
    [LUOGU] P3275 [SCOI2011]糖果
    [BZOJ] 2287: 【POJ Challenge】消失之物
    [BZOJ] 2131: 免费的馅饼
    [JZOJ] 5835. Prime
    [JZOJ] 5837.Omeed
    UF_CAMGEOM_ask_custom_points 封装缺陷
    NX Open 切削层加载
    NX Open 图层说
    c++ Dll调用
    VC操作Excel文件编程相关内容总结
  • 原文地址:https://www.cnblogs.com/nanke/p/2126650.html
Copyright © 2011-2022 走看看