7-78 Summation of special series a (20 points)

7-78 Summation of special series a (20 points)
Given two positive integers a and n, both of which are not more than 9, it is required to write a program to find the sum of a+aa+aaa++⋯+aa⋯a (n a).

Input format:
Input gives positive integers a and n not exceeding 9 in one line.

Output format:
output in the format of "s = corresponding sum" in one line.

Input samples:
2 3
Output samples:
s = 246

Here, if the sum is set to double type, the final output digits must be controlled, otherwise there will be a test point error. It is also correct if it is set to int type, but I have some doubts, because if it is 9 bits, it is beyond the range of int. .

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
/* 给定两个均不超过9的正整数a和n,要求编写程序求
a+aa+aaa++..+aa..a(n个a)之和。*/
int main(){
    
    
	double sum=0,t=0;
	int n,a;
	cin>>a>>n;
	for(int i=0;i<n;i++){
    
    
		t=t+a*pow(10,i);
		sum=sum+t;
	}
	cout<<"s = "<<fixed<<setprecision(0)<<sum;
	return 0;
	
}

Guess you like

Origin blog.csdn.net/weixin_45534301/article/details/112621810