Array Multiplier Accumulator


Submit solution

Points: 10 (partial)
Time limit: 1.0s
Memory limit: 64M

Authors:
Problem type
Allowed languages
Java 19, Java 8

題目說明

開發者需要設計一個程式處理 Array 的數值運算。 程式的執行流程如下

  1. 讀取一個大於零的 Integer n 代表 Array 的長度。

  2. 讀取 n 個 Integer 並依序存入 Array。

  3. 讀取三個 Integer 分別代表三個倍數 m1 與 m2 以及 m3。

  4. 呼叫 Function amplifyAndPrint 進行計算。

計算邏輯為對 Array 中的每個 Element 分別計算其與 m1 與 m2 以及 m3 之乘積的加總。若 Element 為 x 則計算結果為 x * m1 + x * m2 + x * m3。計算完成後需依序輸出結果。

開發者必須在給定的樣板程式中完成 amplifyAndPrint 函數的實作。除了該 Function 內部的代碼之外不可修改程式的其他部分。

樣板程式如下

import java.util.Scanner;

public class ArrayProcessor {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 步驟 1:輸入陣列長度與元素
        int n = scanner.nextInt();
        int[] numbers = new int[n];

        int i = 0;
        while (i < n) {
            numbers[i] = scanner.nextInt();
            i = i + 1;
        }

        // 步驟 2:輸入三個放大器倍數
        int a1 = scanner.nextInt();
        int a2 = scanner.nextInt();
        int a3 = scanner.nextInt();

        // 步驟 3:呼叫函數進行計算與輸出
        amplifyAndPrint(numbers, a1, a2, a3);

        scanner.close();
    }

    public static ... amplifyAndPrint(...) {
        ...
    }
}

輸入值的格式

  • 第一行包含一個正 Integer n。

  • 第二行包含 n 個以空格分隔的 Integer 代表 Array 的內容。

  • 第三行包含三個以空格分隔的 Integer 分別代表 m1 與 m2 以及 m3。

輸出值的格式

  • 輸出一行包含 n 個由計算結果組成的 Integer 且每個數值之間以單個空格分隔。

  • 輸出的末尾包含一個換行符號。

各種需要注意的邊界條件

  1. 請考慮 Array 長度為 1 的極小化情況。

  2. 請考慮 Array 中的 Element 可能為 0 的情況。

  3. 請考慮倍數可能包含 0 或負數的情況。

  4. 輸入值與計算結果皆可能為負 Integer。

  5. 請確保輸出格式完全符合範例要求且數值之間僅有空格。

sample input1

3
10 10 30
2 3 5

sample output1

100 100 300

解釋:

10 * 2 + 10 * 3 + 10 * 5 = 100

10 * 2 + 10 * 3 + 10 * 5 = 100

30 * 2 + 30 * 3 + 30 * 5 = 300

sample input2

1
50
1 1 2

sample output2

200

Comments

There are no comments at the moment.