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

paizaラーニング データセット選択メニュー 動的配列 Java 解答

Posted at

STEP: 1 ランダムアクセス

問題

解答

step1.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        sc.nextLine();
        for(int i = 1; i <= m; i++){
            if(i == m){
                System.out.println(sc.nextInt());
            } else {
                sc.nextInt();
            }
        }
        sc.close();
    }
}

結果

image.png

STEP: 2 複数回のランダムアクセス

問題

解答

step2.java
import java.util.*;


public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        
        // 改行
        sc.nextLine();
        
        List<Integer> list = new ArrayList<Integer>();
        for(int i = 0; i < n; i++){
            list.add(sc.nextInt());
        }
        
        // 改行
        sc.nextLine();
        
        int q = sc.nextInt();
        
        for(int i = 0; i < q; i++){
            System.out.println(list.get(sc.nextInt()-1));
        }
        
        sc.close();
    }
}

結果

image.png

STEP: 3 最大値と最小値

問題

解答

step3.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        
        int max = Integer.max(a,b);
        max = Integer.max(max,c);
        
        int min = Integer.min(a,b);
        min = Integer.min(min,c);

        System.out.println(max-min);
        
    }
}

※sortを使うともっとスマートだった

結果

image.png

STEP: FINAL問題 動的配列

問題

解答

final.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int q = sc.nextInt();
        
        List<Integer> list = new ArrayList<Integer>();
        for(int i = 0; i < n; i++){
            list.add(sc.nextInt());
        }
        
        for(int i = 0; i < q; i++){
            manipulateList(sc, list);
        }
    }
    
    /**
     * リストの操作
     * 
     * push_back x : list の末尾に x を追加する
     * pop_back : list の末尾を削除する
     * print : list を半角スペース区切りで1行に出力する
     * 
     * @param sc 操作内容
     * @param list 操作したリスト
     */
    private static void manipulateList(Scanner sc, List<Integer> list){
        switch(sc.nextInt()){
            case 0:
                //0 x は push_back x
                int x = sc.nextInt();
                list.add(x);
                break;
            case 1:
                // 1 は pop_back
                list.remove(list.size() - 1);
                break;
            case 2:
                // 2 は print
                for(int i = 0; i < list.size(); i++){
                    if(i != list.size()-1){
                        System.out.print(list.get(i) + " ");
                    }else{
                        System.out.println(list.get(i));
                    }
                }

                break;
        }
    } 
    
}

結果

image.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?