首页 > 代码库 > 1015. Reversible Primes (20) C#实现

1015. Reversible Primes (20) C#实现

 1 static void Main(string[] args) 2         { 3             string[] str; 4             bool FLAG; 5             int n = 0, d = 0; 6             List<int> N = new List<int>(); 7             List<int> D = new List<int>(); 8  9             do10             {11                 FLAG = true;12                 str = Console.ReadLine().Split(new Char[] {   });13                 n = Convert.ToInt32(str[0]);14 15                 if (n < 0)16                 {17                     FLAG = false;18                 }19                 else if (n > 100000)20                 {21                     Console.WriteLine("N>100000,请重输:");22                 }23                 else if (Convert.ToInt32(str[1]) <= 1 || Convert.ToInt32(str[1]) > 10)24                 {25                     Console.WriteLine("D值不在范围内,请重输:");26                 }27                 else28                 {29                     N.Add(Convert.ToInt32(str[0]));30                     D.Add(Convert.ToInt32(str[1]));31 32                 }33             } while (FLAG == true);34 35             for (int j = 0; j < D.Count; j++)36             {37                 if (Isprime(N[j]) && Isprime(Reverse(N[j], D[j])))38                 {39                     Console.WriteLine("YES");40                 }41                 else42                 {43                     Console.WriteLine("NO");44                 }45             }46             Console.ReadKey();47 48         }49 50         public static int Reverse(int a, int d)51         {52             int sum = 0;53             do54             {55                 sum = sum * d + a % d;56                 a = a / d;57             } while (a != 0);58 59             return sum;60         }61 62         public static bool Isprime(int i)63         {64             if (i == 0 || i == 1)65                 return false;66             for (int p = 2; p < i / 2; p++)67                 if (i % p == 0)68                     return false;69 70             return true;71 72         }

 

1015. Reversible Primes (20) C#实现