結果

問題 No.2385 Parse Integer with Radix
ユーザー 👑 obakyanobakyan
提出日時 2023-07-23 21:18:44
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,006 bytes
コンパイル時間 422 ms
コンパイル使用メモリ 5,148 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-25 05:17:32
合計ジャッジ時間 1,090 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,348 KB
testcase_04 AC 3 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 3 ms
4,348 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local q = io.read("*n", "*l")
local function solve()
  local s = io.read()
  if #s == 1 then
    return s
  end
  local head = s:sub(1, 2)
  if head == "0b" then
    local mul = 1LL
    local ans = 0LL
    for i = #s, 3, -1 do
      if s:sub(i, i) == "1" then
        ans = ans + mul
      end
      mul = mul + mul
    end
    ans = tostring(ans):gsub("LL", "")
    return ans
  elseif head == "0o" then
    local mul = 1LL
    local ans = 0LL
    for i = #s, 3, -1 do
      local b = s:byte(i) - 48
      ans = ans + mul * b
      mul = mul * 8LL
    end
    ans = tostring(ans):gsub("LL", "")
    return ans
  elseif head == "0x" then
    local mul = 1LL
    local ans = 0LL
    for i = #s, 3, -1 do
      local b = s:byte(i) - 96
      if b < 0 then
        b = s:byte(i) - 48
      else
        b = s:byte(i) - 87
      end
      ans = ans + mul * b
      mul = mul * 16
    end
    ans = tostring(ans):gsub("LL", "")
    return ans
  else
    return s
  end
end
for iq = 1, q do
  print(solve())
end
0