LoginSignup
0
0

スキルチェック、競プロをC#で解く環境作る。おまけでチートシート

Last updated at Posted at 2023-06-02

VscodeでC#がコンパイルできる環境を整える

参考記事:【M1/Mac】C#をVSCodeで動かす方法【.NET 6.0】【VSCode】
こちらの記事から開発環境を整える

標準入力ができる環境を整える

paizaやAOJでも実行環境はありますが、できればVscodeでデバッグしたいです
(補完とかいろいろ便利なので)
先ほど開発環境を整えたらProgram.csファイルが生成されるので、以下のコードをコピペしてください
usingやConsole.SetInの意味は知らなくても大丈夫です

Program.cs
using System;
using System.IO;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        // ここのinputに標準入力を入れます
        string input = @"1";
        using (StringReader reader = new StringReader(input))
        {
            // ここから処理を記述します
            Console.SetIn(reader);
            string input1 = Console.ReadLine();
            Console.WriteLine(input1); // 1
            // 処理の記述はここまで
        }
    }
    
}

入力

基本的な入力 (1区切り文字列の入力)

区切りのない連続した文字列の入力。
入力した文字列の型を任意に指定でき、指定しない場合はstr型になる。

Input1.cs
string a = Console.ReadLine();// string型
int b = int.Parse(Console.ReadLine());// int型
Console.WriteLine($"{a} {a.GetType()}");// 1 System.String
Console.WriteLine($"{b} {b.GetType()}");// 1 System.Int32

行の配列(横一列複数の入力)

区切りのある横一列の複数の文字列をstring型で配列に代入する
区切りの入れ方はsplitで指定する
int型の配列への変換は、Array.ConvertAll関数を使用する

Input2.cs
// 入力 1 2
string[] a = Console.ReadLine().Split(' ');
Console.WriteLine($"{a[0]} {a[0].GetType()}");// 1 System.String
int[] b = Array.ConvertAll(a, int.Parse);
Console.WriteLine($"{b[1]} {b[1].GetType()}");// 2 System.Int32

列の配列(縦一列の複数の入力)

縦一列に入力された文字列を一つのリストで管理する。

Input2.cs
string[] a = new string[5];
// 入力: 
// 1
// 2
// 3
// 4
// 5
int i = 0;
while (true)
{
    string line = Console.ReadLine();// 一行ずつ代入されて配列に格納している
    if (line == null) break;
    a[i++] = line;
}
Console.WriteLine("[" + string.Join(",", a) + "]");// [1,2,3,4,5]

二次元配列(縦横複数行の入力)と配列内容の表示

縦横複数列の入力を列ごとに二次元配列で管理する。

Input.cs
// 入力:
// 1 2 5
// 7 1 6
// 2 3 9
// 6 3 0
// 0 1 2
// 二次元配列の作成
int[,] array = new int[row, col];
// 二次元配列の作成と入力
for (int i = 0; i < row; i++){
    string[] input1 = Console.ReadLine().Split(' ');
    for (int j = 0; j < col; j++){
        array[i, j] = int.Parse(input1[j]);
    }
}
// 二次元配列の内容
Console.WriteLine("二次元配列の内容:");
for (int i = 0; i < row; i++){
    for (int j = 0; j < col; j++){
        Console.Write(array[i, j] + " ");
    }
    Console.WriteLine();
}

2次元配列と3次元配列の初期化

Input.cs
// 初期値0で生成される
int[,] array3D = new int[5,2];// 5*2の配列
// [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] 
int[,,] array3D = new int[5,2,3];// 5*2*3の配列
// [[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], 
// [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
// [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]]

文字列に特定の文字列が含まれているか調べる

大文字小文字を区別して判定

Input.cs
string str = "ABCDEFG";

    Console.WriteLine(str.Contains("ABC")); // 出力:True
    Console.WriteLine(str.Contains("abc")); // 出力:False
    Console.WriteLine(str.Contains(""));       // 出力:True

    Console.WriteLine(str.IndexOf(""));        // 出力:0

大文字小文字を区別せずに判定

Input.cs
string str = "ABCDEFG";

Console.WriteLine(str.IndexOf("BCD", StringComparison.OrdinalIgnoreCase) >= 0);
// 出力:True
Console.WriteLine(str.IndexOf("bcd", StringComparison.OrdinalIgnoreCase) >= 0);
// 出力:True
Console.WriteLine(str.IndexOf("XYZ", StringComparison.OrdinalIgnoreCase) >= 0);
// 出力:False

繰り返した文字を置換する方法

指定した文字を指定した回数繰り返した文字列を取得する

Input.cs
string repeat = (new string('*', 5)).Replace("*", "abc");

//結果を表示する
Console.WriteLine(repeat);
//abcabcabcabcabc

文字列の配列を作って、配列操作を行う

Input.cs
// 入力:abcde
string a = Console.ReadLine();
StringBuilder b = new StringBuilder(a);
b.Remove(0, 2);// cde
b.Append(a.Substring(0, 2));
Console.WriteLine(b.ToString());// cdeab

型変換と小数点指定

C#の型変換の方法と、小数点以下の桁数を指定する方法を記述

Input.cs
// 入力:
// 3 2
string[] a = Console.ReadLine().Split(' ');
int[] b = Array.ConvertAll(a, int.Parse);// [3, 2]
double c = (double)b[0] / b[1];// int型からdouble型に変換
// double c = (double)(b[0] / b[1]);// これは変換できないので注意 1.0と表示される
Console.WriteLine(c);// 1.5
string d = c.ToString("F5");// 小数点以下の桁数を指定
Console.WriteLine(d);// 1.50000

C#で1から5までを3つ組み合わせ、その組み合わせを重複がないように出力したい

Input.cs
using System;
using System.Collections.Generic;
using System.Linq;

class MainClass {
  public static void Main (string[] args) {
    List<int> list1 = Enumerable.Range(1, 5).ToList();
    List<List<int>> combinations = new List<List<int>>();
    foreach (var comb in Combinations(list1, 3)) {
        combinations.Add(comb.ToList());
    }

    foreach(var comb in combinations) {
        Console.WriteLine(string.Join(",", comb));
    }
  }

  static IEnumerable<IEnumerable<T>> Combinations<T>(IEnumerable<T> elements, int k) {
        return k == 0 ? new[] { new T[0] } :
            elements.SelectMany((e, i) =>
                Combinations(elements.Skip(i + 1), k - 1).Select(c => (new[] { e }).Concat(c)));
    }
}
// 出力
// 1,2,3
// 1,2,4
// 1,2,5
// 1,3,4
// 1,3,5
// 1,4,5
// 2,3,4
// 2,3,5
// 2,4,5
// 3,4,5
0
0
0

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
0
0