问题描述
在C中写一个程序计算9 ^(8 ^(7 ^(3 ^(2 ^ 1))))))的语言
我试过了/>
#include< stdio.h>
int pow(int n)
{
int i,power;
power = n;
for(i = 0; i< n; i = i + 1)
power = power * power;
返回功率;
}
void main()
{
int result;
char ignore;
result = pow(9,pow(8,pow(7,pow(6) ,pow(5,pow(4,pow(3,pow(2,1))))))));
printf(" \ nresult is%d",result);
printf(" \\\
Press ENTER");
得到(& ignore);
}
但它不起作用。
如何在C中执行此操作标准语言?
我正在使用lcc-win32编译器& Windows 98.
帮助!
write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1)))))))
I tried
#include <stdio.h>
int pow(int n)
{
int i,power;
power=n;
for(i=0;i<n;i=i+1)
power=power*power;
return power;
}
void main()
{
int result;
char ignore;
result= pow(9,pow(8,pow(7,pow(6,pow(5,pow(4,pow(3,pow(2,1) )))))));
printf("\nresult is %d", result);
printf("\nPress ENTER");
gets(&ignore);
}
but it does not work.
how to do that in "C" standard language?
I am using lcc-win32 compiler & windows 98.
help!
推荐答案
请更改名称。 C有一个std库函数,具有相同的
名称,它的原型是:
double pow(double x,double y);
Change the name please. C has a std library function with the same
name and it''s prototype is:
double pow(double x, double y);
main()在C中永远无效。
main() is never void in C.
你的_own_ pow只需要一个参数,为什么这里有两个?
Your _own_ pow only takes one parameter, why here it has two??
我怀疑它溢出,因为''结果''只是一个int。
I suspect it overflows, since ''result'' is only an int.
gets()被认为是有害的,绝对不能使用它。
gets() is considered harmful, NEVER use it.
我恐怕你不能,结果可能太大了。您可以选择
支持大量操作的非标准库,
,例如: gmp。
I am afraid you can''t, the result may be too big. You can choose
a non-standard libary that supports huge number operations,
e.g. gmp.
我不认为你可以在标准C中,结果将是巨大的...
-
Ian Collins。
I don''t think you can in standard C, the result will be huge...
--
Ian Collins.
首先,你可能想让pow函数取两个参数:int
pow(int a,int n){.. 。}
其次,math.h中定义的pow函数为你完成工作,
除了它处理双打:float pow(double a,double n)
返回a作为浮动的n的幂。因此,如果你想处理
整数,你必须转换结果。
最后你可能想用循环来做这件事。它看起来像这样:
#include< math.h>
#include< stdio.h>
int main(){
double res = 1;
int n;
for(n = 2; n< = 9; n ++)
res = pow((double)n,res);
printf(" res =%f \ n",res);
返回0;
}
编译时不要忘记链接数学库(-lm)
但是这可能会溢出,导致res到达inf。你可以尝试使用long double和powl ...或更复杂的东西。
任何想法
结果可能是什么?
First, you may want to have the pow function take 2 arguments: int
pow(int a, int n) { ... }
Second, the pow function defined in math.h does the job for you,
except that it deals with doubles: float pow(double a, double n)
returns a to the power of n as a float. So if you want to deal with
integers you have to convert the result.
Finally you may want to use a loop to do this. It''d look like this:
#include <math.h>
#include <stdio.h>
int main(){
double res=1;
int n;
for( n=2; n<=9; n++ )
res = pow((double)n,res);
printf("res=%f\n",res);
return 0;
}
Don''t forget to link with the math library when compiling (-lm)
However this might overflow, resulting in res reaching inf. You can
try using long double and powl... Or more complicated stuff. Any idea
of what the resulting number might be?
这篇关于不是作业问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!