首页 > 代码库 > java每日小算法(8)

java每日小算法(8)

/*【程序8】
题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。  */
package test;
import java.util.Scanner;
public class test {
     public static int sum(int number, int input) {
         int result = 0;
         int tem = number;
         for(int i = 0; i < input; i++)
         {
             for(int j = i; j > 0; j--)
             {
                 tem = tem + (int) Math.pow(10, j)*number;
             }
             result += tem;
             tem = number;
         }
         return result;
     }
          
      
 public static void main(String[] args) {
     long a = System.currentTimeMillis();
     int number = 2;
     Scanner s = new Scanner(System.in);
     System.out.println("Please input the number count:");
     int input = s.nextInt();
     int sum = sum(number, input);
     System.out.println("sum is "+sum);
     System.out.println(System.currentTimeMillis() - a);
 }
}