打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C语言常用数值计算算法(素数、公约数、级数、方程根和定积分)

素数判断

#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; }

最大公约数

1.brute-force算法

#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;
}

2.欧几里得算法

#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; }

穷举法

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

#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;
}
#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; }

级数近似

#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;
)
#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);

一元非线性方程求根

一、牛顿迭代法

  1.基本概念:如果函数连续,且待求零点孤立,那么在零点周围存在一个区域,当初值在这个邻域内时,牛顿法收敛。如果零点不为0, 那么牛顿法将具有平方收敛的性能。

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

  3.判断条件:|f(xn+1)|<ε或|xn+1-xn|<ε是否为真。若为真则xn+1就是方程f(x)=0在x0附近误差ε范围内的一个近似根。

  4.实际应用:求cos(x)-x=0的近似解,精确到10-6

#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;
}

二、二分法

  1.基本概念:对于区间[a,b]上连续不断且 f(a)f(b)<0的函数y=f(x) ,通过不断地把函数 f(x) 的零点所在的区间一分为二,使区间的两个端点逐步逼近零点,进而得到零点近似值的方法叫二分法。

  2.实际应用:求exp(x)+x=0在(-1, 0)的根,精确到10-6

#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; }

三、弦截法

  1.基本概念:弦截法是求非线性方程近似根的一种线性近似方法。它是以与曲线弧AB对应的弦AB与x轴的交点横坐标作为曲线弧AB与x轴的交点横坐标的近似值μ来求出方程的近似解。

  2.实际应用:求((x+2)*x-2)*x-1=0在(-1, 0)的根,精确到10-6

#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;
}

定积分近似计算

1.梯形法

  算法思想

  程序实现

#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; }

2.矩形法

  算法思想

  程序实现

#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;
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
c语言练习实例04
c语言中四个数比较大小问题
10个经典的 C 语言面试基础算法及代码
《算法竞赛入门经典(第2版)》代码 Chapter 2
C语言练习题精选
蛇形方阵详细分析【经典之得一看哈】 - - JavaEye技术网站
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服