Calculate the value of n digits

Calculate the value of n digits

S n = a + a a + a a a + a a ⋯ a ( n 个 a ) S_n=a+aa+aaa+aa\cdots a\left( n\text{个}a \right) Sn=a+aa+aaa+aaa( N- th a ) value, where a is a number, represented by a n-bit number, for example:
2 + 22 + 222 + 22222 + 2222 (where n = 5)

Enter two integers a and n.
The calculated value of Sn
Please pay attention to the output line break at the end of the line.

enter

2 5

Output

24690
//n位数的值
#include<iostream>
#include<cmath>			//数学函数头文件
using namespace std;
int main(void)
{
    
    
    int a,n;
    long ans=0,temp;		//ans值可能较大,用long或long long存储
    cin>>a>>n;
    for (int i=1;i<=n;i++)
    {
    
    
        for (int j=i;j>=1;j--)
        {
    
    
            temp=a*pow(10,j-1);		//幂函数
            ans+=temp;
        }
    }
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45830912/article/details/114095914