我服了,到底错哪了?高手帮忙!
程序的目的是实现:a+aa+aaa+... ...+aaa..aaa(n个a)的和,如n=3,a=2,则为:2+22+222。我的代码如下:#include <math.h>
main ()
{
long int a, temp=0, add=0, sum=0;
int n, i;
printf("Please input positive integers 'a' and 'n':\n");
scanf("%ld %d", &a, &n);
while (a<=0 || n<=0)
{
printf("Sorry! 'a' or 'n' doesn't fit. Please input 'a' & 'n' again:\n");
scanf("%ld %d", &a, &n);
}
while (a>=10)
{
printf("Sorry! 'a' can not be larger than 9. Please input 'a' & 'n' again:\n");
scanf("%ld %d", &a, &n);
}
for (i=n; i>0; i--)
{
temp=temp+a*pow(10,i-1);
}
add=(int)(temp);
sum=add;
while (add>9)
{
add=(int)(add/10);
sum=sum+add;
}
printf("%d+%d%d+...+%d=%d.\n",a,a,a,(int)(temp),sum);
}
编译通过,测试:a=2, n=6,结果完全不对,a甚至还等于0了。我郁闷了,到底错哪了?怎么也调不出来!谢谢了!