c++ day03

作者: __method__ | 来源:发表于2021-01-27 22:06 被阅读0次

函数的原型声明

#include <iostream>
using namespace std;
int  main()
{
    // 函数的原型声明; 可以不传参
    int min_num(int, int);
    int a, b, mv;
    cout<<"请输入两个整数:"<<endl;
    cin>>a>>b;
    mv=min_num(a, b);         //函数调用
    cout<<"最小值为: "<<mv<<endl;
}

int min_num(int x, int y)     //函数定义:求两个数中的最小值
{
    return (x < y ? x : y );
}

理解函数参数的传值

#include <iostream>
using namespace std;
void swap(int a, int b)
{
    int temp;
    temp=a;
    a=b;
    b=temp;
    cout<<"a="<<a<<","<<"b="<<b<<endl;
}
int  main()
{
    int a(4), b(5);   //等价于int a=4, b=5;
    cout<<"before a="<<a<<","<<"b="<<b<<endl;
    swap(a, b);
    cout<<"after a="<<a<<","<<"b="<<b<<endl;
}


哥德巴赫猜想验证

#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(int a){
    int i, k;
    // (int) 强制转换为 int 型
    k = (int)sqrt(a);
    for (int j = 2; j <= k ; ++j) {
        if (a%j==0)
            return false; // 是合数
    }
    return true;
}
int  main()
{
    int b;
    // 96 ~ 100 之间的哥德巴赫猜想
    for (int i = 96; i <=100 ; i += 2) {
        for (int j = 2; j < i/2 ; j++) {
            if (isPrime(j)){
               b =  i - j;
               if(isPrime(b)){
                   cout<<i<<" = "<< j <<" + " << b<< endl;
                   break; // 没有break 会找到所有的组合
               }
            }
        }
    }
}


函数引用调用示例

//  函数的引用调用
#include <iostream>
using namespace std;
void swap(int &a, int &b)
{
    int temp;
    temp=a;
    a=b;
    b=temp;
    cout<<"a="<<a<<","<<"b="<<b<<endl;
}
int  main()
{
    int a(4), b(5);   //等价于int a=4, b=5;
    cout<<"before a="<<a<<","<<"b="<<b<<endl;
    swap(a, b);
    cout<<"after a="<<a<<","<<"b="<<b<<endl;
}


函数的嵌套调用

#include <iostream>
using namespace std;
// 最大公约数
int gcd(int x, int y)
{
    int r;

    while ((r=x%y)!=0)
    {   x=y; y=r;   }
    return y;
}
// 最小公倍数
int lcm(int x, int y)
{
    int d ;
    d=gcd(x, y);         //调用求最大公约数的函数
    return (x*y/d);
}
int  main()
{
    int x, y, d, m;

    cout<<"请输入两个整数: ";
    cin>>x>>y;
    d=gcd(x, y);
    m=lcm(x, y);
    cout<<"最大公约数为:"<<d<<'\n';
    cout<<"最小公倍数为:"<<m<<'\n';
}

函数递归

递归就是自己调用自己
递归可以改写成循环, 但是递归写法更加简单
写递归程序一定要注意写出递归的终止条件

// 递归示例 求 n(n-1)
#include <iostream>
using namespace std;
float f(int n){
    if(n<0){
        cout<< "n<0 error !"<< endl;
        return -1;
    } else if (n == 0 || n == 1){
        return 1;
    } else{
        return n*f(n-1);
    }
}
int  main()
{
   int a =-5;
   cout<< f(a)<< endl;
}

斐波那契数列(Fibonacci sequence),又称黄金分割数列、因数学家莱昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、……在数学上,斐波那契数列以如下被以递推的方法定义:F(0)=0,F(1)=1, F(n)=F(n - 1)+F(n - 2)(n ≥ 2,n ∈ N*)

// 0、1、1、2、3、5、8、13、21、34、……
// *F*(0)=0,*F*(1)=1, *F*(n)=*F*(n - 1)+*F*(n - 2)(*n* ≥ 2,*n* ∈ N*)
// f(2) = 1  f(3) = 2 f(5) = 5
// f(6) =8  f(7) = 13 f(5) = 5
#include <iostream>
using namespace std;
float f(int n){
    if (n ==0 ) return 0;
    if (n ==1 ) return 1;
    return f(n-1) +  f(n-2);
}
int  main()
{
   int a = 3;
   cout<< f(7)<< endl;
}


相关文章

网友评论

      本文标题:c++ day03

      本文链接:https://www.haomeiwen.com/subject/tbfzzktx.html