我思考了很久,写不出这个程序,请教高手
求Sn=a+aa+aaa+...+aa...a之值,其中a是一个数字,n表示a的位数,例如:2+22+222+2222+22222(此时n=5)n由键盘输入。
#include <stdio.h> void main() { int n; int i = 1; int sum = 0; int result = 0; int j = 0; printf("input a value: \n"); scanf("%d",&n); for (; i<10; i++) { for (j=0; j<n; j++) { sum = sum*10 + i; result += sum; printf("%d",sum); if(j!=n-1) { printf("+"); } } printf("=%d\n",result); sum = 0; result = 0; } }
#include <stdio.h> int power(int exp) { int i = 1; while (exp--) i *= 10; return i; } int an(int n) { int result = 0; while (n--) result += power(n) * 2; return result; } int main(void) { int n, i, j, sum = 2; scanf("%d", &n); printf("S%d = %d ", n, an(1)); for (i = 2; i <= n; ++i) { j = an(i); sum += j; printf("+ %d", j); } printf(" = %d\n", sum); return 0; }