zoukankan      html  css  js  c++  java
  • Find The Multiple(DFS)

    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 (0) 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 2 6 19 0

    Sample Output

    10

    100100100100100100

    111111111111111111

    个人心得:这题开始没看懂,原来是求所求数的倍数,但这个倍数只由0,1组成,有俩种版本,一个是longlong直接过,这个在数据较多的时候不容易过,

    先说下longlong吧,就是从1开始深搜,有2种方向,一是乘以10,二是乘以10再加1,这个简单深搜还是比较好实现的。

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<cmath>
     5 #include<queue>
     6 #include<algorithm>
     7 using namespace std;
     8 int n,flag;
     9 unsigned long long ans;
    10 void dfs(unsigned long long x,int y)
    11 {
    12   if(y>19) return ;
    13     if(x%n==0)
    14     {
    15         flag=1;
    16         ans=x;
    17         return;
    18 
    19     }
    20     else
    21       {for(int i=0;i<2;i++)
    22         dfs(10*x+i,y+1);
    23       }
    24         if(flag) return;
    25 
    26 
    27 }
    28 int main()
    29 {
    30   unsigned long long x;
    31   while(cin>>n)
    32   {
    33       if(n==0) break;
    34       x=1;
    35       flag=0;
    36       dfs(x,0);
    37       cout<<ans<<endl;
    38 
    39 
    40   }
    41   return 0;
    42 
    43 }

    再看一下数组存储的代码,他这个涉及到的知识点真的不知道,每次得到新数都进行取余再将这个数放入数组里,这样就不会数据溢出了,一开始想这么做怕担心

    数据会溢出。

     1 #include <iostream>
     2 
     3  
     4 
     5 using namespace std;
     6 
     7  
     8 
     9 int n, t, a[100], ac,l;
    10 
    11  
    12 
    13 void dfs(int c, int s)
    14 
    15 {
    16 
    17          if(s==0)//如果余数为0,择停止递归
    18 
    19          {
    20 
    21                    ac = 1;//表示已经找到结果
    22 
    23                    l=c;//给数组的长度赋值
    24 
    25          }
    26 
    27          else if(c < 100)//在深度100范围内进行递归
    28 
    29          {
    30 
    31                    if(ac==0)//还没找到结果,进行递归运算
    32 
    33                    {
    34 
    35                             a[c] = 1;//尝试下一位放1
    36 
    37                             dfs(c + 1, (s * 10 + 1) % n);
    38 
    39                    }
    40 
    41                    if(ac==0)
    42 
    43                    {
    44 
    45                             a[c] = 0;
    46 
    47                             dfs(c + 1, (s * 10) % n);
    48 
    49                    }
    50 
    51          }
    52 
    53 }
    54 
    55  
    56 
    57 int main()
    58 
    59 {
    60 
    61          while(cin>>n,n)
    62 
    63          {
    64 
    65                    a[0] = 1, ac = 0;//初始化
    66 
    67                    dfs(1, 1);
    68 
    69                    for(int i=0;i<l;i++) cout<<a[i];//根据递归得到的结果,进行输出
    70 
    71                    cout<<endl;
    72 
    73          }
    74 
    75          return 0;
    76 
    77 }
    78 
    79  
  • 相关阅读:
    背景不动,内容滚动的解决方案(移动端)
    移动端真实1px的实现方法
    用户模板和用户场景
    构建之法阅读笔记02
    学习进度六
    NABCD
    构建之法阅读笔记01
    学习进度五
    梦断代码阅读笔记03
    地铁系统
  • 原文地址:https://www.cnblogs.com/blvt/p/7256198.html
Copyright © 2011-2022 走看看