LoginSignup
0
0

More than 1 year has passed since last update.

AtCoder229をやった(Java)

Last updated at Posted at 2021-11-28

AtCoder229をやった。
AとB問題の解答例だけみたけど、やっぱり自分はまだまだだなとしみじみと思う。

A

【#(黒)】と【.(白)】の場合分け問題
"辺"としてみたときに隣り合っていたら、"Yes"を違う場合は"No"を出力する。
全パターン列挙でもよい。
私の場合は、全パターン列挙に近いが、途中で場合分けをした。
例えば、以下のYesの入力例
1)  2)  3)  4)
##  #.  #.  ##
##  #.  ##  ..
1と2はリスト型のHashSet()を使えば重複削除により、1ラインとして扱うことができます。
その場合は【#】が含まれていれば"Yes"です。

4は、1と2以外として扱うため、上下が逆でも"Yes"になります。

最後に3は、1と2以外として扱うけど、【#】の個数をカウントして3以上だと"Yes"を出すロジックとなっています。

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>();

        while (scan.hasNextLine()) {
            String str = scan.nextLine();
            list.add(str);
        }

        Set<String> set = new HashSet<String>(list);
        int cnt = 0;
        if(set.size() == 1){
            if(list.get(0).split("")[0].equals("#") || list.get(0).split("")[1].equals("#")){
                System.out.println("Yes");
            }else{
                System.out.println("No");
            }
        } else {
            if(list.get(0).split("")[0].equals("#") && list.get(0).split("")[1].equals("#") ||
                list.get(1).split("")[0].equals("#") && list.get(1).split("")[1].equals("#")
            ){
                System.out.println("Yes");
            } else {
                for(int i = 0; i < list.size(); i++){
                    if(list.get(i).split("")[0].equals(".")){
                        cnt++;
                    }
                    if(list.get(i).split("")[1].equals(".")){
                        cnt++;
                    }
                }
                if(cnt >= 2){
                    System.out.println("No");    
                }
            }
        }
    }
}

B

数値として考えると大変なので、文字列として考えてあげるのがよさそう。
桁違いの場合、特に桁多い側はその分1~9の値でくるので、それは繰り上がりしないし、
事前に排除してループを回してあげればいいような気がします。
※コメントは後で付けました。気にしないでください。

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 str = scan.nextLine();
        list.add(str);

        String[] strIndex = new String[2];
        boolean flg = true;
        for(int i = 0; i < 2; i++){
            strIndex[i] = list.get(0).split(" ")[i];    
        }

        /* 桁数が同じの場合 ex) 328 + 620
        * else if 一つ目の桁数が二つ目の桁数より小さい場合 ex) 28 + 620
        * else if 一つ目の桁数が二つ目の桁数より大きい場合 ex) 1328 + 620
        */
        if(strIndex[0].length() == strIndex[1].length()){
            for(int i = 0; i < strIndex[0].length(); i++){
                int cnt = Integer.parseInt(strIndex[0].substring(i, i+1)) + Integer.parseInt(strIndex[1].substring(i, i+1)); 
                if(cnt >= 10){
                    flg = false;
                    break;
                }
            }
        } else if(strIndex[0].length() < strIndex[1].length()) {
            int strLength = strIndex[1].length() - strIndex[0].length();
            strIndex[1] = strIndex[1].substring(strLength, strIndex[1].length());
            for(int i = 0; i < strIndex[0].length(); i++){
                int cnt = Integer.parseInt(strIndex[0].substring(i, i+1)) + Integer.parseInt(strIndex[1].substring(i, i+1)); 
                if(cnt >= 10){
                    flg = false;
                    break;
                }
            }
        } else if(strIndex[0].length() > strIndex[1].length()) {
            int strLength = strIndex[0].length() - strIndex[1].length();
            strIndex[0] = strIndex[0].substring(strLength, strIndex[0].length());
            for(int i = 0; i < strIndex[0].length(); i++){
                int cnt = Integer.parseInt(strIndex[0].substring(i, i+1)) + Integer.parseInt(strIndex[1].substring(i, i+1)); 
                if(cnt >= 10){
                    flg = false;
                    break;
                }
            }
        }

        if(flg){
            System.out.println("Easy");
        } else {
            System.out.println("Hard");   
        }
    }
}

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