Generation

generate functionSat, 18 Mar 2023

配列 A の要素数 N と新たに作成する配列のサイズ n , 配列 A の各要素 A_1 ... A_N が与えられるので、配列 A の先頭から n 要素を順に保持する配列を作成してください。新たに作成する配列の要素数が A の要素数よりも大きい時は、サイズが合うように 0 を A の要素の末尾に追加してください。

public static void main(String[] args) { List<Integer> listA = new ArrayList<>(); List<Integer> listB = new ArrayList<>(); Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int n = sc.nextInt(); for(int i = 0; i < N; i++) { listA.add(sc.nextInt()); } for(int i = 0; i < n; i++) { listB.add(listA.get(i)); } int size = listB.size(); if(size < n) { int diff = n - size; while(diff > 0) { listB.add(0); diff--; } } for(int i = 0; i < listB.size(); i++) { System.out.print(listB.get(i)); if(i != listB.size() - 1) { System.out

Questions about programming?Chat with your personal AI assistant