题解 | #数字阶梯求和#
数字阶梯求和
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)