题解 | 数字阶梯求和
数字阶梯求和
https://www.nowcoder.com/practice/c66df29f6c1c4dbba0bd51619210e082
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string add(string a,string b){
string res;
int carry=0;
int x=a.size()-1;
int y=b.size()-1;
while(x>=0&&y>=0){
int num=a[x]-'0'+b[y]-'0'+carry;
res+=(num%10+'0');
carry = num/10;
x--;
y--;
}
while(x>=0){
int num = a[x]-'0'+carry;
res+=(num%10+'0');
carry = num/10;
x--;
}
while(y>=0){
int num = b[y]-'0'+carry;
res+=(num%10+'0');
carry = num/10;
y--;
}
if(carry>0) res+=(carry+'0');
reverse(res.begin(),res.end());
return res;
}
int main() {
string a;
int n;
cin>>a>>n;
string res;
for(int i=1;i<=n;i++){
string str;
for(int j=0;j<i;j++){
str+=a;
}
res=add(res,str);
}
cout<<res;
return 0;
}


字节跳动公司福利 1371人发布