結果

問題 No.3 ビットすごろく
ユーザー 4885rhkA4885rhkA
提出日時 2015-07-06 06:58:55
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,729 bytes
コンパイル時間 557 ms
コンパイル使用メモリ 53,484 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-22 09:04:37
合計ジャッジ時間 1,599 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

//
//  main.cpp
//  Q11
//
//  Created by AkihiroKOBAYASHI on 7/5/15.
//  Copyright (c) 2015 Akhr5884. All rights reserved.
//

#include <iostream>

using namespace std;

string to_binString(unsigned int val){
    if( !val ) {
        return string("0");
    }
    string str;
    while( val != 0 ) {
        if( (val & 1) == 0 )  // val は偶数か?
            str.insert(str.begin(), '0');  //  偶数の場合
        else
            str.insert(str.begin(), '1');  //  奇数の場合
        val >>= 1;
    }
    return str;
}

int canGoValue(string binString) {
    int i;
    int value = 0;
    for(i = 0; i < binString.size(); i++) {
        if(binString[i] == '1') {
            value++;
        }
    }
    return value;
}

int main(int argc, const char * argv[]) {
    using namespace std;
    int goalvalue;
    int *goalvalueArray;
    int i = 0;
    int add;
    int count;
    
    cin >> goalvalue;
    goalvalueArray = (int*)malloc(sizeof(goalvalueArray) * (goalvalue + 1));
    
    while(i <= goalvalue) {
        goalvalueArray[i] = 0;
        i++;
    }
    
    count = 1;
    i = 1;
    goalvalueArray[1] = 1;

    while (1) {
        if(i == goalvalue) {
            cout << count << "\n";
            return 0;
        }
        add = canGoValue(to_binString(i));
        count++;
        if(i + add <= goalvalue){
            i += add;
            goalvalueArray[i] = 1;
        }
        else if(i + add > goalvalue) {
            i -= add;
            if(goalvalueArray[i] == 1) {
                count = -1;
                cout << count << "\n";
                return 0;
            }
            else {
                goalvalueArray[i] = 1;
            }
        }
    }
    
    return 0;
}
0