プログラミング入門 宿題 2017.10.30

Back


1と2が出る確率が3割ずつで,それ以外は1割ずつというインチキサイコロを2回振って出た目を表示するプログラムを作成せよ.

My student number: s174099

Doctored dice: 2 6

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

なお,結果の出力では必ず最初の行に自分の学生番号を,最後にハイフンによるラインをつけること.無い場合には減点するので注意.

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

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


解答例

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

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

main()
{
	srand((unsigned) time(NULL));
	
	int d1, d2, dice1, dice2;
	
	printf("My student number: s174099\n\n");
	
	d1 = rand() % 10;
	d2 = rand() % 10;
	
	if(d1<=2)
		dice1 = 1;
	else if(d1<=5)
		dice1 = 2;
	else
		dice1 = d1 - 3;
	
	if(d2<=2)
		dice2 = 1;
	else if(d2<=5)
		dice2 = 2;
	else
		dice2 = d2 - 3;
	
	printf("Doctored dice: %d %d\n", dice1, dice2);
	
	printf("\n--------------------------\n\n");
	
	return(0);
}


Back