C語言特殊a串數列求和

2020-08-14 19:09:36

特殊a串數列求和

特殊a串數列求和 給定兩個均不超過9的正整數a和n,要求編寫程式求a+aa+aaa++⋯+aa⋯a(n個a)之和。
輸入格式:
輸入在一行中給出不超過9的正整數a和n。
輸出格式:
在一行中按照「s = 對應的和」的格式輸出。
輸入樣例:
2 3
輸出樣例:
s = 246

#include "stdio.h"
#include "math.h"
void spite()
{
	int a,m=0,n,i,s=0,t;
	scanf("%d%d",&a,&n);
	if(a<=9&&n<=9)
	{
		for(i=1; i<=n; i++)
		{
			t=a*pow(10,i-1);//求出每一項的最高位 
			m+=t;//求出每一項 
			s+=m;//進行求和 
		}
	}
	printf("s = %d\n",s);
}
int main()
{
	spite();
	return 0;
}

執行結果如下圖:
在这里插入图片描述