0
0

More than 1 year has passed since last update.

AtCoder Beginner Contest 011をやった(Java)

Last updated at Posted at 2021-12-26

AtCoder Beginner Contest 011をやった。

C問題できませんでした、、、こちらでも投稿しましたが、どなたかご助力いただけませんか。。。
C問題できたら追記します。
C問題追記しました!

A

場合分けにより実装。

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        if(N == 12){
            System.out.println(1);
        } else {
            System.out.println(N + 1);
        }
    }
}

B

1文字目とそのほかを抜き出して、問題文に沿う形で変更してあげればよい

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        String S = scan.nextLine();
        String str1 = S.substring(0, 1);
        String str2 = S.substring(1, S.length());

        System.out.println(str1.toUpperCase().concat(str2.toLowerCase()));
    }
}

C

条件を分けて考える問題。
以下、3つの条件がfalseになります。
・3連番かつ与えられる一番大きい数字とNを比較して、Nのほうが大きい場合、
・3連番かつNが3連番に含まれている状態、
・Nが298以上の場合で、100回回したときにNの値が0以下にならなかった場合

C問題は、Twitterにて助けてもらいました。

スパークリング黒ココアさん
image.png

hamamuさん
image.png

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();

        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(scan.nextInt());
        list.add(scan.nextInt());
        list.add(scan.nextInt());
        Collections.sort(list, Collections.reverseOrder());

        boolean flg = true;
        if(Math.decrementExact(list.get(0)) == list.get(1) && Math.decrementExact(list.get(1)) == list.get(2)){
            if(list.get(0) < N){
                flg = false;
            } else if(list.get(0) == N || list.get(1) == N || list.get(2) == N){
                flg = false;
            }
        } else if(list.get(0) == N || list.get(1) == N || list.get(2) == N){
            flg = false;
        } else if(N >= 298){
            for(int i = 1; i <= 100; i++){
                N = N - 3;
                if(N <= 0){
                    flg = true;
                    break;
                } else if(N >= 0){
                    if(i == 100){
                        flg = false;
                        break;
                    }
                }                        
                if(N == list.get(0) || N == list.get(1) || N == list.get(2)){
                    N = N + 1;
                    if(N == list.get(0) || N == list.get(1) || N == list.get(2)){
                        N = N + 1;
                    }
                }
            }
        }

        if(flg == true){
            System.out.println("YES");
        } else if(flg == false){
            System.out.println("NO");
        }
    }
}
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