1
0

More than 1 year has passed since last update.

AtCoder Beginner Contest 013をやった(Java)

Posted at

AtCoder Beginner Contest 013 をやった。
今年もよろしくおねがいします!
今年は頑張って緑に入りたいです!!まだ茶色にすらいないですが。。。
※C問題のちに追記予定。

A

単純に場合分けをしてあげた。

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        if(str.contains("A")){
            System.out.println(1);
        } else if(str.contains("B")){
            System.out.println(2);
        } else if(str.contains("C")){
            System.out.println(3);
        } else if(str.contains("D")){
            System.out.println(4);
        } else if(str.contains("E")){
            System.out.println(5);
        }
    }
}

B

もうちょっと考えればもっといいロジック浮かびそうだけど、とりあえず小さな方に10を足してあげて差分がどちらのほうが小さいかを判定してあげればよし。

import java.util.*;

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

        int ans = 0;
        int bignum = 0;
        if(a < b){
            ans = b - a;
            bignum = a + 10;
            if(ans <= bignum - b){
                System.out.println(ans);
            } else {
                System.out.println(bignum - b);
            }
        }else if(b < a){
            ans = a - b;
            bignum = b + 10;
            if(ans <= bignum - a){
                System.out.println(ans);
            } else {
                System.out.println(bignum - a);
            }
        }
    }
}

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

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