俄罗斯乌拉尔大学在线题库 是一个可以使用C#语言的在线ACM题库,有兴趣的朋友可以去试试。
Problem 1000. A+B Problem 是入门,就是简单地求整数 A 和 B 的和就行了,答案如下:
Problem 1001. Reverse root 也很简单,就是给出一组整数,然后反序输出其平方根就行了,答案如下:
注意该程序的第11行不可省略,不然就无法通过。目前还不知道是什么原因(已经找到原因了,请参见2楼的评论)。
Problem 1005. Stone pile 要求将若干石头分为两堆使其重量差最小,答案如下:
Problem 1068. Sum 也很简单,就是求 1 到 N 的和,答案如下:
Problem 1070. A local time 要求根据两地间的往返航班的起降时刻(用本地时间表示)来计算这两地间的时差,答案如下:
其他的题目可能就没有这么容易了。 :)
根据8楼 CppGohan 朋友的评论,Sphere Onlile Judge (SPOJ) 也是一个支持C#语言的在线ACM题库。
1. Life, the Universe, and Everything 是入门,就是一行一行地将标准输入原样复制到标准输出直到遇到一行为“42”为止,答案如下:
2. Prime Generator 要求生成多组指定范围的素数,答案如下:
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 的编译器。
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
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
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
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 > 0) return n * (n + 1) / 2;
14 if (n < 0) return 1 + n * (1 - n) / 2;
15 return 1;
16 }
17 }
18
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 > 0) return n * (n + 1) / 2;
14 if (n < 0) return 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(1, 1, 1, int.Parse(ss[0]), int.Parse(ss[1]), 0);
32 }
33 }
34
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(1, 1, 1, int.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 == null) break;
18 if (s == "42") break;
19 writer.WriteLine(s);
20 }
21 }
22 }
23
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 == null) break;
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 += 2) if (!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 += 2) if (!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 += 2) if (!sieve[(n - min) >> 1]) writer.WriteLine(n);
97 writer.WriteLine();
98 }
99 }
100
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 += 2) if (!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 += 2) if (!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 += 2) if (!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 的编译器。