結果

問題 No.2385 Parse Integer with Radix
ユーザー HIcoderHIcoder
提出日時 2023-07-21 21:54:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 964 bytes
コンパイル時間 757 ms
コンパイル使用メモリ 87,628 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 21:55:27
合計ジャッジ時間 1,475 ms
ジャッジサーバーID
(参考情報)
judge12 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<tuple>
using namespace std;
typedef long long ll;

ll f(string s,int base){
    int n=s.size();
    
    ll res=0;
    
    for(int i=0;i<n;i++){
        res*=base;

        if(base==16 && 'a'<=s[i] && s[i]<='f'){
            res+=(int)(s[i]-'a'+10);
        }else{
            res+=(int)(s[i]-'0');
        }
    }

    return res;
}

int main(){
    int Q;
    cin>>Q;

    vector<ll> output;

    for(int q=0;q<Q;q++){
        string S;
        cin>>S;

        ll ans=0;
        string s=S.substr(0,2);
        if(s=="0b"){
            ans=f(S.substr(2),2);

        }else if(s=="0o"){
            ans=f(S.substr(2),8);

        }else if(s=="0x"){
            ans=f(S.substr(2),16);

        }else{
            ans=f(S,10);
        }
        output.push_back(ans);
        //cout<<ans<<endl;
    }

    for(ll v:output){
        cout<<v<<endl;
    }
}
0