求Sn=a+aa+aaa+aaaa+……+aaa…aaa(n个a)
#include<stdio.h>
int main()
{int a,n,i=1,tn=0,sum=0;printf("请输入a:");scanf("%d",&a);fflush(stdin);printf("请输入n:");scanf("%d",&n);fflush(stdin);while(i<=n){tn=tn+a;printf("第%d次,tn=%d\n",i,tn);sum=sum+tn; printf("第%d次,sum=%d\n",i,sum);a=10*a;i++;printf("\n");}return 0;
}
几天后默写的另一种算法:
#include<stdio.h>//s=a+aa+aaa+......+aa...a(n个a)=
int main()
{int a,n,s=0,i,xiang=0;printf("a=");scanf("%d",&a);fflush(stdin);printf("n=");scanf("%d",&n);fflush(stdin);for(i=1;i<=n;i++){xiang=xiang+a;printf("xiang=%d\n",xiang);s=s+xiang;xiang=xiang*10; }printf("s=a+aa+aaa+......+aa...a(%d个a)=%d\n\n",n,s);return 0;
}