题目:http://acm.timus.ru/problem.aspx?space=1&num=1209
这样写会超时?有些搞不明白了。。
大伙看看是什么原因?
也顺便说说有没有什么更好更快的方法。。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;
} }
}
这样写会超时?有些搞不明白了。。
大伙看看是什么原因?
也顺便说说有没有什么更好更快的方法。。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;
} }
}
解决方案 »
- 如何在form窗体中画矩形????
- 初学者问题之七:C#中如何控制一个WinForm只能有一个实例
- winform panel动态添加控件,内容超过不显示滚动条
- 关于C# 编写的activeX错误,紧急求救!
- 在C#中,窗口的隐藏和显示
- C# winform里,在主窗体里播放flash,主窗体里有个添加按钮,点击添加会弹出子窗体,在子窗体里浏览添加flash,同时确定,也就是添加的过程是在子窗体里完成,完成后子窗体关闭,当子窗体关闭时怎样让刚才添加的flash在主窗体里显示呢?
- md5高级算法
- C#序列化问题
- datatable.select()排序问题
- 向.NET高手请教一个问题
- DataGridView 里面单元格被双击的时候里面呈现可编辑状态.先不想让之成为可编辑状态.该怎么做?
- .net用户控件
有一无穷位的数:110100100010000100000.......
求第n位是1还是0?(n<2^31)
Time Limit: 1.0 second
Memory Limit: 16 MBLet'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.
num[i] = long.Parse(Console.ReadLine());
这里就很费时间的
其余不清楚
{
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;
}
}
{
static void Main()
{
int count = int.Parse(Console.ReadLine());
for(int i = 0; i < count; i++)
{
Console.Write(ZeroOrOne(long.Parse(Console.ReadLine())));
Console.Write(" ");
}
} static int ZeroOrOne(long number)
{
double d = Math.Sqrt((number<<1) - 1.75) + 0.5;
return d == (long)d ? 1 : 0;
}
}
{
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;
}
}
不是输入费时,应该是字符串操作(字符串相加)费时: foreach (long n in num)
result += ZeroOrOne(n) + " ";
{
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;
}
}
{
static void Main()
{
int count = int.Parse(Console.ReadLine());
for(int i = 0; i < count - 1; i++)
{
Console.Write(ZeroOrOne(uint.Parse(Console.ReadLine())));
Console.Write(" ");
}
Console.Write(ZeroOrOne(uint.Parse(Console.ReadLine())));
} static int ZeroOrOne(uint number)
{
double d = Math.Sqrt((number<<1) - 1.75) + 0.5;
return d == (uint)d ? 1 : 0;
}
}
http://acm.timus.ru/problem.aspx?space=1&num=1201 class Program
{
static void Main(string[] args)
{
string[] ss = Console.ReadLine().Split();
PrintCal(new DateTime(int.Parse(ss[2]), int.Parse(ss[1]), int.Parse(ss[0])));
PrintCal(new DateTime(2009, 3, 31));
} static void PrintCal(DateTime dt)
{
DateTime startDay = dt.AddDays(dt.Day * -1 + 1);
int index = 0;
switch (startDay.DayOfWeek)
{
case DayOfWeek.Monday: index = 0; break;
case DayOfWeek.Tuesday: index = -1; break;
case DayOfWeek.Wednesday: index = -2; break;
case DayOfWeek.Thursday: index = -3; break;
case DayOfWeek.Friday: index = -4; break;
case DayOfWeek.Saturday: index = -5; break;
case DayOfWeek.Sunday: index = -6; break;
}
for (int i = 0; i < 7; i++)
{
switch (i)
{
case 0: Console.Write("mon"); break;
case 1: Console.Write("tue"); break;
case 2: Console.Write("wed"); break;
case 3: Console.Write("thu"); break;
case 4: Console.Write("fri"); break;
case 5: Console.Write("sat"); break;
case 6: Console.Write("sun"); break;
}
for (DateTime day = startDay.AddDays(index+i); day < startDay.AddMonths(1); day = day.AddDays(7))
{
if (day.CompareTo(dt) == 0)
{
if (day.CompareTo(startDay.AddDays(7 + index)) >= 0) Console.Write(".");
Console.Write(".[" + day.Day.ToString().PadLeft(2, '.') + "]");
}
else if (day.CompareTo(dt.AddDays(7)) == 0) Console.Write(".." + day.Day.ToString().PadLeft(2, '.'));
else
{
if (day.CompareTo(startDay) < 0) Console.Write("....");
else if (day.CompareTo(startDay.AddDays(7 + index)) < 0) Console.Write(".." + day.Day.ToString().PadLeft(2, '.'));
else Console.Write("..." + day.Day.ToString().PadLeft(2, '.'));
}
}
Console.WriteLine();
}
}
}//难道是最后多输出了Console.WriteLine();??
这句复制多了,调试时用的。
using System; class Program
{
static void Main(string[] args)
{
string[] ss = Console.ReadLine().Split();
PrintCal(new DateTime(int.Parse(ss[2]), int.Parse(ss[1]), int.Parse(ss[0])));
// PrintCal(new DateTime(2009, 3, 31));
} static void PrintCal(DateTime dt)
{
DateTime startDay = dt.AddDays(dt.Day * -1 + 1);
int index = 0;
switch (startDay.DayOfWeek)
{
case DayOfWeek.Monday: index = 0; break;
case DayOfWeek.Tuesday: index = -1; break;
case DayOfWeek.Wednesday: index = -2; break;
case DayOfWeek.Thursday: index = -3; break;
case DayOfWeek.Friday: index = -4; break;
case DayOfWeek.Saturday: index = -5; break;
case DayOfWeek.Sunday: index = -6; break;
}
for (int i = 0; i < 7; i++)
{
switch (i)
{
case 0: Console.Write("mon"); break;
case 1: Console.Write("tue"); break;
case 2: Console.Write("wed"); break;
case 3: Console.Write("thu"); break;
case 4: Console.Write("fri"); break;
case 5: Console.Write("sat"); break;
case 6: Console.Write("sun"); break;
}
for (DateTime day = startDay.AddDays(index+i); day < startDay.AddMonths(1); day = day.AddDays(7))
{
if (day.CompareTo(dt) == 0)
{
if (day.CompareTo(startDay.AddDays(7 + index)) >= 0) Console.Write(" ");
Console.Write(" [" + day.Day.ToString().PadLeft(2, ' ') + "]");
}
else if (day.CompareTo(dt.AddDays(7)) == 0) Console.Write(" " + day.Day.ToString().PadLeft(2, ' '));
else
{
if (day.CompareTo(startDay) < 0) Console.Write(" ");
else if (day.CompareTo(startDay.AddDays(7 + index)) < 0) Console.Write(" " + day.Day.ToString().PadLeft(2, ' '));
else Console.Write(" " + day.Day.ToString().PadLeft(2, ' '));
}
}
Console.WriteLine();
}
}
}
{
static void Main(string[] args)
{
string[] ss = Console.ReadLine().Split();
PrintCal(new DateTime(int.Parse(ss[2]), int.Parse(ss[1]), int.Parse(ss[0])));
// PrintCal(new DateTime(2009, 3, 31));
} static void PrintCal(DateTime dt)
{
DateTime startDay = dt.AddDays(dt.Day * -1 + 1);
int index = -(((int)startDay.DayOfWeek + 6) % 7);
for (int i = 0; i < 7; i++)
{
Console.Write(new string[]{"mon","tue","wed","thu","fri","sat","sun"}[i]);
for (DateTime day = startDay.AddDays(index+i); day < startDay.AddMonths(1); day = day.AddDays(7))
{
if (day.CompareTo(dt) == 0)
{
if (day.CompareTo(startDay.AddDays(7 + index)) >= 0) Console.Write(" ");
Console.Write(" [" + day.Day.ToString().PadLeft(2, ' ') + "]");
}
else if (day.CompareTo(dt.AddDays(7)) == 0) Console.Write(" " + day.Day.ToString().PadLeft(2, ' '));
else
{
if (day.CompareTo(startDay) < 0) Console.Write(" ");
else if (day.CompareTo(startDay.AddDays(7 + index)) < 0) Console.Write(" " + day.Day.ToString().PadLeft(2, ' '));
else Console.Write(" " + day.Day.ToString().PadLeft(2, ' '));
}
}
Console.WriteLine();
}
}
}
Good!写的时候就觉得那两个switch很碍眼。
using System.Globalization;
using System.Text.RegularExpressions;// http://acm.timus.ru/problem.aspx?space=1&num=1001
class Acm1001
{
static void Main()
{
string[] nums = Regex.Split(Console.In.ReadToEnd().Trim(), @"\s+");
for (int i = nums.Length - 1; i >= 0; i--)
{
Console.WriteLine(Math.Sqrt(ulong.Parse(nums[i])).ToString("F4", CultureInfo.InvariantCulture));
}
}
}
请参见:http://www.cnblogs.com/skyivben/archive/2008/06/07/1215740.html更多请见:http://www.cnblogs.com/skyivben/archive/2008/12/08/1350510.html
using System.Globalization;public class ReverseRoot
{
private static void Main()
{
NumberFormatInfo nfi = NumberFormatInfo.InvariantInfo;
string[] input = Console.In.ReadToEnd().Split(
new char[] {' ', '\t', '\n', '\r'}, StringSplitOptions.RemoveEmptyEntries);
for (int i = input.Length - 1; i >= 0; i--)
{
double root = Math.Sqrt(double.Parse(input[i], nfi));
Console.WriteLine(string.Format(nfi, "{0:F4}", root));
}
}
}
{
// http://acm.timus.ru/problem.aspx?space=1&num=1201
sealed class T1201
{
static void Main()
{
var ss = Console.ReadLine().Split();
var dt = new DateTime(int.Parse(ss[2]), int.Parse(ss[1]), int.Parse(ss[0]));
var d0 = dt.AddDays(1 - dt.Day);
var k = -(((int)d0.DayOfWeek + 6) % 7);
string[] weeks = {"mon", "tue", "wed", "thu", "fri", "sat", "sun"};
for (var i = 0; i < weeks.Length; i++)
{
Console.Write(weeks[i]);
for (var d = d0.AddDays(k + i); d < d0.AddMonths(1); d = d.AddDays(7))
Console.Write((d < d0) ? " " : ((d != dt) ?
(((d >= d0.AddDays(k + 7) && d != dt.AddDays(7)) ? " " : "") + "{0,4}") :
(((d >= d0.AddDays(k + 7))? " " : "") + " [{0,2}]")), d.Day);
Console.WriteLine();
}
}
}
}
(((d >= d0.AddDays(k + 7) && d != dt.AddDays(7)) ? " " : "") + "{0,4}") :
(((d >= d0.AddDays(k + 7))? " " : "") + " [{0,2}]")), d.Day);可改为:Console.Write((d < d0) ? " " : ((d != dt) ? "{0,4} " : " [{0,2}]"), d.Day);请参见: http://www.cnblogs.com/skyivben/archive/2009/03/12/1410028.html