9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【C#】競技プログラミング 入出力処理

Last updated at Posted at 2020-09-09

#入力
##文字列1行

string str = Console.ReadLine();

int配列

int[] array = Console.ReadLine().Split().Select(int.Parse).ToArray();

long配列

long[] array = Console.ReadLine().Split().Select(long.Parse).ToArray();

Console.ReadLine().Split()までの部分で関数を作っておくと良いかもしれません。

N Mなどの複数の時

以下のクラスをコピペして貼る。
(atcoder社長のchokudai氏の利用しているScannerを一部改変したもの)

class Scanner
{
    string[] s;
    int i;
    char[] cs = new char[] { ' ' };
    public Scanner()
    {
        s = new string[0];
        i = 0;
    }
 
    public string Next()
    {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 0) return Next();
        i = 0;
        return s[i++];
    }
 
    public int NextInt()
    {
        return int.Parse(Next());
    }

    public long NextLong()
    {
        return long.Parse(Next());
    }

使い方

var scanner = new Scanner();
//N M K
int N = scanner.NextInt();
long M = scanner.NextLong();
string K = scanner.Next();

#出力

基本

//改行も入ります
var num = 20;
Console.WriteLine(num); 
Console.WriteLine("Yes");
Console.WriteLine("No");

フォーマット

var n = 12, m = 24;
Console.WriteLine("{0} {1} {2}", n, m, 4 + 4);

変数名直接(古いコンパイラでは対応していないかも)

var n = 12, m = 24;
Console.WriteLine("${n} ${m}");

配列

var array = new int[]{ 20, 30, 22, 11 };
//コンマ区切り
Console.WriteLine(string.Join(",", array));
//スペース区切り
Console.WriteLine(string.Join(" ", array));
//改行区切り
Console.WriteLine(string.Join("\r\n", array));

foreachを使ったやつ。atcoderだと、一番最後に空白あっても問題ないため

var array = new int[]{ 20, 30, 22, 11 };
foreach(var a in array)
{
	Console.WriteLine("{0} ", a);
}
9
6
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?