Relative Content

Daily Archives: April 25, 2016

Nim game

Nim game ニム (nim) は、2人で行うレクリエーション数学ゲームの1つである。ルーツは古代中国からあるとされ、16世紀初めの西欧で基本ルールが完成したが、名前については、一般的に1901年にハーバード大学のチャールズ・L.バウトン (Charles L. Bouton) によって名付けられたとされる。 ゲームルール: 一人1個か2個か3個か4個だけ取れて、交互にやっていって、 最後の1個の石を取った人が負けとなります。 #include <stdio.h> int main(void) { int i, stone, n=0, turn=0; char name[2][256]; printf(“Input players’ names\n”); printf(“player 1:”); gets(name[0]); printf(“player 2:”); gets(name[1]); printf(“\nnumber of stones:”); scanf(“%d”, &stone); while (stone > 0){ printf(“\nThere are %d stones.\n”, stone); printf(“%s’s turn! Take some stones:”, name[turn]); while (n […]

11.3 文字列関数

標準ライブラリ関数 : 文字列操作関数 string.hのヘッダファイルをincludeする 主なもの: strcpy(ss,st):文字列(文字型の配列)ssに文字列stをコピーする strlen(st):文字列stの長さを求める strcat(ss,st):文字列ssの後ろに文字列stを連結する strncpy(ss,st,n):文字列ssに文字列stの先頭n文字をコピーする strncat(ss,st,n)文字列ssの後ろに文字列stの先頭n文字を連結する サンプル: #include <stdio.h> #include <string.h> //string.hのインクルードを忘れずに int main(int argc, const char * argv[]) { // insert code here… char str[40]; char alpha1[] = “ABCDE”; char alpha2[] = “VWXYZ”; int length; printf(“alpha1 : %s\n”, alpha1); printf(“alpha2 : %s\n”, alpha2); strcpy(str, alpha1); strcat(str, alpha2); printf(“str : %s\n”, […]