while使用
求Sn=a+aa+aaa+aaaa+…+aaa…a(n个a)之值,其中a是一个数,n表示a的位数!!
#include<stdio.h> int main() { int n,a,i,c,tmp;char s[100000]; while(scanf("%d%d",&a,&n)!=EOF) { c=0; for(i=0;i<n;i++) { tmp=c+(n-i)*a; s[i]=tmp%10+'0'; c=tmp/10; } while(c>0) { s[i++]=c%10+'0'; c=c/10; } s[i]='\0'; while(i>=0) printf("%c",s[--i]); printf("\n"); } return 0; }
# include <stdio.h> int main (void) { int a,n,i = 1; long int s1 = 0; long int sn = 0; printf ("请输入a和n的值,以空格分开:\n"); scanf ("%d%d",&a,&n); printf ("a = %d,n = %d\n",a,n); while (i <= n) { s1 = s1 + a; sn = sn + s1; a = a * 10; ++i; } printf ("a+aa+aaa+...= %d\n",sn); return 0; }