結果

問題 No.36 素数が嫌い!
ユーザー mafuyu-akimafuyu-aki
提出日時 2017-04-30 23:40:57
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,544 bytes
コンパイル時間 300 ms
コンパイル使用メモリ 34,524 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-12 00:04:51
合計ジャッジ時間 7,743 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

#define READ_BUFSIZE ( 1024 )
#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 );
}


//標準入力を取得
//区切り文字で区切られている文字列を取り出す
//取り出した文字列のポインタをポインタ配列にセットする  (文字列型)
int split(char *pszStr, int lStrMax, const char *pszDelim, char *apszOutList[]) {
    char *pszToken;
    int count = 0;
    char *pszNext_token = 0;
	int lLen = 0;
	int lNum = 0;

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

	    pszToken = strtok( pszStr, pszDelim );

	    while ( pszToken != NULL ) 
	    {
            apszOutList[ lNum ] = pszToken;
			lNum++;
//	        pszToken = strtok_s(NULL, pszDelim, &pszNext_token);
		    pszToken = strtok( NULL, pszDelim );
	    }
	}
    return lNum;
}

//Nが素数かどうかチェックする関数
bool isSosu(int N)
{
	//#include <math.h>

	if( N == 0 || N == 1) // 1 は素数ではない
		return false;

	if( N == 2 )
		return true;
		
	if( N % 2 == 0 ) // 2より大きい数の偶数は素数ではない
		return false;

	bool isSosu = true;
	//for( int i = 3; i < N; i +=2 )
	for( int i = 3; i <= sqrt((float)N); i +=2 )  //チェックするのは平方根まで
	{
		if( N % i == 0 )
		{
			isSosu = false;
			break;
		}
	}
	return( isSosu );
}


int main(int argc, char *argv[]) 
{
	char szRead[READ_BUFSIZE] = "";
	//int OutList[100] = { 0 };
	char* pOutList[2] = {0};

	split( szRead, READ_BUFSIZE, READ_DELIMITER, pOutList );
	long long  N = strtoul( pOutList[0], 0 , 0 ); 
	//long long  N = strtoull( pOutList[0], 0 , 0 ); 


	if( N == 1 || isSosu( N ) )
	{
		printf( "NO\n" );
	}
	else
	{
		if( N % 2 == 0 ) //偶数
		{
			if( isSosu( N / 2 ) )//2で割った値が素数ならば
			{
				printf( "NO\n" );
			}
			else //2で割った値が素数でなければ
			{
				printf( "YES\n" );
			}
		}
		else // 素数以外の奇数
		{

			for( long long i=3; i < N / 3; i+=2 )
			{
				if( N % i == 0 )
				{
					if( isSosu( i ) )
						printf( "NO\n" );
					else
						printf( "YES\n" );
					break;
				}
			}
		}


	}

	return 0;

}

0