求一組9位數且不重複(1-9組成),滿足前n位能被n整除,例如:取先二位能被2整除,取先三位能被3整除...取先九位能被9整除。

Code
            Stopwatch l_cc_stopWatch = new Stopwatch();
            l_cc_stopWatch.Start();

            Random rad 
= new Random();
            
while (true)
            {
                
string ts = string.Empty;
                
while (ts.Length < 9)
                {
                    
string tmp = rad.Next(110).ToString();
                    
if (ts.IndexOf(tmp) < 0)
                    {
                        ts 
+= tmp;  //得到9位不重複數
                    }
                }
                
bool flag = false;
                
for (int m = 2; m <= 9; m++)
                {
                    
if (Int32.Parse(ts.Substring(0, m)) % m == 0)  //前n位能被n整除
                    {
                        flag 
= true;
                    }
                    
else
                    {
                        flag 
= false;
                    }

                    
if (flag == false)
                    {
                        
break;
                    }
                }

                
if (flag)
                {
                    Console.WriteLine(ts);
                    
break;
                }
            }

            l_cc_stopWatch.Stop();
            Console.WriteLine(
"{0}:seconds", l_cc_stopWatch.Elapsed.TotalSeconds);

你可能感兴趣的:(求一組9位數且不重複(1-9組成),滿足前n位能被n整除,例如:取先二位能被2整除,取先三位能被3整除...取先九位能被9整除。)