c++ primer 第五版 阅读笔记八

第七章 函数——C++的编程模板

7.12 编程练习

#include <iostream>

using namespace std;

inline double TAve(double x,double y)
{
    double t_ave = 0;
    t_ave = 2.0 * x * y /(x + y);
    return t_ave;
}

int main()
{
    double x;
    double y;

    while(cin>>x>>y && x !=0 && y != 0)
    {
        cout<<TAve(x,y)<<endl;
    }

    return 0;
}

#include <iostream>

using namespace std;

int storeScores(int* a)
{
    int i = 0;
    while(cin>>a[i])
    {
        i++;
    }
    return i;
}

void printScores(int* a,int n)
{
    for(int i = 0;i < n;i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
}

double aveScores(int* a,int n)
{
    double temp = 0;
    for(int i = 0;i < n;i++)
    {
        temp += a[i];
    }
    temp /= n;
    return temp;
}

int main()
{
    int a[10];
    int n = storeScores(a);
    printScores(a,n);
    cout<<aveScores(a,n)<<endl;
    return 0;
}

答: a.

#include <iostream>
#include <cstring>

using namespace std;

struct box
{
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};

void printbox(char* maker,float height,float width,float length,float volume)
{
    box box2;
    strcpy(box2.maker,maker);
    cout<<box2.maker<<endl;
    cout<<height<<endl;
    cout<<width<<endl;
    cout<<length<<endl;
    cout<<volume<<endl;
}

int main()
{
    box box1 = {"maker1",1.1,1.2,1.3,5};
    printbox(box1.maker,box1.height,box1.width,box1.length,box1.volume);
    return 0;
}

b. 

#include <iostream>
#include <cstring>

using namespace std;

struct box
{
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};

void printbox(box* box2)
{
    cout<<box2->maker<<endl;
    cout<<box2->height<<endl;
    cout<<box2->width<<endl;
    cout<<box2->length<<endl;
    cout<<box2->volume<<endl;
}

int main()
{
    box box1 = {"maker1",1.1,1.2,1.3,5};
    printbox(&box1);    //传递的类型一定是一个地址
    return 0;
}

c.

#include <iostream>
#include <cstring>

using namespace std;

struct box
{
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};

void printbox1(box* box2)
{
    cout<<box2->maker<<endl;
    cout<<box2->height<<endl;
    cout<<box2->width<<endl;
    cout<<box2->length<<endl;
    cout<<box2->volume<<endl;
}

void printbox2(char* maker,float height,float width,float length,float volume)
{
    box box2;
    strcpy(box2.maker,maker);
    box2.height = height;
    box2.width = width;
    box2.length = length;
    box2.volume = volume;
    cout<<box2.maker<<endl;
    cout<<box2.height<<endl;
    cout<<box2.width<<endl;
    cout<<box2.length<<endl;
    cout<<box2.volume<<endl;
}

int main()
{
    box box1 = {"maker1",1.1,1.2,1.3,5};
    printbox1(&box1);
    printbox2(box1.maker,box1.height,box1.width,box1.length,box1.volume);
    return 0;
}

这里涉及到排列组合的知识,简单的回顾一下:

排列:A_{N}^{M} 就是按照顺序排列出出多少种组合。 A_{N}^{M} = \frac{N!}{M!} = N * (N-1) * (N-2) * ... * (N-M+1)

组合:C_{N}^{M}就是无序的选出一些数看有多少种结果。C_{N}^{M} = C_{N}^{N-M} = \frac{N!}{M! * (N-M)!}

另外需要知道的几个等式:   C_{N}^{0} = 1      C_{N}^{1} = N     C_{N}^{N} = 1       

#include <iostream>

using namespace std;

int main()
{
    int n1 = 47,m1 = 5;
    int n2 = 27,m2 = 1;
    long double per = 1;

    for(int i = n1;i >= n1 - m1 + 1;i--)
    {
        per *= i;
        cout<<per<<endl;
    }
    for(int i = n2;i >= n2 - m2 + 1;i--)
    {
        per *= i;
    }
    per = 1/per;
    cout<<per<<endl;
    return 0;
}

  • 非递归方式:
#include <iostream>

using namespace std;

int main()
{
    int jie = 1;
    int n;
    cin>>n;
    if(n == 0)
        cout<<1;
    if(n > 0)
    {
        for(int i = n;i >= 1;i--)
        {
            jie *= i;
        }
    }
    cout<<jie;

    return 0;
}
  • 递归方式:
#include <iostream>

using namespace std;

int Jiecheng(int n)
{
    if(n == 0)
        return 1;
    else if(n > 0)
        return n * Jiecheng(n-1);
}

int main()
{
    int jie = 1;
    int n;
    cin>>n;
    cout<<Jiecheng(n);

    return 0;
}

#include <iostream>

using namespace std;

int Fill_array(double* arr,int n)
{
    cout<<"请输入double型值:"<<endl;
    int i;
    for(i = 0;cin>>arr[i] && i < n;i++)
    {
        ;
    }
    return i;
}

void Show_array(double* arr,int n)
{
    int temp;
    for(int i = n - 1,j = 0;i >= n/2;i--,j++)
    {
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
    for(int i = 0;i < n;i++)
    {
        cout<<arr[i]<<"\t";
    }
}

int main()
{
    int n;
    cin>>n;
    double arr[n];

    int i = Fill_array(arr,n);

    Show_array(arr,i);

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值