当前位置: 代码迷 >> C语言 >> [求助]关于对一道题目的理解和解题思路问题!
  详细解决方案

[求助]关于对一道题目的理解和解题思路问题!

热度:97   发布时间:2007-08-02 09:33:59.0
以下是引用Biglon在2007-8-2 9:27:14的发言:

我这代码答案怎么老是0啊??

是关于上面  t=1-1/(2*2)-1/(3*3)-...1/(m*m) 的代码~~ 




/* Note:Your choice is C IDE */
#include "stdio.h"
float anwser=1.0 ;
int m,i ;
float func1 ( int x );


main()
{
printf ("please input a integer \n");
scanf ("%d",&m);
for (i=1 ;i<=m;i++) anwser -= func1 (i);
printf ("The anwser is %f\n",anwser);

}


float func1 (int x )

{

float h;
h = 1.0 / (x*x) ;
return h ;

}

这样就对了


----------------解决方案--------------------------------------------------------
程序有问题吧!结果应该是正数吧!
----------------解决方案--------------------------------------------------------


#include "stdafx.h"
#include "stdio.h"
float anwser=1.0 ;
int m,i ;
float func1 ( int x );


int _tmain(int argc, _TCHAR* argv[])
{
printf ("please input a integer \n");
scanf ("%d",&m);
if(m==1)
{
printf ("The anwser is %f\n",anwser);
}
else
{
for (i=2 ;i<=m;i++) anwser -= func1 (i);
printf ("The anwser is %f\n",anwser);
}
return 0;
}


float func1 (int x )

{

float h;
h = 1.0 / (x*x) ;
return h ;

}
这样应该差不多了吧


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

/* Note:Your choice is C IDE */
#include "stdio.h"
float anwser=1.0 ;
int m,i ;
float func1 ( int x );


main()
{
printf ("please input a integer \n");
scanf ("%d",&m);
for (i=2 ;i<=m;i++) anwser -= func1 (i);
printf ("The anwser is %f\n",anwser);

}


float func1 (int x )

{

float h;
h = 1.0 / (x*x) ;
return h ;

}



果然是有错误,应该从2开始~~


----------------解决方案--------------------------------------------------------
#include "stdafx.h"


这是关于什么的库函数

----------------解决方案--------------------------------------------------------
如果t从0.75开始的话,t=1-1/(2*2)-1/(3*3)-...1/(m*m)结果就是0.因为C语言中两个数相除,结果是取整数部分,好像1/(2*2)的实际结果是0.25;但是以语言中把它转换成整数,变成了0;t的初值0.75也要转换成整数计算,转换后也是0
----------------解决方案--------------------------------------------------------
int i,float t=1.0;
其实忘记了float
----------------解决方案--------------------------------------------------------

t=1-1/(2*2)-1/(3*3)-...1/(m*m)定义一个函数

float qt(m)
{
int i;
float t=1.0;
for(i=2;i<=m;i++)
t=t-1/(i*i);
return t;
}

不知道有没有错误哦~~~


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