【求组】新人写的小代码有点问题
题目如下:编写程序,输入整数n和a,输出S=a+aa+aaa+....aaaa...a(n个a)的结果。如n=5,a=3,则S=3+33+333+3333+3333.
代码如下:
#include <stdio.h>
int sum(int n,int a);
int main()
{
int n,a;
printf("please input two number:");
scanf("%d%d",&n, &a);
printf("s=%d",sum(n,a));
}
int sum(int n,int a)
{
int s=0,item,i,t,add;
for(i=1,t=1; i<=n; i++) {
add=a*t;
item+=add;
s+=item;
t=t*10;
}
return s;
}
运行结果有误:
please input two number :
输入:2 1
S=1594578524