华科大一C++程序设计基础oj编程题参考答案


因为侵犯慕课网版权而被投诉了可还行……


第五章 编程题

1

参考答案

#include <iostream>
#include <cstring>
using namespace std;
int main(){
    char str[80];
    int count = 0;
    cin >> str;
    for (int i = 0;i < strlen(str);i++){
        if (str[i] >= 'a' && str[i] <= 'z'){
            str[i] += 'A' - 'a';
            count++;
        }
    }
    cout << str << endl << count;
}

2

参考答案

#include <iostream>
using namespace std;
int main(){
    int array[10];
    for (int i = 0;i < 10;i++){
        cin >> array[i];
    }
    int search;
    bool thereBe = false;
    cin >> search;
    for (int i = 0;i < 10;i++){
        if (array[i] == search){
            thereBe = true;
            cout << i + 1 << endl;
        }
    }
    if(!thereBe)
        cout << "数组中没有该数";
}

3

参考答案

#include <iostream>
using namespace std;
int main(){
    float score[3][5];
    for (int i = 0; i < 3;i++){
        float sum = 0;
        for (int j = 0; j < 3; j++){
            cin >> score[i][j];
            sum += score[i][j];
        }
        score[i][3] = sum;
        score[i][4] = sum / 3;
    }
    int rank[3];
    if (score[1][3] > score[2][3]){
        rank[0] = 1;
        rank[1] = 2;
    }
    else {
        rank[0] = 2;
        rank[1] = 1;
    }
    if (score[0][3] > score[ rank[0] ][3]){
        rank[1] = rank[0];
        rank[0] = 0;
    }else if (score[0][3] > score[ rank[1] ][3]){
        rank[2] = rank[1];
        rank[1] = 0;
    }else rank[2] = 0;
    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 5; j++){
            cout << score[ rank[i] ][j] << "  ";
        }
        cout << i + 1 << "  " << endl;
    }
}

第六章 编程题

1

参考答案

#include <iostream>
using namespace std;
int main(){
    char str1[80], str2[40];
    int p;
    for (int i = 0; i < 80; i++){
        str1[i] = 0;
    }
    cin >> str1 >> str2 >> p;
    char *point = &str1[p];
    char *pt;
    for (int i = 0; str2[i] != 0; i++){
        for (pt = str1; *pt; pt++);
        for (; pt != point; pt--){
            *pt = *(pt - 1);
            
        }
        *point = str2[i];
        point++;
    }
    cout << str1;
}

2

参考答案

#include <iostream>
#include <cstring>
using namespace std;
int main(){
    char xnum[40];
    int tnum = 0, sxtn = 1;
    cin >> xnum;
    char *point = &xnum[strlen(xnum) - 1];
    while (*point != '\0'){
        if (*point >= '0' && *point <= '9')
            tnum += (*point - '0') * sxtn;
        else if (*point >= 'A' && *point <= 'F')
            tnum += (*point - 'A' + 10) * sxtn;
        else if (*point >= 'a' && *point <= 'f')
            tnum += (*point - 'a' + 10) * sxtn;
        else if (*point == '-')
            tnum = -tnum;
        else {
            cout << "input data error!" << endl;
            return 0;
        }
        sxtn *= 16;
        point--;
    }
    cout << tnum << endl;
}

3

参考答案

#include <iostream>
using namespace std;
int main(){
    char str[40];
    int sum[64];
    for (int i = 0; i < 64; i++)
        sum[i] = 0;
    cin >> str;
    char *pch = str;
    while (*pch != '\0'){
        sum[*pch - 'A']++;
        pch++;
    }
    for (int i = 0; i < 64; i++){
        if (sum[i])
            cout << (char)(i + 'A') << ' ' << sum[i] << endl;
    }
}

第七章 编程题

1

参考答案

