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

    看来没有选好数据类型,毕竟这道题目的数值要求挺高的

    我的代码没有通过,超时了

    说说中间遇到什么问题吧

    tem.list[tem.count]='0'+i;
    strcpy(ans.list,tem.list);

    这个代码花了我好几个小时的时间,一开始是直接ans.list[tem.count]='0'+i忽略了ans中tem.count之前的字符

    虽说超时了,但应该是对的(因为看了人家的代码)

    #include "iostream"
    #include "queue"
    #include "algorithm"
    #include "string.h"
    using namespace std;
    struct Point{
      int num,count;
      char list[100];
    };
    int main(){
      int n,i,count,data[100],j,flag;
      while(cin>>n&&n){
        Point pt,ans;
        data[1]=1;
        for(i=2;i<=100;i++){
          data[i]=data[i-1]*10%n;
        }
        pt.num=0;pt.count=1;
        queue<Point> q;
        q.push(pt);
        while(!q.empty()){
          Point tem=q.front();
          q.pop();
          for(i=0;i<=1;i++){
            flag=0;
            ans.num=(tem.num+i*data[tem.count])%n;
            tem.list[tem.count]='0'+i;
            strcpy(ans.list,tem.list);
            ans.count=tem.count+1;
            for(j=1;j<ans.count;j++){
              if(ans.list[j]=='1')flag=1;
            }
            if(ans.num%n==0&&flag==1)goto l1;
            q.push(ans);
          }
        }
       l1:for(i=ans.count-1;i>=1;i--)cout<<ans.list[i];cout<<endl;
      }
    }

    人家的代码long long 

    #include <iostream>
    #include <queue>
    
    using namespace std;
    int n;
    long long  bfs()
    {
        queue<long long>q;
        while(!q.empty())
        {
            q.pop();
        }
        q.push(1);
        while(1)
        {
            long long  p=q.front();
            if(p%n==0)
            return p;
            q.pop();
            q.push(p*10);
            q.push(p*10+1);
        }
    }
    int main()
    {
        while(cin>>n && n)
        {
            long long res=bfs();
            cout<<res<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Redhat 7使用CentOS 7的Yum网络源
    指定YUM安装包的体系结构或版本
    CURL常用命令
    VIM技巧之去除代码行号并缩进代码
    VIM 中鼠标选择不选中行号
    linux服务器性能优化
    阻塞,非阻塞,同步,异步
    WEB三层架构与MVC
    mvc与三层结构
    Centos环境下Tomcat启动缓慢
  • 原文地址:https://www.cnblogs.com/dowson/p/3346340.html
Copyright © 2011-2022 走看看