首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

2012-03-02 
【C# 每日一题8】一道纠结的问题C# codeDescriptionGiven an infinite array of integers 2,3,.... Now do s

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

C# code
DescriptionGiven 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.InputEvery line contains an integer n. The zero value for n indicates the end of input.OutputPrint "yes" or "no" according whether n is in the array.Sample Input230900Sample OutputyesyesnoHintThe 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("输入错误!请输入一个正整数");        }    } 


[解决办法]
枚举n所有公约数,存于数组,先剔除所有质数的乘方数(只能被改变一次),统计能存在的公约数个数,为奇则此数不存在,为偶则存在,(判断公约数是否存在:质公约数必然存在,和数公约数则对其重复此过程)
[解决办法]
修改了下,效率好很多了~

C# code
            int num = int.Parse(Console.ReadLine());            bool[] a = new bool[num];            for (int i = 0; i < num; i++)            {                a[i] = true;            }            for (int i = 2; i < num; i++)            {                if (a[i] == true)                {                    int j = 2 * i;                    while (j < num)                    {                        a[j] = !a[j];                        j += i;                    }                }            }            int n = int.Parse(Console.ReadLine());            Console.WriteLine("The result is " + a[n]);
[解决办法]
探讨
枚举n所有公约数,存于数组,先剔除所有质数的乘方数(只能被改变一次),统计能存在的公约数个数,为奇则此数不存在,为偶则存在,(判断公约数是否存在:质公约数必然存在,和数公约数则对其重复此过程)

[解决办法]
探讨

引用:
枚举n所有公约数,存于数组,先剔除所有质数的乘方数(只能被改变一次),统计能存在的公约数个数,为奇则此数不存在,为偶则存在,(判断公约数是否存在:质公约数必然存在,和数公约数则对其重复此过程)

百度到的:在数论中有种,把一个数分解成N个素数的积,再把这些素数的指数加一后,全部相乘的积就是约数的个数了.
例如:36 = 2^2 * 3^2 指……

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

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

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

计算一下不就可以了? 其实话说回来 就是个公约数的问题啊~~~

[解决办法]
打表 解决时间问题
用bit储存每个数字状态,解决内存问题
[解决办法]
探讨
引用:
这个只需要计算就可以了 无需提前处理..
输入一个数字,只判断这个数字 在经过 2 3 5 这样的状态改变后 是否仍然有效.

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

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

计算一下不就可以了? 其实话说回来 就是……

[解决办法]
探讨

引用:

引用:
枚举n所有公约数,存于数组,先剔除所有质数的乘方数(只能被改变一次),统计能存在的公约数个数,为奇则此数不存在,为偶则存在,(判断公约数是否存在:质公约数必然存在,和数公约数则对其重复此过程)

百度到的:在数论中有种,把一个数分解成N个素数的积,再把这些素数的指数加一后,全部相乘的积……

[解决办法]
打表好像也不现实
The number n never has a prime factor greater than 13000000, but n may be extremely large.
几个小于13000000大质数一乘, N也是非常非常大。
[解决办法]
结贴吧,你这贴和5一样,纯数学想法过多,真正代码算法没啥技术含量,所以才不加推荐=,=
C# code
            Console.WriteLine("Please input total numbers!");            long num = long.Parse(Console.ReadLine());            Console.WriteLine("Please input the number you want to find!");            long n = long.Parse(Console.ReadLine());            if (num < n)            {                Console.WriteLine("No!");                return;            }            long i = 2;            while (i * i <= n)            {                if (n % (i * i) == 0)                {                    Console.WriteLine("No!");                    return;                }                i++;            }            Console.WriteLine("Yes!");
------解决方案--------------------


探讨

打表好像也不现实
The number n never has a prime factor greater than 13000000, but n may be extremely large.
几个小于13000000大质数一乘, N也是非常非常大。

[解决办法]
13000000以内不到1百万个质数,一个质数表的内存占用为4兆。
程序以后再做解释:
C# code
class Solver{    static List<int> Primes = new List<int>();    static Solver()    {        bool[] masks = new bool[13000000];        for(int i=0; i<masks.Length; i++) masks[i] = true;                for (int i = 2; i < masks.Length; i++)        {            if (masks[i])            {                Primes.Add(i);                for (int j = i + i; j < masks.Length; j += i)                {                    masks[j] = false;                }            }        }    }    public static bool OnOffState(long i)  //<---    {        foreach (int prime in Primes)        {            if (i % prime == 0)            {                i /= prime;                if (i % prime == 0) return false;            }            if (i < prime * prime) break;        }        return true;    }}
[解决办法]
探讨

引用:

13000000以内不到1百万个质数,一个质数表的内存占用为4兆。
程序以后再做解释:
C# code

class Solver
{
static List<int> Primes = new List<int>();
static Solver()
{
bool[] masks = new bool[13000000];
fo……

并非13000000以内的数据,是数据的最大质因子小于13000000!!!!

[解决办法]
探讨

13000000以内不到1百万个质数,一个质数表的内存占用为4兆。
程序以后再做解释:
C# code

class Solver
{
static List<int> Primes = new List<int>();
static Solver()
{
bool[] masks = new bool[13000000];
for(int i=0; i<masks.Length; i++) masks[i] = true;

for (int i = 2; i < masks.Length; i++)
{
if (masks[i])
{
Primes.Add(i);
for (int j = i + i; j < masks.Length; j += i)
{

[解决办法]
朋友,我主要是对下面这句不是很明白。

 if (i % prime == 0)
{
i /= prime;
if (i % prime == 0) return false;

}

主要是对第二个if语句搞不太明白,我曾经以为找到很多的例子来推翻你,可最后我都错了。这一句可能是个数学上的问题...
[解决办法]
探讨
13000000以内不到1百万个质数,一个质数表的内存占用为4兆。
程序以后再做解释:

C# code

class Solver
{
static List<int> Primes = new List<int>();
static Solver()
{
bool[] masks = new bool[13000000];
……

[解决办法]
探讨

朋友,我主要是对下面这句不是很明白。

if (i % prime == 0)
{
i /= prime;
if (i % prime == 0) return false;

}

主要是对第二个if语句搞不太明白,我曾经以为找到很多的例子来推翻你,可最后我都……

[解决办法]
探讨
13000000以内不到1百万个质数,一个质数表的内存占用为4兆。
程序以后再做解释:
……

热点排行