#include <iostream>
using namespace std;
int exchange(int num){
    int exnum = 0;
    while (num) {
        exnum = exnum * 10 + num % 10;
        num /= 10;
    }
    return exnum;
}
void sort(int *array, const int num){
    int brray[num];
    int t1, t2;
    for (int i = 0; i < num; i++)
        brray[i] = exchange(array[i]);
    for (int i = 1; i < num; i++){
        for (int j = i; brray[j] < brray[j - 1] && j; j--){
            t1 = brray[j];
            t2 = array[j];
            brray[j] = brray[j - 1];
            array[j] = array[j - 1];
            brray[j - 1] = t1;
            array[j - 1] = t2;
        }
    }
}
int main()
{
    int A, B, i, k;
    int yuan[50];
    cin >> A >> B;
    for (k = 0, i = A; i <= B; i++)
        yuan[k++] = i;
    sort(yuan, k);
    cout << yuan[0];
    for (i = 1; i < k; i++)
        cout << " " << yuan[i];
    cout << endl;
    return 0;
}

2

参考答案

#include <iostream>
#include <cstring>
using namespace std;
bool detection(char *num, char *merchant){
    const int mbc = 21;
    const int ucc = 12;
    const int tcc = 10;
    int Mobile[21] = {134, 135, 136, 137, 138, 139, 147, 150, 151, 152, 157, 158, 159, 172, 178, 182, 183, 184, 187, 188, 198};
    int Unicom[12] = {130, 131, 132, 145, 155, 156, 166, 171, 175, 176, 185, 186};
    int Telecom[10] = {133, 149, 153, 173, 177, 180, 181, 189, 191, 199};
    int code = 0;
    for (int i = 0; i < 3; i++){
        code = code * 10 + (num[i] - '0');
    }
    for (int i = 0; i < mbc + ucc + tcc; i++){
        if (i < mbc){
            if (code == Mobile[i]){
                strcpy(merchant, "中国移动");
                return true;
            }
        } else if (i < mbc + ucc){
            if (code == Unicom[i - mbc]){
                strcpy(merchant, "中国联通");
                return true;
            }
        } else if (code == Telecom[i - mbc - ucc]){
            strcpy(merchant, "中国电信");
            return true;
        }
    }
    return false;
}
int main(){
    char number[12];
    char merchant[9];
    cin >> number;
    if (detection(number, merchant))
    cout << merchant << endl;
    else
    cout << "不确定" << endl;
    return 0;
}

3

参考答案

#include <iostream>
using namespace std;
long C(int num){
    int result = 0;
    if (num == 0 || num == 1)
        return 1;
    else {
        for (int i = 0; i < num; i++){
            result += C(i) * C(num - 1 - i);
        }
        return result;
    }
}
void CTL(long *lngCTL,const int num){
    lngCTL = new long[num];
    for (int i = 0; i < num; i++){
        lngCTL[i] = C(i);
        cout << lngCTL[i];
        if (i < num - 1)cout << ' ';	//注意:此处最后一项后不得有空格,否则会提示格式错误
    }
    cout << endl;
}
int main(){
    long *lngCTL;
    int n, i;
    cin >> n;
    CTL(lngCTL, n);
    return 0;
}

第九章 编程题


第九章的题目都是看起来很长,其实要写的也没那么多,主函数干脆就直接给你提供了,要写的只有那几个成员函数而已。


1

参考答案

