114-2 OOP B組 Week08 實習課練習 Problem1


Submit solution

Points: 100 (partial)
Time limit: 5.0s
Memory limit: 98M

Author:
Problem type
Allowed languages
Java 19, Java 8

題目說明

使用者輸入一個正整數n,請撰寫一個遞迴副程式計算n的每一個位數的總和。


題目要求

  • 必須使用「遞迴」實作(不可使用迴圈)。
  • 不可將數字轉成字串處理。
  • 需正確處理單一位數(base case)。

參考下方程式碼作答:

import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.close();
        int sum = f(n);
        System.out.println(sum);
    }

    static int f(int n) {

    }
}

測資範圍

\[0 <= N <= 999999999\]


範例輸入/輸出

範例輸入1

1234

範例輸出1

10

範例輸入2

6

範例輸出2

6

範例輸入3

32453446

範例輸出3

31

Comments

There are no comments at the moment.