Head First C#
頭とからだで覚えるC#の基本
(オライリージャパン)
Andrew Stellman (著), Jennifer Green (著)
佐藤 嘉一 (監修), 木下 哲也 (翻訳)
開発環境
- Microsoft Windows 8.1 Pro (VMware Fusion 6, OS X Mavericks - Apple) (OS)
- C# (プログラミング言語)
- Microsoft Visual Studio Express 2013 for Windows Desktop (統合開発環境, IDE)
Head First C#―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Green (著)、佐藤 嘉一 (監修)、木下 哲也 (翻訳)、オライリージャパン)の11章(LINQ: データの管理)、プールパズル(p.485)を解いてみる。
プールパズル(p.485)
コード
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Line[] lines = {
new Line(new String[]{"eating", "carrots,", "but", "enjoy", "Horses"}, 1),
new Line(new String[]{"zebras?", "hay", "Cows", "bridge. ", "bolted"}, 2),
new Line(new String[]{"fork", "dogs!", "Engine", "and"}, 3),
new Line(new String[]{"love", "they", "apples.", "eating"}, 2),
new Line(new String[]{"whistled. ", "Bump"}, 1)
};
var groups =
from line in lines
group line by line.value
into wordGroups
orderby wordGroups.Key
select wordGroups;
var twoGroups = groups.Take(2);
foreach (var group in twoGroups)
{
int i = 0;
foreach (var inner in group)
{
i++;
if (i == group.Key)
{
var poem =
from word in inner.words
orderby word descending
select word + " ";
foreach (var word in poem)
{
Console.Write(word);
}
}
}
}
Console.WriteLine();
}
}
class Line
{
public string[] words;
public int value;
public Line(string[] words, int value)
{
this.words = words;
this.value = value;
}
}
}
出力結果(コマンドプロンプト)
Horses enjoy eating carrots, but they love eating apples. 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