日期:2014-05-20  浏览次数:21092 次

【C# 每日一题8】一道纠结的问题
C# code

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.


解释一下:
一个非常大的从2开始的数组,里面有2 3 4 5 ....
之后从2开始对在这个数组中的数据i做操作,将所有i的倍数(2倍和2倍以上)的数据做状态的改变(在数组中或不在数组中)
例如:
第一次操作 2 则改变 4 6 8 10 12 ...的状态
第二次操作 3 则改变 6 9 12 15 18 ...的状态
第三次操作 5 则改变 10 15 20 25 ...的状态(因为4在第一次操作的时候被移除了就不对他操作)
第四次操作 6 则改变 12 18 24 30 ...的状态(因为6在2 3 两次操作,又被放到数组中了)
.......

之后输入一个数据n,看经过这么多次得变化之后,n是否还在这个数组中!
在的话输出'yes'否则输出'no'

这个题以前纠结了好久,因为有好多的测试用例,所以我觉得应该先打表,之后会发生memory or time 的限制。。。。。

看看大家有没有什么好的想法???



------解决方案--------------------
这个只需要计算就可以了 无需提前处理..
输入一个数字,只判断这个数字 在经过 2 3 5 这样的状态改变后 是否仍然有效.

难道是求公约数? 如果一个数字 N 求是否改变状态 只需知道 N的公约数有哪些.

经过 2 3 5这样的状态改变后, 这样的数字中是否有N的公约数 就知道N的状态是否改变了

计算一下不就可以了? 其实话说回来 就是个公约数的问题啊~~~
------解决方案--------------------
省略号表示多少次变化??
------解决方案--------------------
C# code
            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]);

------解决方案--------------------
C# code

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("输入错误!请输入一个正整数");
        }
    }