求和表达式sum=x+xx+xxx+xxxx+…最后一项为n个x,请给出程序算法步骤及函数实现int sum_nx(int x, int n)其中x的参数范围为0~9,假设不考虑n个x及sum在int范围溢出情况。
#include <iostream>
using namespace std;
int sum_nx(int x, int n)
{
long int sn = 0;
long int tn = 0;
int count = 1;
while (count <= n)
{
tn = tn + x;
sn = sn + tn;
x = x * 10;
++count;
}
return sn;
}
int main()
{
int x;
int n;
cin >> x;
cin >> n;
cout << sum_nx(x, n) << endl;
system("pause");
return 0;
}