#include <iostream>
using namespace std;
class fraction
{
private:
    int above;         //分子
    int below;         //分母
    int gcd(int,int);  //求两个数的最大公约数
    int lcm(int,int);  //求两个数的最小公倍数
public:
    fraction();
    fraction(int ,int);
    void reduction();  //约分
    fraction add(fraction);      //两分数相加
    fraction div (fraction);      //两分数相除
    void setfrac(int ,int);        //重置分数
    void display();                //显示分数
};
fraction::fraction(){ above = 0; below = 1; }
fraction::fraction(int a, int b = 1):above(a),below(b){}
int fraction::gcd(int a, int b){
    if (a % b == 0)
        return b;
    else return gcd(b, a % b);
}		//辗转相除法
int fraction::lcm(int a, int b){
    return a * b / gcd(a, b);
}
void fraction::reduction(){
    int gcd_of_a_b = gcd(above, below);
    above = above / gcd_of_a_b;
    below = below / gcd_of_a_b;
}
fraction fraction::add(fraction b){
    fraction result;
    result.below = below * b.below;
    result.above = above * b.below + b.above * below;
    result.reduction();
    return result;
}
fraction fraction::div(fraction b){
    fraction result;
    result.above = above * b.below;
    result.below = below * b.above;
    result.reduction();
    return result;
}
void fraction::setfrac(int a, int b){ above = a; below = b; }
void fraction::display()
{
    if (below < 0){
        above = -above;
        below = -below;
    }
    if (above == 0) cout << 0 << endl;
    else if (below == 1) cout << above << endl;
    else cout << above << '/' << below << endl;
}
int main()
{
    cout << "--------测试构造函数-----------" << endl;
    fraction f1(20, -9), f2(-7, -9), f3(5, -9);
    cout << "f1=";    f1.display();
    cout << "f2=";    f2.display();
    cout << "f3=";    f3.display();
    
    cout << "--------测试加法函数-----------" << endl;
    f3 = f1.add(f2);
    cout << "f1=";    f1.display();
    cout << "f2=";    f2.display();
    cout << "f3=f1+f2=";    f3.display();
    f1.setfrac(-3, -15);
    f2.setfrac(-10,12);
    f3 = f1.add(f2);
    cout << "f1=";    f1.display();
    cout << "f2=";    f2.display();
    cout << "f3=f1+f2=";    f3.display();
    f1.setfrac(12, 45);
    f2.setfrac(19, -30);
    f3 = f1.add(f2);
    cout << "f1=";    f1.display();
    cout << "f2=";    f2.display();
    cout << "f3=f1+f2=";    f3.display();
    f1.setfrac(3, 5);
    f2.setfrac(-9, 15);
    f3 = f1.add(f2);
    cout << "f1=";    f1.display();
    cout << "f2=";    f2.display();
    cout << "f3=f1+f2=";    f3.display();

    cout << "--------测试除法函数-----------" << endl;
    f1.setfrac(-24, 7);
    f2.setfrac(16, 35);
    f3 = f1.div(f2);
    cout << "f1=";    f1.display();
    cout << "f2=";    f2.display();
    cout << "f3=f1/f2=";f3.display();
    f1.setfrac(33,  -45);
    f2.setfrac(-11, 20);
    f3 = f1.div(f2);
    cout << "f1=";    f1.display();
    cout << "f2=";    f2.display();
    cout << "f3=f1/f2=";    f3.display();
    f1.setfrac(12, 35);
    f2.setfrac(-24, 7);
    f3 = f1.div(f2);
    cout << "f1=";    f1.display();
    cout << "f2=";    f2.display();
    cout << "f3=f1/f2=";    f3.display();
    f1.setfrac(-11, 20);
    f2.setfrac(33, -50);
    f3 = f1.div(f2);
    cout << "f1=";    f1.display();
    cout << "f2=";    f2.display();
    cout << "f3=f1/f2=";    f3.display();
    
    cout << "------------测试结束------------" << endl;

    return 0;
}

