結果

問題 No.4 おもりと天秤
ユーザー mafuyu-akimafuyu-aki
提出日時 2017-04-13 00:48:56
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 2,548 bytes
コンパイル時間 207 ms
コンパイル使用メモリ 28,036 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-25 13:13:54
合計ジャッジ時間 2,720 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 RE -
testcase_06 AC 2 ms
4,380 KB
testcase_07 RE -
testcase_08 AC 2 ms
4,376 KB
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define READ_BUFSIZE ( 3*100+100+1 )
#define READ_DELIMITER ( " " )


//標準入力取得(1行)

int GetStdin( char* pszStr, int lMaxLen )
{
	int lLen = 0;

    memset( pszStr, 0, lMaxLen );
    if( fgets( pszStr, lMaxLen, stdin ) )
    {
	    lLen = strlen( pszStr );
	    if( lLen >= 1 )
	    {
			if( pszStr[ lLen - 1 ] == 0x0A )
			{
				pszStr[ lLen - 1 ] = 0;
				lLen--;
			}
		}
	}
	
	return( lLen );
}

struct stQ
{
	int index;
	int sum;
};


//標準入力を取得
//区切り文字で区切られている文字列を取り出す
//取り出した文字列のポインタをポインタ配列にセットする (数値型)
//戻り値に合計を返す
//lMaxに最大値を返す
int split(char *pszStr, int lStrMax, const char *pszDelim, int *palOutList, stQ *pQ, int &lMax) 
{
    char *pszToken;
    int count = 0;
    char *pszNext_token = 0;
	int lLen = 0;
	int lNum = 0;
	int lSum = 0;
	lMax = 0;

    memset( pszStr, 0, lStrMax );
    lLen = GetStdin( pszStr, lStrMax );
    
	if( lLen > 0 )
	{

	    pszToken = strtok( pszStr, pszDelim );

	    while ( pszToken != NULL ) 
	    {
            *palOutList = atoi( pszToken );
			pQ->index = lNum;
			pQ->sum = *palOutList;
			lSum += *palOutList;

			if( *palOutList > lMax )
				lMax = *palOutList;
			lNum++;

			//	        pszToken = strtok_s(NULL, pszDelim, &pszNext_token);
			pszToken = strtok( NULL, pszDelim );
			palOutList++;
			pQ++;
	    }
	}
    return lSum;
}




int main(int argc, char *argv[]) 
{
	char szRead[READ_BUFSIZE] = "";
	//char* psOutbuf[10000] = { 0 };
	int lInput[4] = {0};

	GetStdin( szRead, READ_BUFSIZE );
	int N = atoi( szRead );

	int W[100+1] = {0};
	stQ *Q = new stQ[100*1000]; 
	memset( Q, 0, sizeof( stQ )*100*1000 );


	int max = 0;
	int sum = split( szRead, READ_BUFSIZE, READ_DELIMITER, W, Q, max );

	if( sum % 2 || ( max > (sum / 2)  ) )
	{
		printf( "impossible\n" );
		return 0;
	}

	int add = N;
	bool possible = false;

	for( int i = 0; Q[i].sum != 0 && !possible; i++ )
	{
		if( Q[i].sum == (sum / 2) )
		{
			possible = true;
			break;
		}
		for( int j = Q[i].index+1; W[j] !=0; j++ )
		{
			if( ( Q[i].sum + W[j] ) < (sum / 2) )
			{
				Q[add].index = j;
				Q[add].sum = Q[i].sum + W[j];
//printf( "i=%d add=%d Q[add].sum=%d\n", i, add, Q[add].sum );
				add++;
			}

			else if( ( Q[i].sum + W[j] ) == (sum / 2) )
			{
				possible = true;
				break;
			}
		}
	}


	if( possible )
		printf( "possible\n" );
	else
		printf( "impossible\n" );


	return 0;

}

0