結果

問題 No.212 素数サイコロと合成数サイコロ (2)
ユーザー asugen0402asugen0402
提出日時 2019-01-18 11:32:26
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 764 ms / 5,000 ms
コード長 3,538 bytes
コンパイル時間 1,018 ms
コンパイル使用メモリ 30,592 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-19 06:20:44
合計ジャッジ時間 2,356 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 18 ms
4,380 KB
testcase_05 AC 764 ms
4,380 KB
testcase_06 AC 108 ms
4,380 KB
testcase_07 AC 108 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// 内部変数
static FILE *szpFpI;											// 入力
static int siPCnt;												// 素数サイコロ数
static int siCCnt;												// 合成数サイコロ数
static int si1PVal[6];											// 素数サイコロ
static int si1CVal[6];											// 合成数サイコロ
static long long slSum;											// 積合計
static long long slCnt;											// 積数

// 内部変数 - テスト用
#ifdef D_TEST
	static int siRes;
	static FILE *szpFpA;
#endif

// 積合計 - 加算
int
fAddSum(
	int *pipPSel				// <I> 素数選択内容
	, int *pipCSel				// <I> 合成数選択内容
)
{
	int i;

	// 積
	long long llVal = 1;

	// 素数選択内容
	for (i = 0; i < siPCnt; i++) {
		llVal *= pipPSel[i];
	}

	// 合成数選択内容
	for (i = 0; i < siCCnt; i++) {
		llVal *= pipCSel[i];
	}

	// 積合計 - 加算
	slSum += llVal;

	// 積数
	slCnt++;

	return 0;
}

// 合成数サイコロ - 選択
int
fSelCVal(
	int piCCnt					// <I> 合成数選択数
	, int *pipCSel				// <I> 合成数選択内容
	, int *pipPSel				// <I> 素数選択内容
)
{
	int i;

	// 選択終了
	if (piCCnt == siCCnt) {
		fAddSum(pipPSel, pipCSel);			// 積合計 - 加算
		return 0;
	}

	// 選択
	for (i = 0; i < 6; i++) {
		pipCSel[piCCnt] = si1CVal[i];
		fSelCVal(piCCnt + 1, pipCSel, pipPSel);
	}

	return 0;
}

// 素数サイコロ - 選択
int
fSelPVal(
	int piPCnt					// <I> 素数選択数
	, int *pipPSel				// <I> 素数選択内容
)
{
	int i;

	// 選択終了
	if (piPCnt == siPCnt) {
		int li1Sel[6];
		fSelCVal(0, li1Sel, pipPSel);		// 合成数サイコロ - 選択
		return 0;
	}

	// 選択
	for (i = 0; i < 6; i++) {
		pipPSel[piPCnt] = si1PVal[i];
		fSelPVal(piPCnt + 1, pipPSel);
	}

	return 0;
}

// 実行メイン
int
fMain(
	int piTNo					// <I> テスト番号 1~
)
{
	char lc1Buf[1024], lc1Out[1024];

	// データ - 初期化
	slSum = 0;													// 積合計
	slCnt = 0;													// 積数

	// 入力 - セット
#ifdef D_TEST
	sprintf(lc1Buf, ".\\Test\\T%d.txt", piTNo);
	szpFpI = fopen(lc1Buf, "r");
	sprintf(lc1Buf, ".\\Test\\A%d.txt", piTNo);
	szpFpA = fopen(lc1Buf, "r");
	siRes = 0;
#else
	szpFpI = stdin;
#endif

	// サイコロ数 - 取得
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	sscanf(lc1Buf, "%d%d", &siPCnt, &siCCnt);

	// 素数サイコロ
	si1PVal[0] = 2;
	si1PVal[1] = 3;
	si1PVal[2] = 5;
	si1PVal[3] = 7;
	si1PVal[4] = 11;
	si1PVal[5] = 13;

	// 合成数サイコロ
	si1CVal[0] = 4;
	si1CVal[1] = 6;
	si1CVal[2] = 8;
	si1CVal[3] = 9;
	si1CVal[4] = 10;
	si1CVal[5] = 12;

	// 素数サイコロ - 選択
	int li1Sel[6];
	fSelPVal(0, li1Sel);

	// 期待値 - 取得
	double ldVal = (double)slSum / (double)slCnt;

	// 結果 - セット
	sprintf(lc1Out, "%.11lf\n", ldVal);

	// 結果 - 表示
#ifdef D_TEST
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, lc1Out)) {
		siRes = -1;
	}
#else
	printf("%s", lc1Out);
#endif

	// 残データ有無
#ifdef D_TEST
	lc1Buf[0] = '\0';
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, "")) {
		siRes = -1;
	}
#endif

	// テストファイルクローズ
#ifdef D_TEST
	fclose(szpFpI);
	fclose(szpFpA);
#endif

	// テスト結果
#ifdef D_TEST
	if (siRes == 0) {
		printf("OK %d\n", piTNo);
	}
	else {
		printf("NG %d\n", piTNo);
	}
#endif

	return 0;
}

int
main()
{

#ifdef D_TEST
	int i;
	for (i = D_TEST_SNO; i <= D_TEST_ENO; i++) {
		fMain(i);
	}
#else
	fMain(0);
#endif

	return 0;
}

0