main()
{
int a;
long int k,s,n,j,sum;
k=1;
s=0;
printf("input number n:\n");
scanf("%d",&n);
for(a=1;a<=9;a++)
for(j=1;j<=n;j++)
{s=k*a+s;k=10*k;sum=sum+s;}
printf("%ld\n,sum);
}
[此贴子已经被作者于2006-8-31 22:15:28编辑过]
[此贴子已经被作者于2006-8-31 22:15:28编辑过]
#include "Stdio.h"
#include "Conio.h"
main()
{
int a,n;
long int k,s,j,sum=0;
s=0;
printf("input number n:\n");
scanf("%d",&n);
for(a=1;a<=9;a++)
{
k=1;s=0;
for(j=1;j<=n;j++)
{
s=k*a+s;
k=10*k;
sum=sum+s;
}
}
printf("%ld\n",sum);
}
自己对照看。sum初值应为0,每次大循环时,k和s的值也要重置.另外n不必是long型.
您这个程序的n值只能用于较小的值,即便用long型,输入应为scanf("%ld");而不是scanf("%d");