結果

問題 No.219 巨大数の概算
ユーザー asugen0402asugen0402
提出日時 2019-01-22 14:07:22
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 15 ms / 1,500 ms
コード長 3,561 bytes
コンパイル時間 146 ms
コンパイル使用メモリ 30,760 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-13 17:40:20
合計ジャッジ時間 4,088 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
4,352 KB
testcase_01 AC 15 ms
4,348 KB
testcase_02 AC 15 ms
4,348 KB
testcase_03 AC 15 ms
4,348 KB
testcase_04 AC 11 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 0 ms
4,348 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 0 ms
4,348 KB
testcase_10 AC 15 ms
4,348 KB
testcase_11 AC 15 ms
4,352 KB
testcase_12 AC 15 ms
4,348 KB
testcase_13 AC 15 ms
4,348 KB
testcase_14 AC 15 ms
4,352 KB
testcase_15 AC 15 ms
4,348 KB
testcase_16 AC 15 ms
4,348 KB
testcase_17 AC 15 ms
4,348 KB
testcase_18 AC 15 ms
4,348 KB
testcase_19 AC 15 ms
4,348 KB
testcase_20 AC 15 ms
4,348 KB
testcase_21 AC 15 ms
4,352 KB
testcase_22 AC 15 ms
4,348 KB
testcase_23 AC 15 ms
4,348 KB
testcase_24 AC 15 ms
4,352 KB
testcase_25 AC 15 ms
4,352 KB
testcase_26 AC 14 ms
4,348 KB
testcase_27 AC 14 ms
4,352 KB
testcase_28 AC 15 ms
4,348 KB
testcase_29 AC 15 ms
4,348 KB
testcase_30 AC 15 ms
4,352 KB
testcase_31 AC 15 ms
4,352 KB
testcase_32 AC 14 ms
4,352 KB
testcase_33 AC 15 ms
4,352 KB
testcase_34 AC 15 ms
4,352 KB
testcase_35 AC 15 ms
4,348 KB
testcase_36 AC 15 ms
4,348 KB
testcase_37 AC 15 ms
4,348 KB
testcase_38 AC 15 ms
4,348 KB
testcase_39 AC 15 ms
4,352 KB
testcase_40 AC 14 ms
4,352 KB
testcase_41 AC 15 ms
4,352 KB
testcase_42 AC 15 ms
4,352 KB
testcase_43 AC 15 ms
4,352 KB
testcase_44 AC 15 ms
4,348 KB
testcase_45 AC 14 ms
4,348 KB
testcase_46 AC 15 ms
4,352 KB
testcase_47 AC 15 ms
4,352 KB
testcase_48 AC 15 ms
4,348 KB
testcase_49 AC 14 ms
4,348 KB
testcase_50 AC 15 ms
4,348 KB
testcase_51 AC 0 ms
4,348 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 > 1.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)) {
				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