結果

問題 No.481 1から10
ユーザー Sylvanas_ship3Sylvanas_ship3
提出日時 2017-04-03 14:40:29
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,143 bytes
コンパイル時間 638 ms
コンパイル使用メモリ 69,032 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 11:28:05
合計ジャッジ時間 1,537 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include <array>
#include <cmath>

#define DEBUG

int getInputNumber( const std::string& );
std::vector<std::string> split( const std::string&, std::string );

int main()
{
    std::cin.sync_with_stdio( false );
    std::cin.tie( 0 );

    // get input from stdin
    std::string input;
    std::array<int, 9> sequence = {};
    if ( std::getline( std::cin, input ) )
    {
        std::vector<std::string> argv = split( input, " " );
        if ( argv.size() == 9 ) 
        {
            for ( unsigned int i = 0; i < argv.size(); ++i )
            {
                sequence[i] = getInputNumber(argv[i]);
            }
        }
        else
        {
            return -1;
        }
    }

    // precondition : target is sorted
    int least = sequence[0];
    int count = 0;
    int dif = 0;
    for ( unsigned int i = 1; i < sequence.size(); ++i )
    {
        int result = least + i + dif;
        if ( sequence[i] != result )
        {
            dif += sequence[i] - result;
            count++;
        }
    }
    std::cout << count << std::endl;

    return 0;
}

int getInputNumber( const std::string& input )
{
    int input_number = 0;

    try
    {
        input_number = std::stoi( input );
    }
    catch ( std::invalid_argument& )
    {
        return 0;
    }
    catch ( std::out_of_range& )
    {
        return 0;
    }

    return input_number;
}

std::vector<std::string> split( const std::string& str, std::string delim )
{
    std::vector<std::string> result;
    result.reserve( 8 );

    std::string::size_type pos = 0;
    while ( pos != std::string::npos )
    {
        std::string::size_type current = str.find( delim, pos );
        if ( current == std::string::npos )
        {
            result.push_back( str.substr( pos ) );
            break;
        }
        else
        {
            std::string tmp = str.substr( pos, current - pos );
            if ( !tmp.empty() )
            {
                result.push_back( tmp );
            }
        }

        pos = current + delim.length();
    }

    return result;
}
0