ForNext
Only Do What Only You Can Do
065. 標準入力からの入力に現れた数字をカウントする
VBScript
JScript
Perl
PHP
Python
Ruby
PowerShell
Scala
F#
C
更新日 : 2010.10.08
#include <stdio.h> int main(int argc, char* argv[]) { int i, ch; int cnt[10] = {0}; while ((ch = getchar()) != EOF) { switch (ch) { case '0': cnt[0]++; break; case '1': cnt[1]++; break; case '2': cnt[2]++; break; case '3': cnt[3]++; break; case '4': cnt[4]++; break; case '5': cnt[5]++; break; case '6': cnt[6]++; break; case '7': cnt[7]++; break; case '8': cnt[8]++; break; case '9': cnt[9]++; break; } } for (i = 0; i < 10; i++) { printf("'%d' : %d\n", i, cnt[i]); } return 0; }
R:\>lesson065\Project1.exe 3.14159265 ^Z '0' : 0 '1' : 2 '2' : 1 '3' : 1 '4' : 1 '5' : 2 '6' : 1 '7' : 0 '8' : 0 '9' : 1
C++
C++Builder
VC++
C#
Java
更新日 : 2010.11.05
import java.util.Scanner; class Lesson065 { public static void main(String[] args) { int[] cnt = new int[10]; Scanner stdIn = new Scanner(System.in); String str; while (stdIn.hasNext()) { str = stdIn.next(); for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); switch (ch) { case '0': cnt[0]++; break; case '1': cnt[1]++; break; case '2': cnt[2]++; break; case '3': cnt[3]++; break; case '4': cnt[4]++; break; case '5': cnt[5]++; break; case '6': cnt[6]++; break; case '7': cnt[7]++; break; case '8': cnt[8]++; break; case '9': cnt[9]++; break; } } } for (int i = 0; i < 10; i++) System.out.printf("'%d' : %d\n", i, cnt[i]); } }
L:\>java Lesson065 3.14159265 ^Z '0' : 0 '1' : 2 '2' : 1 '3' : 1 '4' : 1 '5' : 2 '6' : 1 '7' : 0 '8' : 0 '9' : 1
Objective-C
D
VB
VB.NET
Delphi
更新日 : 2010.09.24
program Project1; {$APPTYPE CONSOLE} uses SysUtils; procedure main(); var i: Integer; s: String; cnt: array[0..9] of Integer; begin for i := 0 to 9 do cnt[i] := 0; while (not Eof) do begin readln(s); for i := 0 to length(s) do begin case s[i] of '0': inc(cnt[0]); '1': inc(cnt[1]); '2': inc(cnt[2]); '3': inc(cnt[3]); '4': inc(cnt[4]); '5': inc(cnt[5]); '6': inc(cnt[6]); '7': inc(cnt[7]); '8': inc(cnt[8]); '9': inc(cnt[9]); end; end; end; for i := 0 to 9 do writeln(format('"%d" : %d', [i, cnt[i]])); end; begin main; end.
S:\>lesson065\Project1.exe 3.14159265 ^Z "0" : 0 "1" : 2 "2" : 1 "3" : 1 "4" : 1 "5" : 2 "6" : 1 "7" : 0 "8" : 0 "9" : 1