简答题
偏难
#13180
233求和 题目描述 有一个序列,前几项如下: 23,233,2333,23333,… 之后的数字以此类推。 给定正整数n,请输出这个序列的前 n 项和。 输入…
233求和
题目描述
有一个序列,前几项如下:
23,233,2333,23333,…
之后的数字以此类推。
给定正整数n,请输出这个序列的前 n 项和。
输入格式
单个正整数:表示n。
输出格式
单个正整数:表示序列的前 n 项和。
输入样例
3
输出样例
2589
说明提示
1≤n≤17
答案与解析
正确答案:<pre class="brush:cpp;toolbar:false">//C代码
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
long long sum = 0;
long long current = 23;
for (int i = 0; i < n; i++) {
sum += current;
current = current * 10 + 3;
}
printf("%lld\n", sum);
return 0;
}</pre><p><br/></p><pre class="brush:cpp;toolbar:false">//C++代码
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n;
cin >> n;
long long sum = 0;
long long term = 23; // 序列的第一项
for (int i = 0; i < n; i++) {
sum += term;
term = term * 10 + 3; // 生成下一项:当前项乘以10再加3
}
cout << sum << endl;
return 0;
}</pre>
这道题暂未补解析。