编程实现.求Sn=a+aa+aaa+.......+aaa....aaa之值,其中a是一个数字,n表示a的位数,例:2+22+222+2222

用C语言
2024-12-26 14:32:19
推荐回答(1个)
回答1:

#include "stdio.h"
#include "math.h"
void main()
{ int a,n,i;
long sum=0;
printf("Please input a(0 scanf("%d%d",&a,&n);
if(a>9||a<=0)
{
printf("a is not in (1,2,3,4,5,6,7,8,9)!\nplease input a again:");
scanf("%d",&a);
}
for(i=1;i<=n;i++)
{
sum+=(pow(10,i)-1)*a/9.0;
}
printf("sum=:%ld",sum);

}