情報科学演習 宿題 2014.12.15

Back


11月10日の演習問題 iii で作成したプログラムを改良して,じゃんけんをあいこになった場合でも決着がつくまで自動で行なうプログラムを while 文を用いて作成せよ.

My student number: s144099

You: Choki
Computer: Choki
Aiko

You: Choki
Computer: Choki
Aiko

You: Pa
Computer: Choki
Winner is Computer!

---------------------------------

解答用紙を使用する際には,まず,科目名の不要な方を削除してください.また,学生番号と名前の記入も忘れないでください.さらに,解答用紙自体がC言語のプログラムとなっていますので, cc コマンドを実行して,コンパイルエラーの無いことを確認してから提出してください.

指定の解答用紙を使用していない,コンパイルエラーが出る,実行時に警告が出る,学生番号と名前が無い,というような答案は提出されても採点しません.注意してください.


解答例

/* ************************************************** */
/*                                                    */
/*      プログラミング入門  情報科学演習C7                            */
/*      レポート課題                                  */
/*      2014.12.15                                    */
/*                                                    */
/* ************************************************** */
/*                                                    */
/*      学生番号:                                    */
/*                                                    */
/*      氏名:                                        */
/*                                                    */
/* ************************************************** */
/*                                                    */
/*      この行以降に解答のプログラムを書くこと        */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

main()
{
	srand((unsigned) time(NULL));

	int hand1, hand2, judge = 0;

	printf("My student number: s144099\n\n");
	
	while(judge==0){
		hand1 = rand() % 3 + 3;
		hand2 = rand() % 3;
		judge = (hand1 - hand2) % 3;
		
		
		if(hand1==5)	
			printf("You: Gu\n");
		else if(hand1==4)
			printf("You: Choki\n");
		else
			printf("You: Pa\n");
		
		if(hand2==2)	
			printf("Computer: Gu\n");
		else if(hand2==1)
			printf("Computer: Choki\n");
		else
			printf("Computer: Pa\n");

		if(judge==1)
			printf("Winner is you!\n\n");
		else if(judge==2)
			printf("Winner is Computer!\n\n");
		else
			printf("Aiko\n\n");
	}
	
	printf("---------------------------------\n\n");
	
	return(0);
}


Back