当前位置: 代码迷 >> C语言 >> 这个程序错在哪了?
  详细解决方案

这个程序错在哪了?

热度:255   发布时间:2006-02-11 18:55:00.0
这个程序错在哪了?
1+11+111+1111+++++++111111111=?
2+22+222+2222+++++++222222222=?
3+33+333+3333+++++++333333333=?
4+44+444+4444+++++++444444444=?
5+55+555+5555+++++++555555555=?
6+66+666+6666+++++++666666666=?
7+77+777+7777+++++++777777777=?
8+88+888+8888+++++++888888888=?
9+99+999+9999+++++++999999999=?


#include "math.h"
#include "stdio.h"
main()
{
int i,n,m,x,y=0;
for (i=1;i<=9;i++)
{
for (n=0;n<=8;n++)
{x=pow(10,n)+i;
y=x+y;}
printf ("y=%d\n",y);}
}
搜索更多相关的解决方案: include  

----------------解决方案--------------------------------------------------------
应该是长整形吧
----------------解决方案--------------------------------------------------------
#include "math.h"
#include "stdio.h"
main()
{
long i,n,m,x,y=0;
for (i=1;i<=9;i++)
{
for (n=0;n<=8;n++)
{x=pow(10,n)+i;
y=x+y;}
printf ("y=%d\n",y);}
}

晕,改成长整形了.为何还不行?
----------------解决方案--------------------------------------------------------
pow 函数返回的是double不能这么用 可以写个递归的函数调用

----------------解决方案--------------------------------------------------------

递归...请指教.谢谢.


----------------解决方案--------------------------------------------------------
把原题发上来吧
----------------解决方案--------------------------------------------------------

题目在一楼.我改程序,帮我检测一下.

#include "stdio.h"
main()
{
long i,n,m,x=0,y=1;
for (i=1;i<=9;i++)
{
for (n=0;n<=8;n++)
{x=x+y;
y=y*10+i;}
printf ("y=%d\n",x);}
}

运行,仍不行?


----------------解决方案--------------------------------------------------------

#include<stdio.h>
long fn(int );
void main()
{
int i;
unsigned long num;
for (i=1;i<10;i++)
{
num=fn(i)+i*2;
printf("%ld\n",num);
num=0;
}
}

long fn(int i)
{
unsigned long sum=0;
long n=i;

int j;
for (j=0;j<8;j++)
{
n=n*10+1;
sum+=n;
}
return sum;
}
呵呵 并没有用到递归
关键的地方是变量类型而已.因为你要求的数太大了.

[此贴子已经被作者于2006-2-11 21:21:13编辑过]


----------------解决方案--------------------------------------------------------
不知道对不对 没有算

[此贴子已经被作者于2006-2-11 19:44:01编辑过]



----------------解决方案--------------------------------------------------------

需要长数据,可以使用链表


----------------解决方案--------------------------------------------------------
  相关解决方案