0
0

More than 1 year has passed since last update.

AtCoder Beginner Contest 008をやった(Java)

Posted at

AtCoder Beginner Contest 008をやった。

A

差分を出してあげて、ひと工夫すれば終わります。

import java.util.*;

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

        int num1 = scan.nextInt();
        int num2 = scan.nextInt();
        System.out.println((num2 - num1) + 1);

    }
}

B

重複文字はkey : Valueでカウントを重ねればおのずと結果に導かれます。

import java.util.*;

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

        String[] strIndex = new String[Integer.parseInt(scan.nextLine())];
        for(int i = 0; i < strIndex.length; i++){
            strIndex[i] = scan.nextLine();
        }

        Map<String, Integer> map = new HashMap<String, Integer>();

        for(String str : strIndex){
            int v;
            if(map.containsKey(str)){
                v = map.get(str) + 1;    
            } else {
                v = 1;
            }
            map.put(str, v);
        }

        int max = 0;
        String S = "";
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if(max < entry.getValue()){
                S = entry.getKey();
                max = entry.getValue();
            }
        }        
        System.out.println(S);
    }
}


いつか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