---
# System prepended metadata

title: Java_Cmoney_ft9104_計算重複數字
tags: [Cmoney_Java題目]

---

---
tags: Cmoney_Java題目
---
Java_Cmoney_ft9104_計算重複數字
===

![](https://i.imgur.com/A4LWsQc.png)
![](https://i.imgur.com/K1BEv3N.png)


1.主程式
---
先用遞迴寫出重複的數字，再用迴圈將這些重複數字加起來

```java=
import java.util.Scanner;

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

    public static int number(int a, int n) {
        if (n == 1)
            return a;
        return 10 * number(a, n - 1) + a;
    }
}
```
