結果

問題 No.67 よくある棒を切る問題 (1)
ユーザー asugen0402
提出日時 2019-01-25 16:17:36
言語 C
(gcc 13.3.0)
結果
AC  
実行時間 59 ms / 5,000 ms
コード長 2,676 bytes
コンパイル時間 556 ms
コンパイル使用メモリ 28,288 KB
実行使用メモリ 8,608 KB
最終ジャッジ日時 2025-03-03 11:21:13
合計ジャッジ時間 2,811 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function ‘fMain’:
main.c:75:9: warning: ignoring return value of ‘fgets’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   75 |         fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:80:17: warning: ignoring return value of ‘fscanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   80 |                 fscanf(szpFpI, "%d", &si1Tree[i]);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:82:9: warning: ignoring return value of ‘fgets’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   82 |         fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:85:9: warning: ignoring return value of ‘fgets’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   85 |         fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

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

// 内部定数
#define D_TREE_MAX		200000									// 最大木数
#define D_LEN_MAX		1000000000								// 最大木の長さ
#define D_TRY_CNT		100										// 試行回数

// 内部変数
static FILE *szpFpI;											// 入力
static int si1Tree[D_TREE_MAX];									// 木
static int siTCnt;												// 木数
static long long slSCnt;										// 同一長の木の数
static double sdLen;											// 木の長さ

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

// 木の長さ - セット
int
fSetLen(
	double *pdpMin				// <IO> 木の長さ - 下限
	, double *pdpMax			// <IO> 木の長さ - 上限
)
{
	int i;

	// 木の長さ
	sdLen = (*pdpMin + *pdpMax) / 2.0;

	// 本数合計
	long long llSum = 0;
	for (i = 0; i < siTCnt; i++) {
		llSum += (long long)((double)si1Tree[i] / sdLen);
	}
	if (llSum >= slSCnt) {			// OK
		*pdpMin = sdLen;
	}
	else {							// NG
		*pdpMax = sdLen;
	}

	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

	// 木数 - 取得
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	sscanf(lc1Buf, "%d", &siTCnt);

	// 木 - 取得
	for (i = 0; i < siTCnt; i++) {
		fscanf(szpFpI, "%d", &si1Tree[i]);
	}
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);

	// 同一長の木の数 - 取得
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	sscanf(lc1Buf, "%lld", &slSCnt);

	// 木の長さ - セット
	double ldMin = 0.0;
	double ldMax = (double)D_LEN_MAX;
	for (i = 0; i < D_TRY_CNT; i++) {
		fSetLen(&ldMin, &ldMax);
	}

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

	// 結果 - 表示
#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