指定されたビット数を生成するエコー素数

12708 ワード

/*

 *       1   ,    1        ,      ;      ,       ,

 *        ,      ,         。  ,11,131      。

 *           ,                  。

 *   :

 * a、        ,         

 * b、            3 

 * c、            

 * d、      -1

 */



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



namespace huiwensushu

{

    class Program

    {

        private static int str1 = 0, str2 = 0, str3 = 0;

        private static int jeck = 0;

        static void Main(string[] args)

        {

            GetMain();

        }

        //      

        private static void GetMain()

        {

            int number = 0; string gets = null;

            do

            {

                Console.Write("         (    3):");

                number = Convert.ToInt32(Console.ReadLine());

                if (number < 3)

                {

                    Console.Write("    ,     :");

                    number = Convert.ToInt32(Console.ReadLine());

                }

                int result = GetResult(number);

                if (result != 0)

                    Console.WriteLine(result);

                else

                    Console.WriteLine("-1");

                Console.Write("    (Y/N):");

                gets = Console.ReadLine();

            } while (gets == "Y" || gets == "y");

        }

        //           

        private static int GetResult(int n)

        {            

            int getNum = 0;            

            List<int> lists = new List<int>();

            lists = GetSushu(n);

            for (int i = 0; i < lists.Count; i++)

            {

                if (IsHuiwen(Convert.ToInt32(lists[i])))

                {                    

                    getNum = lists[i];

                    //                   

                    if (jeck == 0)

                    {                        

                        str1 = getNum;

                        jeck = 1;

                        break;

                    }

                    else if (getNum == str1 || getNum == str2||getNum==str3)

                    {

                        continue;

                    }

                    else

                    {

                        if (jeck == 1)

                        {

                            str2 = getNum;

                            jeck = 2;

                        }

                        else if (jeck == 2)

                        {

                            str3 = getNum;

                            jeck = 3;

                        }

                        else

                        {

                            str1 = getNum;

                            jeck = 1;

                        }

                        break;

                    }         

                }

            }

            return getNum;

        }

        //  number    

        private static List<int> GetSushu(int number)

        {

            List<int> list = new List<int>();

            int num=Convert.ToInt32(Math.Pow(10, number))-1;

            int start=Convert.ToInt32(Math.Pow(10, number-1));

            //   start num     

            for (int i =start ; i <=num ; i++) 

            {

                bool flag = true;

                int k = Convert.ToInt32(Math.Sqrt(i));

                for (int j = 2; j <= k; j++)

                {

                    //  i  j   0,   flag False,         

                    if (i % j == 0)

                    {

                        flag = false;                        

                        break;         

                    }

                }

                if (flag == true)

                {

                    list.Add(i);

                }

            }

            return list;

        }

        //  number     

        private static bool IsHuiwen(int number)

        {

            string leng = number.ToString();

            bool a = true;

            for (int i = 0; i < leng.Length/2; i++)

            {

                int dd = Convert.ToInt32(leng.Substring(i, 1));

                int mm = Convert.ToInt32(leng.Substring(leng.Length-1 - i, 1));

                if (dd != mm)

                {

                    a = false;

                    break;

                }

            }

            return a;

        }

    }

}