結果

問題 No.219 巨大数の概算
ユーザー asugen0402asugen0402
提出日時 2019-01-22 13:58:58
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 3,483 bytes
コンパイル時間 403 ms
コンパイル使用メモリ 30,592 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-10-13 17:40:11
合計ジャッジ時間 9,294 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 1 ms
4,352 KB
testcase_06 WA -
testcase_07 AC 0 ms
4,352 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 1 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

// 内部定数
#define D_ROUGH_MAX		40										// 最大概算数

// 内部構造体 - 概算情報
typedef struct Rough {
	double mdVal;												// 値
	long long mlDigit;											// 桁数
} Rough;

// 内部変数
static FILE *szpFpI;											// 入力
static Rough sz1Rough[D_ROUGH_MAX];								// 概算
static int siRCnt;												// 概算数

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

// 桁 - セット
int
fSetDigit(
	Rough *pzpRough				// <I> 概算
)
{
	while (pzpRough->mdVal >= 10.0) {
		pzpRough->mdVal /= 10.0;
		pzpRough->mlDigit++;
	}

	return 0;
}

// 積
int
fMulti(
	Rough *pzpRough1			// <I> 概算1
	, Rough *pzpRough2			// <I> 概算2
	, Rough *pzpRes				// <O> 積
)
{
	pzpRes->mdVal = pzpRough1->mdVal * pzpRough2->mdVal;
	pzpRes->mlDigit = pzpRough1->mlDigit + pzpRough2->mlDigit;

	// 桁 - セット
	fSetDigit(pzpRes);

	return 0;
}

// 結果 - セット
int
fSetRes(
	Rough *pzpRough				// <I> 概算
	, double pdVal				// <I> 加算値
	, char *pcpRes				// <O> 結果
)
{
	char lc1Buf[1024];

	// 値補正
	if (pzpRough->mdVal > 0.1) {
		pdVal -= 0.1;
	}

	// 小数部分
	sprintf(lc1Buf, "%.1lf\n", pzpRough->mdVal + pdVal);

	// 結果 - セット
	sprintf(pcpRes, "%c %c %lld\n", lc1Buf[0], lc1Buf[2], pzpRough->mlDigit);

	return 0;
}

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

	// 入力 - セット
#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

	// テスト列数 - 取得
	int liTCnt;
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	sscanf(lc1Buf, "%d", &liTCnt);

	// 値 - 取得
	for (i = 0; i < liTCnt; i++) {
		long long llA, llB;
		fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
		sscanf(lc1Buf, "%lld%lld", &llA, &llB);

		// 1乗
		long long llMVal = 1;
		sz1Rough[0].mdVal = (double)llA;
		sz1Rough[0].mlDigit = 0;
		siRCnt = 1;
		fSetDigit(sz1Rough);															// 桁 - セット

		// B乗以上までセット
		while (llMVal < llB) {
			llMVal += llMVal;
			fMulti(&sz1Rough[siRCnt - 1], &sz1Rough[siRCnt - 1], &sz1Rough[siRCnt]);	// 積
			siRCnt++;
		}

		// B乗算出
		Rough lzRough;
		lzRough.mdVal = 1.0;
		lzRough.mlDigit = 0;
		while (llB > 0) {
			siRCnt--;
			if (llB >= llMVal) {
				llB -= llMVal;
				fMulti(&lzRough, &sz1Rough[siRCnt], &lzRough);
			}
			llMVal /= 2;
		}

		// 結果 - セット
		fSetRes(&lzRough, 0.0, lc1Out);

		// 結果 - 表示
#ifdef D_TEST
		fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
		if (strcmp(lc1Buf, lc1Out)) {
			fSetRes(&lzRough, 0.1, lc1Out);
			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