1. 程式人生 > >java經典演算法_008求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數字

java經典演算法_008求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數字

題目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數字。
例如2+22+222+2222+22222(此時共有5個數相加),幾個數相加有鍵盤控制。  
package wzs.arithmetics;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

// 題目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數字。
// 例如2+22+222+2222+22222(此時共有5個數相加),幾個數相加有鍵盤控制。   
public class Test_wzs8
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String _temp = br.readLine();
        int temp = Integer.valueOf(_temp);

        int newNumber = 0; // 每次生成的新數
        int total = 0; // 計算結果
        for (int i = 0; i < temp; i++)
        {
            newNumber = newNumber * 10 + temp;
            System.out.println(newNumber);
            total = total + newNumber;
        }
        System.out.println("計算結果:" + total);
    }
}