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 3 years have passed since last update.

愚直にでも解けないことはないが計算量が増えるのは愚策、みたいな問題なので「そうそうこれこれそれっぽい」とnoob丸出しなことを言う。

まず愚直版

Main.java
import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        
        int score = 0;
        for(int i=0; i<n; i++){
            score += sc.nextInt();
            System.out.println(score);
        }
    }
}

キャプチャ_3.PNG
0.63秒……かかってるなあ。(チケットないので入力は見られてない)
というかお手本でもこの方法なので「これでいいんじゃね?」感はある。

(計算式だけを見て)累積和でやってみた。

Main.java
import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        
        int[] a, b;
        a = new int[n];
        b = new int[n+1];
        
        b[0] = 0;
        
        for(int i=0; i<n; i++){
            a[i] = sc.nextInt();
        }
        
        for(int i=1; i<=n; i++){
            b[i] = b[i-1] + a[i-1];
        }
        
        for(int i=1; i<b.length; i++){
            System.out.println(b[i]);
        }
    }
}

キャプチャ_3.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?