例如:2+22+222+2222+22222的值(此时n=5)
C语言代码:
# include <stdio.h>
int main()
{
int a, n, i;
long int t = 0, sum = 0;
printf("请您输入a,n:\n");
scanf("%d,%d", &a, &n);
if(n>=1)
{
for(i=1; i<=n; i++)
{
t = t + a;
sum = sum + t;
a = a * 10;
}
printf("a+aa+…+aa…a=%ld\n", sum);
}
else
printf("a+aa+…+aa…a=0\n");
return 0;
}
运行结果: