結果

問題 No.2385 Parse Integer with Radix
ユーザー おかゆおかゆおかゆおかゆ
提出日時 2023-07-27 22:12:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 967 bytes
コンパイル時間 2,006 ms
コンパイル使用メモリ 168,480 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 08:41:28
合計ジャッジ時間 2,385 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

long long convert(string s, int mul)
{
	long long a = 0;
	long long b = 1;
	for (int i = 0; i < s.size(); i++)
	{
		if (s[s.size() - i - 1] >= 'a' && s[s.size() - i - 1] <= 'f')
		{
			a += b * (10 + s[s.size() - i - 1] - 'a');
		}
		else
		{
			a += b * (s[s.size() - i - 1] - '0');
		}

		b *= mul;
	}
	// cout << a << endl;
	return a;
}

int main()
{
	int Q;
	cin >> Q;
	vector<string> S(Q);
	for (int i = 0; i < Q; i++)
	{
		cin >> S[i];
	}
	for (int i = 0; i < Q; i++)
	{
		if (S[i].size() < 2)
		{
			cout << S[i] << endl;
			continue;
		}
		string header = S[i].substr(0, 2);
		string num = S[i].substr(2);
		// cout << header << endl;
		long long ans;
		if (header == "0b")
		{
			ans = convert(num, 2);
		}
		else if (header == "0o")
		{
			ans = convert(num, 8);
		}
		else if (header == "0x")
		{
			ans = convert(num, 16);
		}
		else
		{
			cout << S[i] << endl;
			continue;
		}
		cout << ans << endl;
	}
}
0