头歌Educoder实验:C++之函数进阶练习题

第1关:计算成绩

任务描述

本关任务:编写一个能根据学生号码来输出对应成绩的小程序。

编程要求

实数个数为5(人数为5),注意成绩由程序员赋值为:65,79, 98, 87, 83。 输入用户指定的学生的号码(1-5),输出对应的成绩,当用户输入为0时程序结束。

要求:用户输入、查找成绩并输出都在函数中实现。 提示:函数原型 void check(int x[], int N);

效果如下: 输入: 3 3 3 3 0

输出: 98 98 98 98


开始你的任务吧,祝你成功!

#include<iostream>
using namespace std;
void check(int x[],int N);
int main()
{
     // 请在此添加你的代码
    /********** Begin ********/
    int score[5] = {65, 79, 98, 87, 83};
    int number;
    cin>>number;
    while(number >= 1 && number <= 5){
        check(score, number - 1);
        cin>>number;
    }
    
    /********** End **********/
}
void check(int x[],int N)
{
    // 请在此添加你的代码
    /********** Begin ********/
    cout<<x[N]<<endl;
    /********** End **********/
}

第2关:买衣服要花多少钱呢

任务描述

本关任务:编写一个能计算买上衣和裤子要付多少钱的小程序。

编程要求

某服装店经营套服,也单件出售。若买的不少于50套,每套80元;若不足50套,则每套90元;单件上衣,每件60元;单条裤子,每条45元。 现编写函数计算应付款。

函数原型是 int Payfor(int clothes,int pants),其中:参数clothes表示衣服的数量,参数pants表示裤子的数量。

输入依次为要买的上衣和裤子数 输出为付款数

效果如下: 输入:1 2 输出:135


开始你的任务吧,祝你成功!

#include <iostream>
using namespace std;
int Payfor(int clothes,int pants)
{
     // 请在此添加你的代码
    /********** Begin ********/
    if(clothes >= 50 && pants >= 50){
        if(clothes == pants){
            return clothes * 80;
        } 
        if(clothes > pants){
            return pants * 80 + (clothes - pants) * 60;
        }
        if(clothes < pants){
            return clothes * 80 + (pants - clothes) * 45;
        }
    }else{
        if(clothes == pants){
            return clothes * 90;
        }
        if(clothes > pants){
            return pants * 90 + (clothes - pants) * 60;
        }
        if(clothes < pants){
            return clothes * 90 + (pants - clothes) * 45;
        }
    }

    /********** End **********/
}
int main()
{
    int clothes, pants;
    cin >> clothes >> pants;
    int n = Payfor(clothes,pants);
    cout << n << endl;
    return 0;
}

第3关:求余数

任务描述

本关任务:编写一个能求余数的小程序。

编程要求

完成两个mod函数的编写,分别求两个整数相除的余数和两个实数相除的余数。两个实数求余定义为实数四舍五入取整后相除的余数。

输入: 5 3 8.6 2.3

输出: 2 1


开始你的任务吧,祝你成功!

#include <iostream>
#include <cmath>
using namespace std;

    //获取参数方式 cin
    //int x =0;
    //cin >> x;

    //结果输出使用 cout
    //cout<<"1";

 	// 请在此添加你的代码
    /********** Begin ********/
    int main(){
        int a, b;
        double c, d;
        int Zmod(int a, int b);
        int Rmod(double c, double d);
        cin>>a>>b;
        cin>>c>>d;
        cout<<Zmod(a, b)<<endl;
        cout<<Rmod(c, d)<<endl;

        return 0;
    }
    int Zmod(int a, int b){
        return a % b;
    }
    int Rmod(double c, double d){
        int a = round(c);
        int b = round(d);
        return a % b;
    }


	/********** End **********/

第4关:计算x的n次幂

任务描述

本关任务:编写一个用递归方法来计算x的n次幂的小程序。

编程要求

编写一个函数power(float x,int n)用递归的方法计算x的n次幂。在主函数实现输入和输出。

效果如下: 输入:2 3 输出:8

#include<iostream>
using namespace std;
float power(float X,int N);
int main()
{
     // 请在此添加你的代码
    /********** Begin ********/
    float number;
    int n;
    cin>>number>>n;
    cout<<power(number, n)<<endl;
    /********** End **********/
}
float power(float X,int N)
{
     // 请在此添加你的代码
    /********** Begin ********/
    if(N == 0){
        return 1;
    }else if(N == 1){
        return X;
    }else{
        return X*power(X, N - 1);
    }
    /********** End **********/
}

第5关:加密函数

编程要求

请编写一加密函数,函数原型是void encrypt(char * info) 该函数针对输入字符串的加密规律是:对字符串的每个字母以该字母后面第4个字母加以替换。

例如,字母'A'后面第4个字母是'E',用'E'代替'A'。 因此,"China"应译为"Glmre",

注意: 'W'后面的第4个字母是'A','X'后面的第4个字母是'B'。 'Y'后面的第4个字母是'C','Z'后面的第4个字母是'D'。 (小写字母与大写字母处理相类似)

效果如下: 输入:aVwpFz 输出:eZatJd


开始你的任务吧,祝你成功!

#include<iostream>
using namespace std;
void encrypt(char info[]);
int main()
{
     // 请在此添加你的代码
    /********** Begin ********/
    char ch[100];
    cin>>ch;
    encrypt(ch);
    /********** End **********/
}
void encrypt(char info[])
{
     // 请在此添加你的代码
    /********** Begin ********/
    int i = 0;
    while(info[i] != ' '){
        if(info[i] >= 'A' && info[i] <= 'V'){
            info[i] += 4;
        }
        else if(info[i] >= 'a' && info[i] <= 'v'){
            info[i] += 4;
        }
        else if(info[i] >= 'W' && info[i] <= 'Z'){
            info[i] = info[i] - 26 + 4;
        }
        else if(info[i] >= 'w' && info[i] <= 'z'){
            info[i] = info[i] - 26 + 4;
        }else{
            break;
        }
        cout<<info[i];
        i++;
    }
    
    /********** End **********/
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风绪Fengxu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值