乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C語言常用數(shù)值計(jì)算算法(素?cái)?shù)、公約數(shù)、級(jí)數(shù)、方程根和定積分)

       菌心說 2021-09-16

      素?cái)?shù)判斷

      復(fù)制代碼
      #include<stdio.h> #include<math.h> int main() { int n,min,max,isprime; scanf('%d %d',&min,&max); if(min<=2){ printf('%4d',2); min=3; } if(min%2==0) min++; for(n=min;n<=max;n+=2){ for(isprime=1,i=2;t&&i<=sqrt(n);i++) if(n%i==0) isprime=0; if(isprime) printf('%4d',n); } return 0; }
      復(fù)制代碼

      最大公約數(shù)

      1.brute-force算法

      復(fù)制代碼
      #include<stdio.h>
      int main()
      {
          int x=30,y=45,z;
          z=x;
          while(!(x%z==0&&y%z==0))
              z--;
          printf('%d',z);
          return 0;
      }
      復(fù)制代碼

      2.歐幾里得算法

      復(fù)制代碼
      #include<stdio.h> int main() { int x=35,y=45,r; while((r=x%y)!=0){ x=y; y=r; } printf('%d',y); return 0; }
      復(fù)制代碼

      窮舉法

      解方程: ①x+y+z=100 ②5x+3y+z/3=100

      復(fù)制代碼
      #include<stdio.h>
      int main()
      {
          int x,y,z;
          for(x=0;x<=20;x++)
              for(y=0;y<=33;y++)
                  for(z=0;z<=100;z++)
                      if(x+y+z==100&&5*x+3*y+z/3==100&&z%3==0)
                          printf('x=%d,y=%d,z=%d\n');
          return 0;
      }
      復(fù)制代碼
      復(fù)制代碼
      #include<stdio.h> int main() { int x,y,z; for(x=0;x<=20;x++) for(y=0;y<=33;y++){ z=100-x-y; if(5*x+3*y+z/3==100&&z%3==0) printf('x=%d,y=%d,z=%d\n',x,y,z); } return 0; }
      復(fù)制代碼

      級(jí)數(shù)近似

      復(fù)制代碼
      #include<stdio.h>
      #include<math.h>
      int main()
      {
          double s=1,a=1,x,eps;
          int n;
          scanf('%lf%lf',&x,&eps);
          for(n=2;fabs(a)>eps;n++){
              a=a*x/(n-1);
              s+=a;
          }
          printf('%f',s);
          return 0;
      )
      復(fù)制代碼
      復(fù)制代碼
      #include<stdio.h> #include<math.h> int main() { double sum,x,eps=1e-6,fn,tn; int s=1,n=2; scanf('%lf',&x); s=fn=x; do{ s=-s; fn=fn*(2*n-3)/(2*n-2)*x*x; tn=sign*fn/(2*n-1); sum=sum+tn; n++; }while(fabs(tn)>eps); printf('%f',sum);
      復(fù)制代碼

      一元非線性方程求根

      一、牛頓迭代法

        1.基本概念:如果函數(shù)連續(xù),且待求零點(diǎn)孤立,那么在零點(diǎn)周圍存在一個(gè)區(qū)域,當(dāng)初值在這個(gè)鄰域內(nèi)時(shí),牛頓法收斂。如果零點(diǎn)不為0, 那么牛頓法將具有平方收斂的性能。

        2.基本公式:xk+1=xk-f(xk)/f'(xk)

        3.判斷條件:|f(xn+1)|<ε或|xn+1-xn|<ε是否為真。若為真則xn+1就是方程f(x)=0在x0附近誤差ε范圍內(nèi)的一個(gè)近似根。

        4.實(shí)際應(yīng)用:求cos(x)-x=0的近似解,精確到10-6

      復(fù)制代碼
      #include<stdio.h>
      #include<math.h>
      
      int main()
      {
          double x1=0, x2 = 2;
          while (fabs(cos(x2)-x2)>1e-6&&fabs(x2-x1)>1e-6){
              x1 = x2;
              x2 = x1 + (cos(x1) - x1) / (sin(x1) + 1);
          }
          printf('x=%f\nf(x)=%.6f',x2,fabs(cos(x2)-x2));
          return 0;
      }
      復(fù)制代碼

      二、二分法

        1.基本概念:對(duì)于區(qū)間[a,b]上連續(xù)不斷且 f(a)f(b)<0的函數(shù)y=f(x) ,通過不斷地把函數(shù) f(x) 的零點(diǎn)所在的區(qū)間一分為二,使區(qū)間的兩個(gè)端點(diǎn)逐步逼近零點(diǎn),進(jìn)而得到零點(diǎn)近似值的方法叫二分法。

        2.實(shí)際應(yīng)用:求exp(x)+x=0在(-1, 0)的根,精確到10-6

      復(fù)制代碼
      #include<stdio.h> #include<math.h> double f(double x); int main() { double a=-1, b=0, c; c = (a+b)/2; do{ if(f(a)*f(c)>0) a = c; else b = c; c = (a+b)/2; } while (fabs(f(c)) > 1e-6&&fabs(a-b)>1e-6); printf('x=%.6f', c); return 0; } double f(double x) { return exp(x) + x; }
      復(fù)制代碼

      三、弦截法

        1.基本概念:弦截法是求非線性方程近似根的一種線性近似方法。它是以與曲線弧AB對(duì)應(yīng)的弦AB與x軸的交點(diǎn)橫坐標(biāo)作為曲線弧AB與x軸的交點(diǎn)橫坐標(biāo)的近似值μ來求出方程的近似解。

        2.實(shí)際應(yīng)用:求((x+2)*x-2)*x-1=0在(-1, 0)的根,精確到10-6

      復(fù)制代碼
      #include <math.h>
      #include <stdio.h>
      float f(float x)
      {
          float y;
          y = ((x + 2.0) * x - 2.0) * x - 1.0;
          return y;
      }
      
      int main()
      {
          float x, x1, x2;
          x1 = -1, x2 = 0;
          do{
              x = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));
              if (f(x) * f(x1) > 0)
                  x1 = x;
              else
                  x2 = x;
          } while (fabs(f(x)) >= 1e-6);
          printf('the root is %f\n', x);
          printf('((x+2.0)*x-2.0)*x-1.0=%f\n', f(x));
          return 0;
      }
      復(fù)制代碼

      定積分近似計(jì)算

      1.梯形法

        算法思想

        程序?qū)崿F(xiàn)

      復(fù)制代碼
      #include<stdio.h> #include<math.h> int main() { long n, i; double a=-1.57,b=1.57,h,s,x; scanf('%ld',&n); h=(b-a)/n; s=(cos(a)+cos(b))/2; x=a; for(i=1;i<n;i++){ x+=h; s+=cos(x); } printf('%f',s*h); return 0; }
      復(fù)制代碼

      2.矩形法

        算法思想

        程序?qū)崿F(xiàn)

      復(fù)制代碼
      #include<stdio.h>
      #include<math.h>
      int main()
      {
          long n,i;
          double a=-1.57,b=1.57,h,s=0,x;
          scanf('%ld',&n);
          h=(b-a)/n;
          x=a;
          for(i=0;i<n;i++){
              s+=cos(x);
              x+=h;
          }
          printf('%f',s*h);
          return 0;
      }
      復(fù)制代碼

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多