程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 可以使用C#語言的在線ACM題庫

可以使用C#語言的在線ACM題庫

編輯:C#入門知識

可以使用C#語言的在線ACM題庫


俄羅斯烏拉爾大學在線題庫 是一個可以使用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 > 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
其他的題目可能就沒有這麼容易了。 :)


根據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. 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

Sphere Onlile Judge (SPOJ) 應該是使用 Linux 操作系統。目前使用的 C# 編譯器是 mcs 1.0.1 (有點舊,目前最新版本是 1.9.1),C/C++ 編譯器是 gcc 4.0.0-8 (也有點舊,目前最新版本是 4.3.1)。

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved