c语言 大数开方,c语言求一个数的平方根
一般的來說我們在進(jìn)行開方運(yùn)算時(shí),都會使用sqrt函數(shù)進(jìn)行開方運(yùn)算。使用sqrt時(shí)就需要引用頭文件math.h。(這里使用%d來輸出整數(shù))
#include
#include
int Mysqrt(int n)
{
return sqrt(n*1.0);
}
int main()
{
printf("%d\n",Mysqrt(10));
printf("%d\n",Mysqrt(9));
return 0;
}
還有一種方法可以不使用math.h。我們利用循環(huán)來找出n使得n*n小于所求根的數(shù),且(n+1)*(n+1)大于所求根的數(shù)。
#include
int Mysqrt(int n)
{
int i;
for(i=0;i*i<=n;i++)
;
return i-1;
}
int main()
{
printf("%d\n",Mysqrt(10));
printf("%d\n",Mysqrt(9));
return 0;
}
方法二用時(shí)要短一些。
一般的來說我們在進(jìn)行開方運(yùn)算時(shí),都會使用sqrt函數(shù)進(jìn)行開方運(yùn)算。使用sqrt時(shí)就需要引用頭文件math.h。(這里使用%d來輸出整數(shù))
#include
#include
int Mysqrt(int n)
{
return sqrt(n*1.0);
}
int main()
{
printf("%d\n",Mysqrt(10));
printf("%d\n",Mysqrt(9));
return 0;
}
還有一種方法可以不使用math.h。我們利用循環(huán)來找出n使得n*n小于所求根的數(shù),且(n+1)*(n+1)大于所求根的數(shù)。
#include
int Mysqrt(int n)
{
int i;
for(i=0;i*i<=n;i++)
;
return i-1;
}
int main()
{
printf("%d\n",Mysqrt(10));
printf("%d\n",Mysqrt(9));
return 0;
}
總結(jié)
以上是生活随笔為你收集整理的c语言 大数开方,c语言求一个数的平方根的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HQL入门学习
- 下一篇: H264中的SPS、PPS提取与作用