日期:2014-05-20 浏览次数:21178 次
Description Given an infinite array of integers 2,3,.... Now do some operations on it. The operation is to choose a minimum number from the array which is never been chosen, then change the status of its multiples excluding itself, i.e remove the multiples of the chosen number if they are in the array , otherwise add it to the array.keep the order after change. For instance, the first step, choose number 2, change the status of 4, 6, 8, 10... They are all removed from the array. The second step, choose 3, change the status of 6, 9, 12, 15... Pay attention: 9 and 15 are removed from the array while 6 and 12 are added to the array. Input Every line contains an integer n. The zero value for n indicates the end of input. Output Print "yes" or "no" according whether n is in the array. Sample Input 2 30 90 0 Sample Output yes yes no Hint The number n never has a prime factor greater than 13000000, but n may be extremely large.
int num = int.Parse(Console.ReadLine()); bool[] a = new bool[num + 2]; for (int i = 0; i < num + 2; i++) { a[i] = true; } for (int i = 2; i < num + 2; i++) { if (a[i] == true) { for (int j = i + 1; j < num + 2; j++) { if (j % i == 0) { if (a[j] == false) a[j] = true; else a[j] = false; } } } } int n = int.Parse(Console.ReadLine()); Console.WriteLine("The result is " + a[n]);
------解决方案--------------------
List<uint> list = new List<uint>(); uint temp; string str; while (true) { Console.WriteLine("请输入一个状态数字<Q=Exit>:"); try { str = Console.ReadLine(); if (str == "Q" || str == "q") { break; } temp = uint.Parse(str); if (temp > 1) { list.Add(temp); } else { Console.WriteLine("必须为大于等于2的数字"); } } catch { Console.WriteLine("输入错误!请输入一个正整数"); } } bool state = false; while (true) { Console.WriteLine("请输入一个要查询的数字<Q=Exit>:"); try { str = Console.ReadLine(); if (str == "Q" || str == "q") { break; } temp = uint.Parse(str); if (temp > 1) { foreach (uint u in list) { if (temp % u == 0) { state = true; break; } else { state = false; } } if (state) { Console.WriteLine("已改变"); } else { Console.WriteLine("未改变"); } } else { Console.WriteLine("必须为大于等于2的数字"); } } catch { Console.WriteLine("输入错误!请输入一个正整数"); } }