短短幾行代碼,卻也可圈可點。如把變量s放在PI表達式中,還有正負值的處理,都堪稱經典。尤其是處處考慮執行效率的思想令人敬佩。
/* pi/4=1-1/3+1/5-1/7+1/9-…… */ #include <stdio.h> int main(){ int s=1; float pi=0.,n=1.,t=1.; while(t>1e-6) { pi+=s*t; n+=2.; s=-s; t=1./n; } printf("\tPI=%7.6f\n",pi*4); return 0; }
?
短短幾行代碼,卻也可圈可點。如把變量s放在PI表達式中,還有正負值的處理,都堪稱經典。尤其是處處考慮執行效率的思想令人敬佩。
/* pi/4=1-1/3+1/5-1/7+1/9-…… */ #include <stdio.h> int main(){ int s=1; float pi=0.,n=1.,t=1.; while(t>1e-6) { pi+=s*t; n+=2.; s=-s; t=1./n; } printf("\tPI=%7.6f\n",pi*4); return 0; }
?
轉載于:https://www.cnblogs.com/AwakenCode/p/11253669.html
本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。 如若轉載,請注明出處:http://www.pswp.cn/news/392128.shtml 繁體地址,請注明出處:http://hk.pswp.cn/news/392128.shtml 英文地址,請注明出處:http://en.pswp.cn/news/392128.shtml
如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!