【算法010】求s=a+aa+aaa+aaaa+aa...a的值

求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制

package com.example.chyer.demo;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        int n = input.nextInt();
        int sum = 0, b = 0;
        for (int i = 0; i < n; i++) {
            b += a;
            sum += b;
            a = a * 10;
        }
        System.out.println(sum);
    }
}

运行结果:

发布了29 篇原创文章 · 获赞 5 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/chyercn/article/details/103245405