2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AtCoder Beginner Contest 336 (バチャ) 振り返り

Posted at

今回は家庭の事情で参加できなかったので、toyboot4eさんにバチャを作成してもらいました。toyboot4eさんありがとうございます!
https://kenkoooo.com/atcoder/#/contest/show/27c015a7-380c-4485-b22a-750873666b24

※前回の記事はこちら
https://qiita.com/flowerhill/items/ce81230c32675dd31cc3

今回の結果

今回は危なげなくA,B,C 3完でした。本番も参加できてたらなあ。

スクリーンショット 2024-01-17 2.41.06.png

各問題の振り返り

A - Long Loong

文字列を'L''ng'の間に指定された回数'o'を連結する方法です。replicateを使って繰り返し文字列を連結します。

main :: IO ()
main = do
  n <- readInt

  putStrLn $ "L" ++ replicate n 'o' ++ "ng"

B - CTZ

2進数に変換して、末尾に0が何個続くかをカウントする問題です。
2で割って余が1になるまでの数をカウントします。

main :: IO ()
main = do
  n <- readInt

  print $ cntDigitZero n

cntDigitZero :: Int -> Int
cntDigitZero n = go n 0
  where
    go l count = if r == 0 then go q (count + 1) else count
      where
        (q, r) = l `divMod` 2

C - Even Digits

(0,2,4,6,8) のみが登場する数字で、小さい値からN番目の値を求める問題です。実質的に5進数なので、Bと同様に5で割って行きます。
5で割った余をindexとして使い、[0, 2, 4, 6, 8]から値を取得した値がその桁の値になります。
こうして取得した値を元の値の頭に追加して行くのですが、計算して足すのが面倒なので文字列で頭に連結するやり方にしました。

こうして解いた回答が以下です。

main :: IO ()
main = do
  n <- readInt

  putStrLn $ solve n

solve :: Int -> String
solve n = init (go (n - 1) "0")
  where
    go :: Int -> String -> String
    go l ans = if l < 5 then conc (lst !! l) ans else go q $ conc (lst !! r) ans
      where
        (q, r) = l `divMod` 5
        lst = [0, 2, 4, 6, 8]
        conc m n = show m ++ n

全体を振り返って

今回はn進数への変換がメインだったのかなーという印象でした。
それほど難しくなかった印象なので、どこかでD問題をやりたいと思ってます。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?