Skip to content

Instantly share code, notes, and snippets.

@draqoon
Last active August 29, 2015 14:22
Show Gist options
  • Save draqoon/ee7a63708e01ef8e94ac to your computer and use it in GitHub Desktop.
Save draqoon/ee7a63708e01ef8e94ac to your computer and use it in GitHub Desktop.
//Paiza Lerning 単語のカウント (paizaランク C 相当)
//https://paiza.jp/learning/word-count
using System;
using System.Collections.Generic;
public class Hello{
public static void Main(){
//標準入力から1行読み込む
string line = Console.ReadLine();
//半角スペースで単語の配列に分割する
string[] parts = line.Split( ' ' );
//単語リストを保存するList変数を初期化
List<string> words = new List<string>();
//単語の出現数を保存するDictionary変数を初期化
Dictionary<string,int> wordCount = new Dictionary<string,int>();
//単語の配列を1件毎にループして処理
for( int i = 0; i < parts.Length; i++ ) {
if( words.Contains( parts[i] ) ) {
//単語リストに既に追加済みの場合は
//単語の出現数を+1
wordCount[parts[i]] += 1;
}
else {
//単語リストに未登録の場合は、
//単語リストに登録
words.Add( parts[i] );
//単語の出現数を1に設定
wordCount.Add( parts[i], 1 );
}
}
//単語リストと単語の出現数を参照し、標準出力へ出力
for( int i = 0; i < words.Count; i++ ) {
Console.WriteLine( "{0} {1}", words[i], wordCount[words[i]] );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment