读书人

竟然会超时WHY?解决方案

发布时间: 2012-02-09 18:22:27 作者: rapoo

竟然会超时,WHY?
题目:http://acm.timus.ru/problem.aspx?space=1&num=1209
这样写会超时?有些搞不明白了。。
大伙看看是什么原因?
也顺便说说有没有什么更好更快的方法。。

C# code
using System;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            int count = int.Parse(Console.ReadLine());            long[] num = new long[count];            for(int i=0;i<count;i++)                num[i] = long.Parse(Console.ReadLine());            string result = string.Empty;            DateTime dt1 = DateTime.Now;            foreach (long n in num)                result += ZeroOrOne(n) + " ";            Console.Write(result.Trim());        }        static int ZeroOrOne(long number)        {            double d = Math.Sqrt((number<<1) - 1.75) + 0.5;            if (d - (long)d == 0) return 1;            return 0;        }    }}


[解决办法]
友情帮顶……
[解决办法]
sf
帮顶;
回去再看看
[解决办法]
只有一个3楼。
[解决办法]
先帮顶
[解决办法]
好玩的题目,有空了试试

1, 10, 100, 1000...
Time Limit: 1.0 second
Memory Limit: 16 MB

Let's consider an infinite sequence of digits constructed of ascending powers of 10 written one after another. Here is the beginning of the sequence: 110100100010000… You are to find out what digit is located at the definite position of the sequence.
Input
There is the only positive integer number N in the first line, N < 65536. The i-th of N left lines contains the positive integer Ki — the number of position in the sequence. It's given that Ki < 231.
Output
You are to output N digits 0 or 1 separated with a space. More precisely, the i-th digit of output is to be equal to the Ki-th digit of described above sequence.



[解决办法]
哪个地方超时?

for(int i=0;i<count;i++)
num[i] = long.Parse(Console.ReadLine());
这里就很费时间的
其余不清楚
[解决办法]
探讨
引用:
只有一个3楼。


好诡异啊。。又成一个3楼了。。

[解决办法]
这样就OK了:(LZ可能是字符串操作太费时了)

C# code
using System;class Program{  static void Main()  {    int count = int.Parse(Console.ReadLine());    long[] num = new long[count];    for(int i=0; i < count; i++)    {      num[i] = long.Parse(Console.ReadLine());    }    foreach (long n in num)    {      Console.Write(ZeroOrOne(n));      Console.Write(" ");    }  }  static int ZeroOrOne(long number)  {    double d = Math.Sqrt((number<<1) - 1.75) + 0.5;    return d == (long)d ? 1 : 0;  }}
[解决办法]
好像输入的数字一大就超时。小数字没有问题
[解决办法]
这样更简单,没有必要开一个数组保存输入,边输入边输出就可以了:

C# code
using System;class Program{  static void Main()  {    int count = int.Parse(Console.ReadLine());    for(int i = 0; i < count; i++)    {      Console.Write(ZeroOrOne(long.Parse(Consoleguration::
------解决方案--------------------


没有必要用 long,用 uint 就够了:

C# code
using System;class Program{  static void Main()  {    int count = int.Parse(Console.ReadLine());    for(int i = 0; i < count; i++)    {      Console.Write(ZeroOrOne(uint.Parse(Console.ReadLine())));      Console.Write(" ");    }  }  static int ZeroOrOne(uint number)  {    double d = Math.Sqrt((number<<1) - 1.75) + 0.5;    return d == (uint)d ? 1 : 0;  }}
[解决办法]
看看
[解决办法]
探讨
输入费时?郁闷了。。


[解决办法]
没有必要用字符串相加,得到结果时立即输出好了。
[解决办法]
如果不想要最后一个空格,加上 if (i < count - 1) 就好了:

C# code
using System;class Program{  static void Main()  {    int count = int.Parse(Console.ReadLine());    for(int i = 0; i < count; i++)    {      Console.Write(ZeroOrOne(uint.Parse(Console.ReadLine())));      if (i < count - 1) Console.Write(" ");    }  }  static int ZeroOrOne(uint number)  {    double d = Math.Sqrt((number<<1) - 1.75) + 0.5;    return d == (uint)d ? 1 : 0;  }} 

读书人网 >C#

热点推荐