要说有什么要注意的地方的话,大概就是要注意正负号了。我这里是在display是规范了分数的正负号形式,因为这样写最省事。
还有就是在写约分函数时有个要注意的,不能直接写above = above / gcd(above, below0; below = below / gcd(above, below); 因为第一句修改了above,第二句算below时就会用修改后的above算,刚开始我这里就写错了……
gcd函数我用了辗转相除法,lcm函数则直接用了最小公倍数=两数之积/最大公约数


2

参考答案

#include <iostream>
using namespace std;
class timeC{
    int hour;
    int minute;
    int sec;
public:
    timeC(int h = 0, int m = 0, int s = 0):hour(h), minute(m), sec(s){}
    timeC add(timeC &);
    timeC minus(timeC &);
    void showtimeC();
};
timeC timeC::add(timeC &t)
{
    timeC k;
    k.hour = hour + t.hour;
    k.minute = minute + t.minute;
    k.sec = sec + t.sec;
    int i;
    i = k.sec / 60;
    k.sec = k.sec % 60;
    k.minute += i;
    i = k.minute / 60;
    k.minute = k.minute % 60;
    k.hour += i;
    k.hour = k.hour % 24;
    return k;
}
timeC timeC::minus(timeC &t){
    timeC result;
    result.hour = hour - t.hour;
    result.minute = minute - t.minute;
    result.sec = sec - t.sec;
    if (result.sec < 0){
        result.minute--;
        result.sec += 60;
    }
    if (result.minute < 0){
        result.hour--;
        result.minute += 60;
    }
    if (result.hour < 0)
        result.hour += 24;
    return result;
}
void timeC::showtimeC()
{
    cout << hour << " : " << minute << " : " << sec << endl;
}
int main()
{
    
    timeC t1(21, 12, 34), t2(12, 56, 45);
    
    timeC t3(9,23);
    t3.showtimeC();
    t3 = t3.add(t2);
    t3.showtimeC();
    
    timeC t4;
    t4.showtimeC();
    t4 = t1.minus(t2);
    t4.showtimeC();

    return 0;
}

这道题要写的也就只有构造函数和minus函数,不算难


3

参考答案

#include <iostream>
#include <cstring>
using namespace std;
class opera
{
    char name[30];                      //歌剧名称
    int ts[4];                          // ts[0]:包厢的总数,ts[1]:一等座的总座位数
                                        // ts[2]:二等座的总座位数,ts[3]:三等座的总座位数
    int es[4];                          // es[0]:空包厢的数量,es[1]:一等座的空闲座位数
                                        // es[2]:二等座的空闲座位数,es[3]:三等座的空闲座位数
    int ps[4];                          // ps[0]:包厢的票价,ps[1]:一等座的票价
                                        // ps[2]:二等座的票价,,ps[3]:三等座的票价
    int income;                         //总收入
public:
    opera();                            //默认构造函数,数据成员的初始值请参看输出样例
    void set_name(char *);              //更改剧名
    void set_ps(int []);                //更改座位价格
    void booking(int,int);              //售票,如果余票小于订票数,
                                        //终止该次售票,并输出购票失败提示信息
    void refund(int,int);               //退票,不收手续费
    void print();
    ~opera();                           //析构函数,完成票房统计,内容与格式请参看输出样例
};
opera::opera(){
    strcpy(name, "未命名");
    ts[0] = es[0] = 20; ts[1] = es[1] = 100;
    ts[2] = es[2] = 240; ts[3] = es[3] = 300;
    ps[0] = ps[1] = ps[2] = ps[3] = 0;
    income = 0;
}
void opera::set_name(char *n){ strcpy(name, n); }
void opera::set_ps(int *p){
    for (int i = 0; i < 4; i++)
        ps[i] = p[i];
}
void opera::booking(int rank, int num){
    if (num > es[rank])
        cout << "余票(" << es[rank] << ")不足,购买失败\n";
    else {
        es[rank] -= num;
        income += num * ps[rank];
    }
}
void opera::refund(int rank, int num){
    es[rank] += num;
    income -= ps[rank] * num;
}
void opera::print()
{
    char line[20]="------------------";
    cout<<line<<"戏曲名:"<<name<<line<<endl;

    char st[4][20]={ "包厢","一等座","二等座","三等座"};
    
    for(int i=0;i<4;i++)
        cout<<st[i]<<"<"<<"票价:"<<ps[i]<<" /总数:"<<ts[i]<<" /可售:"<<es[i]<<">"<<endl;

    cout<<line<<"总收入:"<<income<<line<<endl<<endl;
}
opera::~opera(){
    cout << name << "的总收入:" << income << endl;
    cout << name << "的票房如下:\n";
    cout << "包厢售出" << ts[0] - es[0] << endl;
    cout << "一等座售出" << ts[1] - es[1] << endl;
    cout << "二等座售出" << ts[2] - es[2] << endl;
    cout << "三等座售出" << ts[3] - es[3] << endl;
}

int main()
{

    opera p1;
    p1.print();

    p1.set_name("二进宫");//void set_name(char *),更改剧名

    int p[]={700,380,180,80};
    p1.set_ps(p);//void set_ps(int []),更改座位价格

    p1.booking(0,9);//购买9张包厢票
    p1.booking(1,15);//购买15张一等票
    p1.booking(3,20);//购买20张三等票
    p1.print();
    p1.booking(0,13);//购买13张包厢票
    p1.booking(2,14);//购买14张二等票
    p1.refund(3,6);//退6张三等票
    p1.print();
    return 0;
}

4

参考答案

#include <iostream>
#include <cstring>
using namespace std;
class phone{
    char number[12];    //11位本机号码
    char city[5];       //本机归属地编号
    double fee;         //本机话费余额
public:
    phone();                        //默认构造函数,初始值请参看输出样例第一行
    void recharge(double m);        //给手机充值
    void insertcard(const char*,const char*); //设置手机号码和归属地
    double  getfee();               //返回本机话费
    char *getno();                  //返回本机号码
    char *getcity();                //返回归属地
    int call(phone &other,int m);   //和手机other通话m分钟,
    /*  1.归属地相同,本机支付话费,话费为每分钟0.7元。
        2.归属地不同,双方都要支付话费,本机话费为每分钟1.2元,other话费为每分钟0.7元。
        3.通话过程中,如果因为某方话费不足,通话中断,函数结束,返回实际通话时长
        通话正常完成,函数结束,返回m */
};
phone::phone(){ strcpy(number, "08600000000"); strcpy(city, "***"); fee = 0; }
void phone::insertcard(const char *num, const char *ct){
    strcpy(number, num);
    strcpy(city, ct);
}
void phone::recharge(double m){ fee += m; }
double phone::getfee(){ return fee; }
char* phone::getno(){ return number; }
char* phone::getcity(){ return city; }
int phone::call(phone &other, int m){
    double cost1, cost2 = 0;
    int minute = 0;
    if (!strcmp(other.city, city)){
        cost1 = 0.7;
    } else {
        cost1 = 1.2;
        cost2 = 0.7;
    }
    while (minute < m){
        if (fee > cost1 && other.fee > cost2){
            minute++;
            fee -= cost1;
            other.fee -= cost2;
        } else
            break;
    }
    return minute;
}
void display( phone t)
{
    cout << "手机号码:" << t.getno()
         << ";归属地:" << t.getcity()
         << ";话费余额:" << t.getfee()
         << endl;
}
int main()
{
    
    phone p1;
    display(p1);
    p1.insertcard("13458901211", "027");
    p1.recharge(40);
    phone p2, p3;
    p2.insertcard("13652901219", "021");
    p2.recharge(30);
    p3.insertcard("15651004523", "027");
    p3.recharge(50);

    char line[] = "*-------------------------------------------------*";
    int talk, m;
    m = 35;
    talk = p1.call(p2, m);
    cout << line << endl;
    cout << "p1主叫p2......." << endl;
    if (talk == m)
        cout << "通话时长" << talk << "分钟。通话结束,祝您愉快" << endl;
    else
        cout << "通话时长" << talk << "分钟。余额不足,请尽快预存话费" << endl;
    display(p1); display(p2);
    cout << line << endl;

    p1.recharge(30);
    m = 40;
    talk = p3.call(p1, 40);
    cout << line << endl;
    cout << "p3主叫p1......." << endl;
    if (talk == m)
        cout << "通话时长" << talk << "分钟。通话结束,祝您愉快........" << endl;
    else
        cout << "通话时长" << talk << "分钟。电话余额不足.......请尽快预存话费." << endl;
    display(p3); display(p1);
    cout << line << endl;

    return 0;
}

这道题唯一的难点也就在call函数了。不过call函数硬用if-else语句也能凑出来,就是会麻烦点。
由于C++11标准不允许把字符串常量传递给char*,我把insertcard函数的形参改成了const char*,当然一般来说不改也能通过编译,就是一些新的编译器会警告⚠️,看起来不爽。


有时间会写写提高题和第九章
学期也快结束了,提高题也懒得写了
(第九章已更新)
Created by goolwind on 2021/1/6
Edited on 2021/1/11

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值