あにす

next >>

島の数をカウントする (Nested Flatten)
これで例の2問は解けました。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//http://ja.doukaku.org/219/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

namespace どう書く_org島の数をカウントする {
    class Program {
        static void Main(string[] args) {
            string map = @"
□□□□
■□■□
□■□□
□□□□
";
            islandCount(map);

            Console.ReadLine();
        }

        static void islandCount(string map_) {
            List<string> map = new List<string>(map_.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
            List<Island> islandList = new List<Island>();
            for(int y = 0; y < map.Count; y++) {
                for(int x = 0; x < map[0].Length; x++) {
                    char cell = map[y][x];
                    bool flag = false;
                    for(int islandIndex = 0; islandIndex < islandList.Count; islandIndex++) {
                        for(int cellIndex = 0; cellIndex < islandList[islandIndex].Count; cellIndex++) {
                            if(isNext(islandList[islandIndex][cellIndex], new Point(x, y)) && map[islandList[islandIndex][0].Y][islandList[islandIndex][0].X] == cell) {
                                islandList[islandIndex].Add(new Point(x, y));
                                flag = true;
                                break;
                            }
                        }
                    }
                    if(!flag) {
                        Island island = new Island();
                        island.Add(new Point(x, y));
                        islandList.Add(island);
                    }
                }
            }

            while(islandListMarge(islandList, map)) { }

            Console.WriteLine("白の島は" + islandList.Count<Island>((x) => (map[x[0].Y][x[0].X] == '□')).ToString() + "つ");
            Console.WriteLine("黒の島は" + islandList.Count<Island>((x) => (map[x[0].Y][x[0].X] == '■')).ToString() + "つ");
        }

        static bool islandListMarge(List<Island> islandList, List<string> map) {
            for(int i = 0; i < islandList.Count; i++) {
                for(int j = 0; j < islandList.Count; j++) {
                    if(i == j) continue;
                    Island a = islandList[i];
                    Island b = islandList[j];
                    if(map[a[0].Y][a[0].X] != map[b[0].Y][b[0].X]) continue;
                    for(int a_ = 0; a_ < a.Count; a_++) {
                        for(int b_ = 0;b_ < b.Count; b_++) {
                            if(isNext(a[a_], b[b_])) {
                                a.AddRange(b);
                                islandList.Remove(b);
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        }

        static bool isNext(Point a, Point b) {
            Point tmpB = b;
            bool r = false;
            for(int i = -1; i <= 1; i++) {
                for(int j = -1; j <= 1; j++) {
                    if(i * j != 0) continue;
                    tmpB.Offset(i, j);
                    if(a == tmpB) r = true;
                    tmpB = b;
                }
            }
            return r;
        }
    }

    class Island :List<Point> {

    }
}
漢数字で九九の表 (Nested Flatten)
こんな感じで出力されました。

 一  二  三  四  五  六  七  八  九
 二  四  六  八 一〇 一二 一四 一六 一八
 三  六  九 一二 一五 一八 二一 二四 二七
 四  八 一二 一六 二〇 二四 二八 三二 三六
 五 一〇 一五 二〇 二五 三〇 三五 四〇 四五
 六 一二 一八 二四 三〇 三六 四二 四八 五四
 七 一四 二一 二八 三五 四二 四九 五六 六三
 八 一六 二四 三二 四〇 四八 五六 六四 七二
 九 一八 二七 三六 四五 五四 六三 七二 八一
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;
using System.Collections.Generic;

class Program {
    static List<char> 漢数字List = new List<char>(new char[] { '〇', '一', '二', '三', '四', '五', '六', '七', '八', '九' });
    
    static void Main(string[] args) {
        char[] To = new char[] { '一', '二', '三', '四', '五', '六', '七', '八', '九' };
        foreach(char i in To) {
            foreach(char j in To) {
                int answer = 漢数字ToInt(i) * 漢数字ToInt(j);
                string answerStr = intTo漢数字(answer);
                Console.Write(answerStr.PadLeft("##".Length, ' ') + ' ');
            }
            Console.WriteLine();
        }
    }

    static int 漢数字ToInt(char 漢数字) {
        return 漢数字List.IndexOf(漢数字);
    }

    static string intTo漢数字(int int_) {
        string r = int_.ToString();
        foreach(char i in int_.ToString()) {
            r = r.Replace(i, 漢数字List[int.Parse(i.ToString())]);
        }
        return r;
    }
}
デバッグ用出力 (Nested Flatten)
出力をIDEで確認出来ます。
1
2
3
4
5
6
class Program {
    static void Main(string[] args) {
        System.Diagnostics.Debug.Print("文字列");
        System.Diagnostics.Debug.Write(new object());
    }
}
タブ区切りデータの処理 (Nested Flatten)
いまいちすっきりと書けません。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using System.Text;
using VB_IO = Microsoft.VisualBasic.FileIO;

class Program {
    static void Main(string[] args) {
        //テーブルの準備
        List<string> headers = new List<string>();
        List<List<string>> table = new List<List<string>>();

        //TSVの読み込みにはMicrosoft.VisualBasic.FileIOTextFieldParserクラスが便利。CSVにも使えるよ。
        //Microsoft.VisualBasicを参照に加えてね。
        using(VB_IO.TextFieldParser tfp = new VB_IO.TextFieldParser(args[0], Encoding.GetEncoding("shift-jis"))) {
            tfp.SetDelimiters("\t");//区切り記号はタブ

            //通常のテキストファイルと同じ感覚で扱えます。
            headers.AddRange(tfp.ReadFields());
            while(!tfp.EndOfData) {
                table.Add(new List<string>(tfp.ReadFields()));
            }

            tfp.Close();
        }

        //第1カラムの値でデータを昇順にソートする。
        table.Sort((a, b) => (int.Parse(a[0]) - int.Parse(b[0])));//何回見ても書いてもラムダ式きもい。

        //第2カラムと第3カラムをヘッダを含めて入れ替える。
        headers.Reverse(1, 2);
        foreach(List<string> row in table){
            row.Reverse(1, 2);
            //第4カラムの値にそれぞれ1を加える。
            row[3] = (int.Parse(row[3]) + 1).ToString();
        }

        //出力
        foreach(string header in headers){
            Console.Write(header + "\t");
        }
        Console.WriteLine();
        foreach(List<string> row in table) {
            foreach(string cell in row) {
                Console.Write(cell + "\t");
            }
            Console.WriteLine();
        }
    }
}
LL Golf Hole 9 - トラックバックを打つ (Nested Flatten)

今回は短くしなくて良いのですね。 こういう遊び心のあるお題、好きです。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;

class Program {
    static void Main(string[] args) {
        //接続の作成
        WebClient webClient = new WebClient();
        webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

        //送信パラメータ作成
        NameValueCollection postValues = new NameValueCollection();
        postValues.Add("title",
            "LL Golf Hole 9 - トラックバックを打つ");
        postValues.Add("excerpt",
            "あにすです。面白いお題の数々、楽しませて頂きました。");
        postValues.Add("url",
            "http://ja.doukaku.org/207/");
        postValues.Add("blog_name",
            "どう書く?org");

        //トラックバック送信
        byte[] r = webClient.UploadValues("http://ll.jus.or.jp/2008/blog/archives/38/trackback", postValues);

        //テスト用
        //byte[] r = webClient.UploadValues("http://d.hatena.ne.jp/takano32/20080905", postValues);

        //後始末
        webClient.Dispose();

        //レスポンス確認
        Console.WriteLine(Encoding.UTF8.GetString(r));
        Console.ReadLine();
    }
}
文字列型日時ののN秒後時間取得 (Nested Flatten)
date = "2008/08/27 23:59:25"
addSeconds = 40
> "2008/08/28 00:00:05"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;

class Program {
    static void Main(string[] args) {
        Console.WriteLine(
            DateEx(Console.ReadLine(), long.Parse(Console.ReadLine()))
            );
        
        Console.WriteLine(
            DateEx(long.Parse(Console.ReadLine()))
            );
        
        Console.ReadLine();
    }

    static string DateEx(string date, long addSeconds) {
        DateTime d;
        DateTime.TryParse(date, out d);
        if(d == null) throw new ArgumentException("日付が正しくありません。", "date");
        return d.AddSeconds((double)addSeconds).ToString();
    }

    static string DateEx(long addSeconds) {
        return DateEx(DateTime.Now.ToString(), addSeconds);
    }
}
LL Golf Hole 5 - 最上位の桁を数え上げる (Nested Flatten)
こんな感じで。
1
using System;class P{static void Main(string[]a){for(int i=0;i<=int.Parse(a[0]);i++)if(i%Math.Pow(10,i.ToString().Length-1)==0)Console.WriteLine(i);}}
LL Golf Hole 4 - 文章から単語の索引を作る (Nested Flatten)
どうやっても短くならないです。
1
using System;using System.Collections.Generic;using System.IO;using System.Net;class P{static void Main(){char[]c=new char[]{' ',',','.','\'',';',':','-','?','\n','\r','(',')','/','\\','<','>','\"'};Dictionary<string,List<int>>d=new Dictionary<string,List<int>>();StreamReader s=new StreamReader(WebRequest.Create("http://www.gnu.org/licenses/gpl.txt").GetResponse().GetResponseStream());for(int l=1;!s.EndOfStream;l++){foreach(string t in s.ReadLine().Split(c)){string w=t.Trim().ToLower();if(w!="")if(d.ContainsKey(w)){if(!d[w].Contains(l))d[w].Add(l);}else d.Add(w,new List<int>(new int[]{l}));}}List<string>k=new List<string>(d.Keys);k.Sort((x,y)=>{int r=d[y].Count-d[x].Count;if(r==0){r=d[x][0]-d[y][0];}return r;});foreach(string m in k){Console.WriteLine("<<"+m+">>"+" "+d[m].Count+"回");foreach(int u in d[m]){Console.Write(u.ToString()+", ");}Console.WriteLine("\n");}Console.ReadLine();}}
LL Golf Hole 3 - 13日の金曜日を数え上げる (Nested Flatten)
おぉ!これがやりたかったんですよ。
勉強になりました。

実はLINQも試したのですが、長くなったので投稿しませんでした。 LINQで抽出する対象のDateTimeのコレクションを作る為に結局ループを回しちゃってるんですよね。

1
using System;using System.Collections.Generic;using System.Linq;class C{static void Main(){List<DateTime>l=new List<DateTime>();for(DateTime d=DateTime.Now;d<=new DateTime(2013,12,31);d=d.AddDays(1)){l.Add(d);}var v=from f in l where(int)(f.DayOfWeek)==5&f.Day==13select f.Date;foreach(var d in v){Console.WriteLine(d);}Console.Write(v.Count<DateTime>());}}
C#は捻らずに素直に書いた方が短くなりますね。
1
using System;class P{static void Main(){DateTime d=DateTime.Now;int c=0;for(;d<=new DateTime(2013,12,31);d=d.AddDays(1)){if(d.Day==13&(int)(d.DayOfWeek)==5){Console.WriteLine(d);c++;}}Console.Write(c);}}
tailの実装 (Nested Flatten)
tailの仕様がわかりません。
検索すると概要はわかるのですが、実際にどういった動作をさせれば良いのでしょう?
UNIX環境が無いのでどうしたらよいのか…。
LL Golf Hole 2 - 文字列に含まれる単語の最初の文字を大文字にする (Nested Flatten)
でも、こっちの方が短かった。
1
using System;class P{static void Main(string[]a){for(int i=0;i<a[0].Length;i++){if(i!=0&&a[0][i-1]!=' ')Console.Write(a[0][i]);else Console.Write(a[0][i].ToString().ToUpper());}}}
がんばってみました。
1
using System;using System.Linq;class d{static void Main(string[]a){int c=0;a[0].ToCharArray().ToList<char>().ForEach(b=>{if(c!=0&&a[0][c-1]!=' ')Console.Write(b);else Console.Write(b.ToString().ToUpper());c++;});}}
単語と単語の間には空白があるって前提でいいのでしょうか。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;

class Program {
    static void Main(string[] args) {
        Console.WriteLine(ToUpper1st1(args[0]));
        Console.WriteLine(ToUpper1st2(args[0]));
        Console.ReadLine();
    }

    //こっちの方が短いけど、' 'が2文字続いたときにstr.Substring(1)が通らない
    static string ToUpper1st1(string value) {
        string r = "";
        foreach(string str in value.Split(new char[] { ' ' })) {
            r += str[0].ToString().ToUpper() + str.Substring(1) + " ";
        }
        return r;
    }

    //こっちはだいぢょぶ。でも、3項演算子って読みにくいですよね。
    static string ToUpper1st2(string value) {
        string r = "";
        for(int i = 0; i < value.Length; i++) {
            r += (i != 0) && (value[i - 1] != ' ') ? value.Substring(i, 1) : value.Substring(i, 1).ToUpper();
        }
        return r;
    }
}
2次元ランダムウォーク (Nested Flatten)
コマンドライン引数で次元数とログファイルのパスを指定するようにしました。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.IO;

class Program {
    static void Main(string[] args) {
        RandomWalk rndwalk = new RandomWalk(int.Parse(args[0]));

        while(true) {
            if(Console.ReadLine() != "end") {
                foreach(int i in rndwalk.Tic()) {
                    Console.Write(i.ToString() + " ");
                }
                Console.WriteLine();
            } else {
                break;
            }
        }

        rndwalk.WriteLog(Environment.CurrentDirectory + "\\" + args[1]);

        using(StreamReader sr = new StreamReader(Environment.CurrentDirectory + "\\" + args[1])) {
            while(!sr.EndOfStream) {
                Console.WriteLine(sr.ReadLine());
            }
        }
        Console.ReadLine();
    }
}

class RandomWalk {
    int rank;
    List<int[]> log = new List<int[]>();
    Random rnd = new Random();
    int[] direction = new int[] { -1, 1 };

    public RandomWalk(int rank) {
        this.rank = rank;
        log.Add(new int[rank]);
    }

    public int[] Tic() {
        int changeRank = rnd.Next(0, rank);
        int[] pre = log[log.Count - 1];
        int[] ticedPoint = new int[rank];

        for(int i = 0; i < rank; i++) {
            if(i == changeRank) {
                ticedPoint[i] = pre[i] + direction[rnd.Next(0, 2)];
            } else {
                ticedPoint[i] = pre[i];
            }
        }

        log.Add(ticedPoint);

        return log[log.Count - 1];
    }

    public void WriteLog(string fileName) {
        using(StreamWriter sw = new StreamWriter(fileName)) {
            for(int time = 0; time < log.Count; time++) {
                int[] point = log[time];
                sw.Write(time + " ");
                foreach(int i in point) {
                    sw.Write(i.ToString() + " ");
                }
                sw.WriteLine();
            }
            sw.Close();
        }
    }
}
環境変数の取得 (Nested Flatten)
System.Environment.GetEnvironmentVariableメソッドで指定したキーの環境変数の値を取得出来ます。
System.Environment.GetEnvironmentVariablesメソッドで環境変数のキーと値がペアになったSystem.Collections.IDictionaryを取得出来ます。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
using System;

class Program {
    static void Main(string[] args) {
        //キーから取得
        Console.Write("環境変数を取得します。キーを入力して下さい。:");
        string key = Console.ReadLine();
        Console.WriteLine(Environment.GetEnvironmentVariable(key));

        //一覧を表示
        Console.Write("環境変数の一覧を表示します。");
        Console.ReadLine();
        foreach(string key_ in Environment.GetEnvironmentVariables().Keys) {
            Console.WriteLine("{0} = {1}", key_, Environment.GetEnvironmentVariables()[key_]);
        }
        Console.ReadLine();
    }
}
クリップボードへの転送 (Nested Flatten)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
using System;

class Program {
    [STAThread]//クリップボードを利用する場合に必須の属性です。
    static void Main(string[] args) {
        Console.Write("クリップボードのセットする文字列を入力して下さい。:");
        string setStr = Console.ReadLine();//セットする文字列を入力

        System.Windows.Forms.Clipboard.SetText(setStr);//クリップボードにセット

        string getStr = System.Windows.Forms.Clipboard.GetText();//クリップボードから文字列を取得

        Console.WriteLine("クリップボードの内容は\n\n{0}\n\nです。", getStr);
        Console.ReadLine();
    }
}
比較しないソートの作成 (Nested Flatten)
horiuchiさんの#6648をコピペしてC#のコードに改変しました。
殆ど一緒です。行数まで一致しています。
JavaとC#って似てますよね。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;

public class Sample187 {
    public static int[] countingSort(int min, int max, int[] data) {
        int[] counter = new int[max - min + 1];
        foreach(int i in data) {
            counter[i - min]++;
        }

        int[] result = new int[data.Length];
        int index = 0;
        for(int i = 0; i < counter.Length; i++) {
            for(int count = 0; count < counter[i]; count++) {
                result[index++] = i + min;
            }
        }
        return result;
    }

    public static void Main(String[] args) {
        int[] sort = countingSort(-1, 10, new int[] { -1, 9, 4, 8, 9, 6, 3, 9, 5, 2 });
        foreach(int i in sort) Console.Write(i + " ");
    }
}
設定ファイルから値を取得 (Nested Flatten)
WinAPIを叩いてINIファイルを読み書きするコードも挙げようと思ったのですが、
誰かがC辺りで書いてくれるのを期待します。
.NETでWinAPI使うとコードが読みにくくなるんですよね。