#include <stdio.h>
int main(int argc, const char * argv[]) {
int n = 0,a = 0;
long S = 0;
printf("请输入a的值:\n");
scanf("%d",&a);
printf("请输入n的值(一个正整数):\n");
scanf("%d",&n);
if (n<=0) {
printf("输入的数字错误!");
}
//定义变量j进行位数循环,b接受10的倍数,s接受a的n位数
int b = 1,s = 0 ;
//对n个数进行循环
for (int j = 1; j <= n; j++) {
//对a的每一个数进行循环取得,赋给s
for (int i = 1; i<=j; i++) {
s = s+a*b;
b = b*10;
}
printf("%d+",s);
S = S +s ;
//将b和s还原,进行下一次循环
b = 1,s = 0 ;
}
printf("......=S(n) = %ld",S);
return 0;
}
|