题解 | #数字阶梯求和#

数字阶梯求和

https://www.nowcoder.com/practice/c66df29f6c1c4dbba0bd51619210e082

//C++版代码
#include <iostream>
using namespace std;
int main() {
    int a, n;
    while (cin >> a >> n) {
        string sum;
        int carry = 0;
        for (int i = n; i >= 1; i--) {
            int num = a * i + carry;
            sum = to_string(num % 10) + sum;
            carry = num / 10;
        }
        cout << sum << endl;
    }
    return 0;
}
//Java版代码
import java.math.BigInteger;
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();
        BigInteger sum = BigInteger.ZERO, num = BigInteger.ZERO;
        for (int i = 0; i < n; i++) {
            num = num.multiply(BigInteger.TEN).add(BigInteger.valueOf(a));
            sum = sum.add(num);
        }
        System.out.println(sum);
    }
}
//Python版代码
a, n = map(int, input().split())
sum = num = 0
for i in range(n):
    num = num * 10 + a
    sum += num
print(sum)

全部评论

相关推荐

Hello_WordN:咱就是说,除了生命其他都是小事,希望面试官平安,希望各位平时也多注意安全
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务