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);
 }
}


java每日小算法(8),布布扣,bubuko.com

java每日小算法(8)

上一篇:Laravel队列任务超时后不退出,导致队列挂起的故障分析(curl)


下一篇:java学习记录(4):ActionListener