银河

SKYIV STUDIO

  博客园 :: 首页 ::  ::  :: 订阅 订阅 :: 管理 ::
  133 随笔 :: 2 文章 :: 1006 评论 :: 48 引用
俄罗斯乌拉尔大学在线题库 是一个可以使用C#语言的在线ACM题库,有兴趣的朋友可以去试试。

Problem 1000. A+B Problem 是入门,就是简单地求整数 A 和 B 的和就行了,答案如下:

 1 using System;
 2 
 3 // http://acm.timus.ru/problem.aspx?space=1&num=1000
 4 class Acm1000
 5 {
 6   static void Main()
 7   {
 8     string[] ss = Console.ReadLine().Split();
 9     Console.WriteLine(long.Parse(ss[0]) + long.Parse(ss[1]));
10   }
11 }
12 

Problem 1001. Reverse root 也很简单,就是给出一组整数,然后反序输出其平方根就行了,答案如下:

 1 using System;
 2 using System.Threading;
 3 using System.Globalization;
 4 using System.Text.RegularExpressions;
 5 
 6 // http://acm.timus.ru/problem.aspx?space=1&num=1001
 7 class Acm1001
 8 {
 9   static void Main()
10   {
11     Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
12     string[] nums = Regex.Split(Console.In.ReadToEnd().Trim(), @"\s+");
13     for (int i = nums.Length - 1; i >= 0; i--)
14       Console.WriteLine("{0:F4}", Math.Sqrt(ulong.Parse(nums[i])));
15   }
16 }
17 

注意该程序的第11行不可省略,不然就无法通过。目前还不知道是什么原因(已经找到原因了,请参见2楼的评论)。

