2次元配列の回転

2 次元配列を、時計回り、反時計回りに回転させるプログラムです。 実行例です。 時計回りに回転 0 1 2 3 4 5 3 0 4 1 5 2 5 4 3 2 1 0 2 5 1 4 0 3 反時計回りに回転 0 1 2 3 4 5 2 5 1 4 0 3 5 4 3 2 1 0 3 0 4 1 5 2 プログラム using System; using Syst…

Mono で BigInteger を使う

Mono で BigInteger を使う方法です。 以下のコードでは BigInteger を使っています。mcs コマンドでコンパイルすると、コンパイルエラーになります。 // sample.cs using System; class Program { public static void Main() { BigInteger x = 10; Console.…

長さ 1 の文字列の配列を文字の配列に変換する

文字列を要素とする配列があります。配列に格納されている文字列の長さは全て 1 とします。 そのときに、文字列の配列を、文字の配列に変換するプログラムです。 using System; using System.Collections.Generic; using System.Linq; class Program { stati…

Mono: checked キーワードを使わずに、オーバーフローチェックを有効にする

以下の記事で、Visual Studio では checked キーワードを使わなくても、ビルド設定でオーバーフローチェックができるとありました。 競技プログラミングのための C# (4.0 以降) の Tips 詰め合わせ - C♯の勉強 Mono でも checked キーワードを使わずにオー…

0..6 の範囲でぐるぐる回る

mod

0..6 の範囲でぐるぐる回るサンプルです。 以下の、上の段の数値を下の段の数値に変換するサンプルです。 -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ↓に変換する 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, …

Codeforces 1B - Spreadsheets

Problem - 1B - Codeforces using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; class Program { // A, B, C, ..., Y, Z, AA, AB, AC, ... を返す static IEnumerable<string> Seq() { // A = 1, B = 2, Y </string>…

Convert クラスで基数変換

Convert クラスを使用して基数変換を行うサンプルです。 using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { for (int i = 0; i < 256; i++) { var bin = Convert.ToString(i, 2); var oct = Conver…

Python の itertools.repeat を C# で作る。その 2

Python の itertools.repeat を C# で作る の、その 2 です。 using System; using System.Collections.Generic; using System.Linq; class Iter { public static IEnumerable<T> Repeat<T>(T obj) { for (;;) { yield return obj; } } public static IEnumerable<T> </t></t></t>…

Python の itertools.count を C# で作る

Python の itertools.count を C# で作ってみます。 using System; using System.Collections.Generic; using System.Linq; class Iter { public static IEnumerable<int> Count(int start, int step=1) { int x = start; while (true) { yield return x; x += st</int>…

Python の itertools.cycle を C# で作る

Python の itertools.cycle を C# で作ってみます。 using System; using System.Collections.Generic; using System.Linq; class Iter { public static IEnumerable<T> Cycle<T>(IEnumerable<T> seq) { for (;;) { bool empty = true; foreach (var elt in seq) { yi</t></t></t>…

AOJ で string.Join の第2引数に int[] を渡すとコンパイルエラーになる

AOJ

AOJ で C# コードのコンパイルエラーに関するメモです。 AOJ の mono のバージョンのためか、string.Join の第2に引数に int[] を渡すとコンパイルエラーになるようです。その回避方法です。 // OK static void Display1(int[] xs) { string[] ss = xs.Selec…

Python の itertools.repeat を C# で作る

Python の itertools.repeat を C# で作ってみます。 using System; using System.Collections.Generic; using System.Collections; using System.Linq; class Repeat<T> : IEnumerable<T> { private const int INFINITY = -1; // 無限のときの _times の値 privat</t></t>…

連続する数値でグループ分けする

昇順に並んだ数値を、連続する数値ごとにグループ分けするプログラムです。 using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { int[] xs = { 1, 2, 3, 6, 9, 10, 13, 14, 15, 16, 99, 100 }; var g…

同値関係(Union Find)

結城浩の『Perlクイズ』 [まぐまぐ!] の No.0087 同値関係を求めるクイズをやってみました。 // unionfind.cs using System; using System.Collections.Generic; using System.Linq; // 結城浩の『Perlクイズ』2004-06-18 No.0087 // http://archive.mag2.c…

疑問: string の Split(char) メソッドの定義

string の Split メソッドの引数に文字を渡すと、それを区切り文字として文字列が分割されます。 // split.cs using System; class Program { static void Main() { string s = "hello,C#,world"; foreach (var t in s.Split(',')) { Console.WriteLine(t); …

配列を特定の値で初期化する拡張メソッド

using System; using System.Collections.Generic; using System.Linq; using System.Text; // StringBuilder static class ArrayExtensions { public static void Fill<T>(this T[] xs, T val) { for (int i = 0; i < xs.Length; i++) { xs[i] = val; } } publ</t>…

AA折れ線グラフ

入力は'R','F','C'の3種類の文字からなる長さ1以上の文字列 'R'は上昇を表し,折れ線グラフの要素としては '/' (スラッシュ)1文字に対応 'F'は下降を表し,折れ線グラフの要素としては '\' (バックスラッシュ)1文字に対応 'C'は変化なしを表し,折れ線グラフ…

差し金

差し金の形に 'a' を出力するプログラムです。 using System; using System.Collections.Generic; using System.Linq; class Program { static void Square(int n) { // 左下を (0, 0) とする for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { …

Enumerable.GroupBy メソッドで偶数と奇数に分ける

Enumerable.GroupBy メソッドで、数列を偶数と奇数に分けます。 クエリ式と、メソッド呼び出し構文の 2 通りの方法でプログラムを書いてみました。クエリ式いいですね。 using System; using System.Collections.Generic; using System.Linq; class Program …

Project Euler 56

PE

Problem 56 using System; using System.Collections.Generic; using System.Numerics; using System.Linq; class PE056 { static void Main() { var seq = from a in Enumerable.Range(1, 99) from b in Enumerable.Range(1, 99) let sum = BigInteger.Pow(…

Project Euler 55

PE

Problem 55 using System; using System.Collections.Generic; using System.Numerics; using System.Linq; // コンパイル方法: // $ mcs 055.cs -r:System.Numerics -out:a.exe class PE055 { static bool IsPalindrome(BigInteger n) { var a = n.ToString…

Project Euler 52

PE

Problem 52 6 倍しても桁が変わらないので、最上位の桁は 1 のはず。 using System; using System.Collections.Generic; using System.Linq; class PE052 { static void Main() { for (int i = 1; /* */; i++) { string s = "1" + i; // 6 倍しても桁数が同…

Project Euler 50

PE

Problem 50 using System; using System.Collections.Generic; using System.Linq; class Prime { public static List<int> Sieve(int n) { // n 以下の全ての素数を求める bool[] tab = new bool[n+1]; for (int i = 0; i < tab.Length; i++) tab[i] = true; int</int>…

Project Euler 49

PE

Problem 49 using System; using System.Collections.Generic; using System.Linq; class Prime { public static List<int> Sieve(int n) { // n 以下の全ての素数を求める bool[] tab = new bool[n+1]; for (int i = 0; i < tab.Length; i++) tab[i] = true; int</int>…

同じ数字のみからなる文字列かどうか(正規表現)

文字列が "111" や "888888" のように全て同じ数字であるかを調べるプログラムです。 正規表現を使っています。 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; class Program { static void M…

数値を3桁ごとに区切る正規表現

以下の 3 桁で区切る正規表現が面白かったので、C# でやってみました。 JavaScript - 数値文字列に3桁区切りでカンマを入れる処理 - Qiitaqiita.com using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressi…

パスカルの三角形

パスカルの三角形を求めます。 using System; using System.Collections.Generic; using System.Linq; class PascalTriangle { static int Combination(int n, int k) { if (k == 0 || k == n) return 1; else return Combination(n-1, k) + Combination(n-1…

Enumerable.Select メソッドに 2 引数の関数を渡す

Enumerable.Select メソッドには、2 つの引数を受け取る関数を渡すことができます。2 番目の引数には、要素のインデックスが渡されます。 サンプル 配列の要素とインデックスをペアにしたリストを返すサンプルです。 using System; using System.Collections…

文字列の分割

文字列の分割の練習です。 using System; using System.Text; class StringSplit { static void P(string[] ss) { var sb = new StringBuilder(); sb.Append("["); for (int i = 0; i < ss.Length; i++) { if (i > 0) sb.Append(","); sb.Append(ss[i]); } s…

ぷよぷよを解く

C#

人材募集企画 2011年版: 人生を書き換える者すらいた。 の【問題2】を解いてみました。30 分以上、1 時間未満でした。 using System; using System.Collections.Generic; using System.Linq; class Loc { public int Row { get; private set; } public int …