1
0

More than 1 year has passed since last update.

AtCoder Beginner Contest 006をやった(Java)

Posted at

AtCoder Beginner Contest 006をやった。

B問題だから簡単!ということではなくて、今回のB問題は緑相当でしたし・・・
C問題も緑だし、、C問題は解説見たけど意識が遠のいていく感じがしたのでまた今度で()

A

FizzBuzzの問題です。プログラム学び始めたときはお世話になりました。。。
いろんな実装方法があるかと思いますが、僕は剰余で。

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner scan = new Scanner(System.in);
        ArrayList<Integer> list = new ArrayList<Integer>();

        list.add(scan.nextInt());

        if(list.get(0) % 3 == 0){
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
}

B

正直、自力で解けなかったので解説見ました。
解説に書いてあることが本当かを紙に書きながらあっているか確認して、、、(アナログ)
例えば、27項目を生成するときは
(list.get(26-3) + list.get(26-2) + list.get(26-1)) % 10007
とそれぞれの項に、都度10007の剰余をした和は上の計算式と同じになるか、、、みたいな。
小学校の分配法則を理解できていなかった主です(泣)。

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner scan = new Scanner(System.in);
        ArrayList<Long> list = new ArrayList<Long>();

        int scInt = scan.nextInt();
        list.add(0L);
        list.add(0L);
        list.add(1L);

        if(scInt <= 3){
            System.out.println(list.get(scInt - 1) % 10007);  
        } else {
            for(int i = 3; i <= scInt; i++){
                list.add(i, ((list.get(i-3) % 10007) + (list.get(i-2) % 10007) + (list.get(i-1) % 10007)));
                if(list.get(i) >= 10007){
                  list.set(i, list.get(i) % 10007);
                }
            }
            System.out.println(list.get(scInt - 1));
        }
    }
}


1
0
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
1
0