HDOJ C#指南

之前HDOJ上线C#的时候,特地为此写了一个说明。
不过然后这个功能并没有用,也就留在题库里,DIY/Web DIY/ STD Contest等全部没上线对此的支持,说明也没上。
然后今天整理硬盘的时候发现这个,于是贴到这里来了(做个备份)。
(当时写的是英文版,今天懒得翻译了233333)

Since 11 March 2015, the C# programming language is supported at HDU Online Judge.
Microsoft Visual C# 2010 Compiler version 4.0.30319.33440 (.NET 4.5) is used to compile and run solutions written in C#.
You may get the compiler after you installed the .Net Framework 4.5.

To read online documentation ,please visit https://msdn.microsoft.com/en-us/library/gg145045(v=vs.110).aspx .

Q : How to write a C# solution?
A : Here is a sample of A + B Problem solution:

using System;

class Program {
    public static void Main() {
        string line;
        string []p;
        int a,b;
        while((line=Console.ReadLine())!=null){
            p=line.Split(' ');
            a=int.Parse(p[0]);b=int.Parse(p[1]);
            Console.WriteLine(a+b);
        }
    }
}

The following solution is also correct:

using System;

namespace HDOJ_A_Plus_B_CSharp{
    class HDOJ1000 {
        public static void Main() {
            string line;
            string []p;
            int a,b;
            while((line=Console.ReadLine())!=null){
                p=line.Split(' ');
                a=int.Parse(p[0]);b=int.Parse(p[1]);
                Console.WriteLine(a+b);
            }
        }
    }
}

Q : How to Input/Output?
A :
1) The standard input/output methods like Console.ReadLine, String.Split and Console.WriteLine are not always sufficient. In some problems it’s necessary to manually implement fast input reading or output formatting.
2) In some problems numbers or strings are not necessarily separated with single space. Use this code to split:

string input;
while((input=Console.ReadLine())!=null){
   string[] part=input.Split(new char[] {' ', '\t', '\r', '\n'},StringSplitOptions.RemoveEmptyEntries);
}

3) To print a new line, don’t use "\n" ,or you’ll get an Presentation Error / Wrong Answer. Use "\r\n" or Environment.NewLine instead.
4) To print a decimal number retains 2 digits after the decimal point with decimal separator ".", use following code:

Console.WriteLine(decimal_value.ToString("F" + precision, System.Globalization.CultureInfo.InvariantCulture));

Q : Is BigInteger in System.Numerics supported?
A : Yes. You can use BigInteger in C# on HDOJ freely.
Please don’t forget to add “using System.Numerics;” at the beginning of your solution. Here is a sample of N!(1042) using BigInteger.

using System;
using System.Numerics;

class Program
{
    static void Main(string[] args)
    {
        string sin;
        int n;
        BigInteger ans;
        while((sin=Console.ReadLine())!=null){
            n=int.Parse(sin);
            ans=1;
            for(int i=2;i<=n;i++)
                ans*=i;
            Console.WriteLine(ans);
        }
    }
}

Q : Is there any way to determine if my C# program is runned at Online Judge or not ?
A : There is a conditional define of compiler called ONLINE_JUDGE. Example of using:

#if (!ONLINE_JUDGE)
    //Something you don't want your program do on online judge , such as:
    Console.WriteLine("This is a test.");
#endif

#if (ONLINE_JUDGE)
    //Something you expect your program do on online judge 
#elif (!ONLINE_JUDGE && DEBUG)
    //...
#else
    //...
#endif

你可能感兴趣的:(HDOJ C#指南)