当前位置:   article > 正文

关于折纸问题_折纸(origami)c++

折纸(origami)c++
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. double paperFold(double thickness,int time){
  4. int i;
  5. double res=thickness;
  6. for(i=0;i<time;i++){
  7. res*=2;
  8. }
  9. return res;
  10. }
  11. int main()
  12. {
  13. double thickness = 7e-8;
  14. printf("%f\n",paperFold(thickness,51));
  15. return 0;
  16. }

double的精确度确实好很多,使用float得出的都是约数

C++

  1. #include <iostream>
  2. using namespace std;
  3. class PageFold{
  4. private:
  5. long double thickness;
  6. int times;
  7. public:
  8. PageFold(double thickness,int times){
  9. thickness = thickness;
  10. times = times;
  11. }
  12. long double calculate(){
  13. long double result = thickness;
  14. for(int i=0;i<times;i++){
  15. result*=2.0;//±ØÐëÓÃ*=
  16. }
  17. return result;
  18. }
  19. };
  20. int main()
  21. {
  22. long double thickness = 0.000000007;
  23. int times = 51;
  24. PageFold example(thickness,times);
  25. cout <<example.calculate()<< endl;
  26. return 0;
  27. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/290140
推荐阅读
相关标签
  

闽ICP备14008679号