2015-01-01から1年間の記事一覧

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

昇順に並んだ数値を、連続する数値ごとにグループ分けするプログラムです。 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 …

2次元配列の練習

2次元配列の練習。配列の長さの取得方法を調べてみました。 using System; // http://msdn.microsoft.com/ja-jp/library/2yd9wwz4.aspx class MultidimensionalArrays { static void Main() { int[,] xs = new int[2, 6]; Console.WriteLine(xs.Length); // …