2
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.

AtCoder004をやった(Java)

Last updated at Posted at 2021-12-16

AtCoder004をやった。
C問題のちに追記予定。。。間に合わない、いろいろと。
C問題追記(12/17)

##A
借金の算出方法を参考に実装

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>();
        
        System.out.println(scan.nextInt() * 2);
    }
}

##B
180度回転させたときの状態をイメージまたは、書き出してみると簡単かもしれない
競プロで初めてStringBuilder使った。

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>();
        
        for(int i = 0; i < 4; i++){
            StringBuilder sb = new StringBuilder();
            sb.append(scan.nextLine());
            sb.reverse();
            list.add(i, sb.toString());
        }
    
        System.out.println(list.get(3));
        System.out.println(list.get(2));
        System.out.println(list.get(1));
        System.out.println(list.get(0));
    }
}

##C
123456って文字列を入力値に基づいて差し替える。
例えば、15って入力値が与えられたら、456123これが正当
入力値は10^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>();
        
        int num = scan.nextInt() % 30;
        String str = "";
        if(num >= 0 && num < 5){
            str = "123456";     
        }else if(num >= 5 && num < 10){
            str = "234561";
        }else if(num >= 10 && num < 15){
            str = "345612";
        }else if(num >= 15 && num < 20){
            str = "456123";
        }else if(num >= 20 && num < 25){
            str = "561234";
        }else if(num >= 25 && num < 30){
            str = "612345";
        }

        char[] ch = new char[6];
        if(num % 5 == 0){
            System.out.println(str);
        } else {
            for(int i = 0; i < str.length(); i++){
                ch[i] = str.charAt(i);
            }
            // 初期化
            str = "";
            // 出力用(組み替えるところまで)
            for(int i = 0; i < num % 5; i++){
                str = str.concat(String.valueOf(ch[i + 1]));    
            }
            // 出力用(残った文字)
            for(int i = num % 5; i < ch.length - 1; i++){
                if(num % 5 == i){
                    str = str.concat(String.valueOf(ch[0]));    
                }
                str = str.concat(String.valueOf(ch[i + 1]));    
            }
            System.out.println(str);
        }
    }
}


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

2
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
2
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?