How to find the smallest number with just 0 and 1 which is divided by a given number?

后端 未结 11 1343
情话喂你
情话喂你 2020-11-29 22:44

Every positive integer divide some number whose representation (base 10) contains only zeroes and ones.

One can prove that:

Consider the numbers 1, 11, 111,

11条回答
  •  一向
    一向 (楼主)
    2020-11-29 23:25

    Here is a C# solution using linked list

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            public static void print(LinkedList lst)
            {
                foreach(int i in lst)
                {
                    Console.Write(i);
                }
            }
    
            static void Main(string[] args)
            {
                int number = Convert.ToInt32(Console.ReadLine());
                int product;
                LinkedList list = new LinkedList();
                bool Istrue = true;
                int range = 1;
    
                while (range <= 10) { 
                    Istrue = true;
                    product = number * range;
                    while (product > 0)
                    {
                        list.AddFirst(product % 10);
                        product /= 10;
    
                    }
    
                    foreach (int i in list)
                    {
                        if (i > 1) Istrue = false;
                    }
    
                    if (Istrue) { print(list); break; }
                    else
                    {
                        list.Clear();   
                    }
    
                    range++;    
                }
    
                Console.WriteLine("Done");
    
                string s = Console.ReadLine();
            }
        }
    }
    

提交回复
热议问题