Problem 1005. Stone pile 要求将若干石头分为两堆使其重量差最小,答案如下:

 1 using System;
 2 using System.IO;
 3 using System.Text.RegularExpressions;
 4 
 5 // http://acm.timus.ru/problem.aspx?space=1&num=1005
 6 class Acm1005
 7 {
 8   static void Main()
 9   {
10     new Acm1005().Run(Console.In, Console.Out);
11   }
12 
13   void Run(TextReader reader, TextWriter writer)
14   {
15     writer.WriteLine(GetResult(GetWeigths(reader)));
16   }
17 
18   int[] GetWeigths(TextReader reader)
19   {
20     string[] ss = Regex.Split(reader.ReadToEnd().Trim(), @"\s+");
21     int[] weigths = new int[int.Parse(ss[0])];
22     for (int i = 0; i < weigths.Length; i++) weigths[i] = int.Parse(ss[i + 1]);
23     return weigths;
24   }
25 
26   int GetResult(int[] weigths)
27   {
28     int n = weigths.Length - 1;
29     int result = int.MaxValue;
30     int[] piles = new int[2];
31     for (int i = (1 << n) - 1; i >= 0; i--)
32     {
33       piles[0= weigths[n];
34       piles[1= 0;
35       for (int j = n - 1; j >= 0; j--) piles[(((i >> j) & 1== 0? 1 : 0+= weigths[j];
36       int v = Math.Abs(piles[0- piles[1]);
37       if (result > v) result = v;
38     }
39     return result;
40   }
41 }
42 


Problem 1068. Sum 也很简单,就是求 1 到 N 的和,答案如下:

 1 using System;
 2 
 3 // http://acm.timus.ru/problem.aspx?space=1&num=1068
 4 class Acm1068
 5 {
 6   static void Main()
 7   {
 8     Console.WriteLine(Sum(int.Parse(Console.ReadLine())));
 9   }
10 
11   static long Sum(long n)
12   {
13     if (n > 0return n * (n + 1/ 2;
14     if (n < 0return 1 + n * (1 - n) / 2;
15     return 1;
16   }
17 }
18 

Problem 1070. A local time 要求根据两地间的往返航班的起降时刻(用本地时间表示)来计算这两地间的时差,答案如下:

 1 using System;
 2 using System.IO;
 3 
 4 // http://acm.timus.ru/problem.aspx?space=1&num=1070
 5 class Acm1070
 6 {
 7   static void Main()
 8   {
 9     new Acm1070().Run(Console.In, Console.Out);
10   }
11 
12   void Run(TextReader reader, TextWriter writer)
13   {
14     double diff1 = GetDuration(reader);
15     double diff2 = GetDuration(reader);
16     writer.WriteLine(Math.Abs((int)Math.Round((diff1 - diff2) / 2)));
17   }
18 
19   double GetDuration(TextReader reader)
20   {
21     string[] ss = reader.ReadLine().Split();
22     double diff = (GetTime(ss[1]) - GetTime(ss[0])).TotalHours;
23     if (diff > 6) diff -= 24;
24     if (diff < -6) diff += 24;
25     return diff;
26   }
27 
28   DateTime GetTime(string s)
29   {
30     string[] ss = s.Split('.');
31     return new DateTime(111int.Parse(ss[0]), int.Parse(ss[1]), 0);
32   }
33 }
34 

其他的题目可能就没有这么容易了。 :)


根据8楼 CppGohan 朋友的评论,Sphere Onlile Judge (SPOJ) 也是一个支持C#语言的在线ACM题库。

1. Life, the Universe, and Everything 是入门,就是一行一行地将标准输入原样复制到标准输出直到遇到一行为“42”为止,答案如下:

 1 using System;
 2 using System.IO;
 3 
 4 // http://www.spoj.pl/problems/TEST/
 5 class S1
 6 {
 7   static void Main()
 8   {
 9     new S1().Run(Console.In, Console.Out);
10   }
11 
12   void Run(TextReader reader, TextWriter writer)
13   {
14     for (; ; )
15     {
16       string s = reader.ReadLine();
17       if (s == nullbreak;
18       if (s == "42"break;
19       writer.WriteLine(s);
20     }
21   }
22 }
23 


2. Prime Generator 要求生成多组指定范围的素数,答案如下:

  1 using System;
  2 using System.IO;
  3 
  4 // http://www.spoj.pl/problems/PRIME1/
  5 class S2
  6 {
  7   struct Range
  8   {
  9     public int Min;
 10     public int Max;
 11   }
 12 
 13   static void Main()
 14   {
 15     new S2().Run(Console.In, Console.Out);
 16   }
 17 
 18   void Run(TextReader reader, TextWriter writer)
 19   {
 20     int theMax;
 21     Range[] ranges = GetRanges(reader, out theMax);
 22     int min = 3;
 23     int max = (int)Math.Sqrt(theMax) + 1;
 24     if ((max & 1== 0) max--;
 25     int[] primes = GetPrimes(GetSieve(min, max), min, max);
 26     foreach (Range range in ranges)
 27     {
 28       min = range.Min;
 29       max = range.Max;
 30       if (min == 1) min = 3;
 31       if ((min & 1== 0) min++;
 32       if ((max & 1== 0) max--;
 33       OutPrimes(writer, GetSieve(primes, min, max), min, max, range.Min, range.Max);
 34     }
 35   }
 36 
 37   Range[] GetRanges(TextReader reader, out int max)
 38   {
 39     max = 0;
 40     Range[] ranges = new Range[int.Parse(reader.ReadLine())];
 41     for (int i = 0; i < ranges.Length; i++)
 42     {
 43       string[] ss = reader.ReadLine().Split();
 44       ranges[i].Min = int.Parse(ss[0]);
 45       ranges[i].Max = int.Parse(ss[1]);
 46       if (max < ranges[i].Max) max = ranges[i].Max;
 47     }
 48     return ranges;
 49   }
 50 
 51   bool[] GetSieve(int min, int max)
 52   {
 53     bool[] sieve = new bool[((max - min) >> 1+ 1];
 54     int sqrt = (int)Math.Sqrt(max) + 1;
 55     for (int n = min; n <= sqrt; n += 2if (!sieve[(n - min) >> 1]) SetSieve(sieve, n, min, max);
 56     return sieve;
 57   }
 58 
 59   bool[] GetSieve(int[] primes, int min, int max)
 60   {
 61     bool[] sieve = new bool[((max - min) >> 1+ 1];
 62     int sqrt = (int)Math.Sqrt(max) + 1;
 63     for (int i = 0; primes[i] <= sqrt; i++) SetSieve(sieve, primes[i], min, max);
 64     return sieve;
 65   }
 66 
 67   void SetSieve(bool[] sieve, int v, int min, int max)
 68   {
 69     int step = v << 1;
 70     for (int n = GetStart(v, min); n <= max; n += step) sieve[(n - min) >> 1= true;
 71   }
 72 
 73   int GetStart(int v, int min)
 74   {
 75     int v2 = v * v;
 76     if (v2 >= min) return v2;
 77     int x = min / v;
 78     if ((x & 1== 0) x++;
 79     v2 = x * v;
 80     if (v2 < min) v2 += v * 2;
 81     return v2;
 82   }
 83 
 84   int[] GetPrimes(bool[] sieve, int min, int max)
 85   {
 86     int[] primes = new int[3401];
 87     int i = 0;
 88     for (int n = min; n <= max; n += 2if (!sieve[(n - min) >> 1]) primes[i++= n;
 89     primes[i] = int.MaxValue;
 90     return primes;
 91   }
 92 
 93   void OutPrimes(TextWriter writer, bool[] sieve, int min, int max, int min0, int max0)
 94   {
 95     if (min0 <= 2 && max0 >= 2) writer.WriteLine(2);
 96     for (int n = min; n <= max; n += 2if (!sieve[(n - min) >> 1]) writer.WriteLine(n);
 97     writer.WriteLine();
 98   }
 99 }
100 


Sphere Onlile Judge (SPOJ) 应该是使用 Linux 操作系统。目前使用的 C# 编译器是 mcs 1.0.1 (有点旧,目前最新版本是 1.9.1),C/C++ 编译器是 gcc 4.0.0-8 (也有点旧,目前最新版本是 4.3.1)。
俄罗斯乌拉尔大学在线题库 应该是使用 Windows 操作系统,估计是使用 C# 2.0 的编译器。

posted on 2008-06-07 20:43 银河 阅读(4633) 评论(34)  编辑 收藏 网摘 所属分类: 算法

评论

#1楼 2008-06-07 21:06 Cat Chen      
其实测试环境加上C#支持就可以了,这不难。不过C#没有按空格分割读入数字、字符串的能力,要自己手写,这个非常烦。
  回复  引用  查看    

#2楼[楼主] 2008-06-07 21:07 银河      
已经找到 Problem 1001. Reverse root 的答案中第11行不可省略的原因了。
俄罗斯乌拉尔大学在线题库的服务器是俄罗斯的服务器,如果省略了第11行,就相当将该行改为:
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("ru-RU");
这样,该程序的输出将使用逗号(,)而不是句号(.)作为小数点。

  回复  引用  查看    

#3楼[楼主] 2008-06-07 21:11 银河      
--引用--------------------------------------------------
Cat Chen: 其实测试环境加上C#支持就可以了,这不难。不过C#没有按空格分割读入数字、字符串的能力,要自己手写,这个非常烦。
--------------------------------------------------------
C#中可以使用正则表达式实现按空格分割读入字符串,如 Problem 1001. Reverse root 的答案中第12行所示:
string[] nums = Regex.Split(Console.In.ReadToEnd().Trim(), @"\s+");

  回复  引用  查看    

#4楼[楼主] 2008-06-07 21:59 银河      
如果把 Problem 1001. Reverse root 的答案中第14行改为:
Console.WriteLine(Math.Sqrt(ulong.Parse(nums[i])).ToString("F4", CultureInfo.InvariantCulture));
就可以省略该程序中的第11行了。

  回复  引用  查看    

#5楼 2008-06-07 22:16 tengcy[未注册用户]
acm.cs.bupt.cn
北邮的acm oj平台,二年前就支持c#了。。

  回复  引用    

#6楼[楼主] 2008-06-07 22:21 银河      
--引用--------------------------------------------------
tengcy: acm.cs.bupt.cn
北邮的acm oj平台,二年前就支持c#了。。
--------------------------------------------------------
我怎么无法打开该网页,是不是要在教育网内才能使用?

  回复  引用  查看    

支持什么语言没什么重要的,
  回复  引用  查看    

#8楼 2008-06-07 22:38 CppGohan[未注册用户]
SPOJ是我所了解的支持语言巨多的OJ
  回复  引用    

#9楼 2008-06-07 23:50 化石[未注册用户]
喜欢一大片题目Solved的感觉^_^。不知道c#会不会很容易超时……
  回复  引用    

#10楼 2008-06-08 00:19 BAsil      
关注
  回复  引用  查看    

#11楼 2008-06-08 10:09 布尔      
程序员杂志的算法擂台可以采用这样的系统,呵呵。
  回复  引用  查看    

#12楼[楼主] 2008-06-08 10:12 银河      
--引用-(7楼)---------------------------------------------
Phinecos(洞庭散人): 支持什么语言没什么重要的,
--------------------------------------------------------
能够支持C#语言还是很有好处的。
1. 可能使用自己最熟悉、最习惯的语言。
2. FCL 提供的基础类库还是非常丰富的。
如 Problem 1070. A local time 答案中的第22行:
double diff = (GetTime(ss[1]) - GetTime(ss[0])).TotalHours;
简单明了地将两个时间相减得到以小时数表示的时差。
在其他语言中可能就没有这么容易了。
:)

  回复  引用  查看    

#13楼[楼主] 2008-06-08 11:03 银河      
--引用-(8楼)---------------------------------------------
CppGohan: SPOJ是我所了解的支持语言巨多的OJ
--------------------------------------------------------
Sphere Onlile Judge (SPOJ) 确实是一个很好的支持C#语言的在线题库。
我已经在正文中添加了这方面的内容。
:)

  回复  引用  查看    

#14楼[楼主] 2008-06-08 11:49 银河      
--引用-(9楼)---------------------------------------------
化石: 喜欢一大片题目Solved的感觉^_^。不知道c#会不会很容易超时……
--------------------------------------------------------
C#其实并不比其他语言慢。而且速度的关键在于算法。

  回复  引用  查看    

#15楼 2008-06-08 14:08 tty[未注册用户]
TopCoder:c#,vb都行。。
  回复  引用    

#16楼 2008-06-08 15:44 重典      
http://www.codeplex.com/OnlineJudge
C#...都支持

  回复  引用  查看    

--引用--------------------------------------------------
银河: --引用-(7楼)---------------------------------------------
Phinecos(洞庭散人): 支持什么语言没什么重要的,
--------------------------------------------------------
能够支持C#语言还是很有好处的。
1. 可能使用自己最熟悉、最习惯的语言。
2. FCL 提供的基础类库还是非常丰富的。
如 Problem 1070. A local time 答案中的第22行:
double diff = (GetTime(ss[1]) - GetTime(ss[0])).TotalHours;
简单明了地将两个时间相减得到以小时数表示的时差。
在其他语言中可能就没有这么容易了。
:)
--------------------------------------------------------
直接使用类库,反而失去了锻炼的意义,像大数类等这样的东西,最好自己写才有提高,否则面对其他算法题,如何能应付的来?

  回复  引用    

#18楼 2008-06-08 16:55 Zhuang miao      
这个我喜欢~谢谢
  回复  引用  查看    

#19楼[楼主] 2008-06-08 17:13 银河      
--引用-(16楼)--------------------------------------------
重典: http://www.codeplex.com/OnlineJudge
C#...都支持
--------------------------------------------------------
这个,好像只提供一套在线题库的程序,我没有找到题库。 :)

  回复  引用  查看    

#20楼 2008-06-08 18:21 深蓝      
比较有意思。可惜我不是搞ACM的人。
  回复  引用  查看    

#21楼 2008-06-08 20:19 农夫三拳      
呵呵,做做TopCoder不错
  回复  引用  查看    

#22楼 2008-06-08 20:54 空间/IV      
不同的语言各有其优缺点。
  回复  引用  查看    

#23楼 2008-06-08 22:50 刘亮      
我高中一直搞oi,到了大学也没搞acm了。
  回复  引用  查看    

c#果真强大
  回复  引用    

#25楼 2008-06-09 16:02 ndaysy[未注册用户]
请问是不是不能识别foreach这样的语法?问题1001我用for可以通过,用foreach就报错了。
  回复  引用    

#26楼[楼主] 2008-06-09 19:17 银河      
--引用-(25楼)--------------------------------------------
ndaysy: 请问是不是不能识别foreach这样的语法?问题1001我用for可以通过,用foreach就报错了。
--------------------------------------------------------
当然可以识别 foreach 这样的语法。
Problem 1001 要求反序输出其平方根,而 foreach 只能正序输出,所以报错了(除非在 foreach 循环之前先调用 Array.Reverse() 方法)。

  回复  引用  查看    

#27楼 2008-06-10 13:34 体彩[未注册用户]
你根银河使者是一个人吗???
  回复  引用    

#28楼 2008-06-11 12:10 银河使者      
不是一个人。http://www.cnblogs.com/Emoticons/qface/055243929.gif" alt="" />
  回复  引用  查看    

#29楼 2008-06-11 14:49 七爵[未注册用户]
string[] nums = System.Text.RegularExpressions.Regex.Split(Console.In.ReadToEnd().Trim(), @"\s+");

怎么运行了就一直在输入数据状态?

  回复  引用    

#30楼[楼主] 2008-06-12 07:54 银河      
--引用-(29楼)---------------------------------------------
七爵: string[] nums = System.Text.RegularExpressions.Regex.Split(Console.In.ReadToEnd().Trim(), @&quot;\s+&quot;);

怎么运行了就一直在输入数据状态?

--------------------------------------------------------
需要重定向标准输入:
C> acm1001.exe < test.txt

  回复  引用  查看    

#31楼 2008-07-26 00:08 seek      
楼主可否把1005的算法描述下,或者在GetResult加点注释吧。有些晦涩~~
  回复  引用  查看    

#32楼[楼主] 2008-07-26 10:05 银河      
@seek (31楼)
谢谢关注。
算法的解释请参见“Timus 1005. Stone pile”(http://www.cnblogs.com/skyivben/archive/2008/07/26/1251907.html" target="_new">http://www.cnblogs.com/skyivben/archive/2008/07/26/1251907.html)


  回复  引用  查看    

#33楼 2009-01-17 17:23 davin      
弱弱的问一句,problem 1000 a+b问题那样做对么? 我在学校里也搞过acm,记得第一次上机练习就是在acm.zju.edu.cn上做这一题,如果a+b如你所写的代码,在判题系统下通过肯定不可能.除非指明a,b均为不超过64为的整型,但一旦指明这些acm和普通的程序又有何分别?
btw:对acm最大的感概就是测试数据很强大,也可以说是很极端,当然只有这样才能保证程序的健壮性。

  回复  引用  查看    

#34楼[楼主] 2009-01-18 09:47 银河      
@davin
Problem 1000. A+B Problem 那样做是对的,已经通过了 Timus Online Judge 的检测。这题的本意就是用来入门的,让人们熟悉该系统的规则,而不是出一个难题。

  回复  引用  查看    




发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 1215740




相关文章:

相关链接: