0
0

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 009 をやった(Java)

Last updated at Posted at 2021-12-22

AtCoder Beginner Contest 009をやった。

##A
3個の段ボールだとして両手で持った時何回往復が必要かを実装してください

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        
        if(N == 1 || N == 2){
            System.out.println(1);
        } else {
            if(N % 2 == 0){
                System.out.println(N / 2);    
            } else {
                System.out.println(N / 2 + 1);
            }            
        }
    }
}

##B
Set使ってます。
Listをつかって重複排除してもいいですが、Setのほうが利便性も高いので。
重複排除した後はsortしてあげて問題文の通り出力です。

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        
        Set<Integer> set = new HashSet<Integer>();
        for(int i = 0; i < N; i++){
            set.add(scan.nextInt());
        }
        
        ArrayList<Integer> list = new ArrayList<Integer>(set);
        Collections.sort(list);

        System.out.println(list.get(list.size()-2));
    }
}


いつかC問題もJavaで解きたいな(n回目)。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?