山东科技大学2020年5月21日实验题解

山东科技大学2020年5月21日实验题解

第一题: 新奇的加法运算

Description

定义类newInt,包括:

  1. int类型的数据成员。

  2. 重载运算符“+”。计算规则为:将A、B对应位置上的数字相加,只保留个位数作为结果的对应位置上的数字。比如:876 + 543 = 319。注意:该运算不改变两个操作数的值。

  3. 重载输入和输出运算符,用于输入和输出对象的属性值。

  4. 无参构造函数和带参构造函数。

Input

第1行N>0,表示测试用例数量。

每个测试用例包括2个非负整数,用空格隔开。

Output

见样例。

Sample Input

4
876 543
999 9999
9 1999
199 88

Sample Output

876 + 543 = 319
999 + 9999 = 9888
9 + 1999 = 1998
199 + 88 = 177

题目给定代码

int main()
{
    int cases;
    newInt a, b, c;
    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
        cin>>a>>b;
        c = a + b;
        cout<<a<<" + "<<b<<" = "<<c<<endl;
    }
    return 0;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class newInt {
private:
    int i;
public:
    newInt operator + (newInt& n) {
        int x = i;
        int y = n.i;
        int s = 0;
        if (x >= y) {
            int a = x, l = 1, mode = 1;
            a /= 10;
            while (a) {
                a /= 10, ++ l;
            }
            for (R int j = 0; j < l; ++j) {
                s += ((x % 10 + y % 10) % 10) * mode, mode *= 10, x /= 10, y /= 10;
            }
        }
        else {
            int a = y, l = 1, mode = 1;
            a /= 10;
            while (a) {
                a /= 10, ++l;
            }
            for (R int j = 0; j < l; ++j) {
                s += ((x % 10 + y % 10) % 10) * mode, mode *= 10, x /= 10, y /= 10;
            }
        }
        return newInt(s);
    }
    friend istream& operator >> (istream& is, newInt& n) {
        is >> n.i;
        return is;
    }
    friend ostream& operator << (ostream & os, const newInt & n) {
        os << n.i;
        return os;
    }
    newInt(int a = 0) {
        i = a;
    }
};

int main()
{
    int cases;
    newInt a, b, c;
    cin >> cases;
    for (int i = 0; i < cases; i++)
    {
        cin >> a >> b;
        c = a + b;
        cout << a << " + " << b << " = " << c << endl;
    }
    return 0;
}

题目二: 重载字符的加减法

Description

定义一个字符类Character,只有一个char类型的数据成员。

重载它的+、-、<<和>>运算符,其中+、-的第二个操作数是int类型的整数n。“+”用于返回以当前字符之后的第n个字符为属性值的对象,“-”用于返回当前字符之前的第n个字符为属性值的对象。如样例所示。

Input

第1行N>0表示测试用例个数。

每个测试用包括1个字符(小写英文字母)和1个int类型的整数。

Output

输出有N行,每行输入对应一行输出,每行输出包括对应输入字符之后的第n个字符,以及该字符之前的第n个字符。如样例中第2个用例输入字符是“a”,整数是“1”,那么“a”之后的第1个字符是”b“,"a"之前的第1个字符是”z“;注意:输入的整数可能是负数。

Sample Input

3
a 0
a 1
a -1

Sample Output

a a
b z
z b

题目给定代码

int main()
{
    int cases, data;
    Character ch;
    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
        cin>>ch;
        cin>>data;
        cout<<(ch + data)<<" "<<(ch - data)<<endl;
    }
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Character {
private:
    char c;
public:
    Character(char ch = '\0') {
        c = ch;
    }
    friend char operator + (Character ch, int n) {
        n %= 26, ch.c += n;
        if (ch.c > 'z') {
            ch.c -= 26;
        }
        else if (ch.c < 'a') {
            ch.c += 26;
        }
        return ch.c;
    }
    friend char operator - (Character ch, int n) {
        n %= 26, ch.c -= n;
        if (ch.c < 'a') {
            ch.c += 26;
        }
        else if (ch.c > 'z') {
            ch.c -= 26;
        }
        return ch.c;
    }
    friend istream& operator >> (istream& is, Character& ch) {
        is >> ch.c;
        return is;
    }
    friend ostream& operator << (ostream& os, Character& ch) {
        os << ch.c;
        return os;
    }
};

int main()
{
    int cases, data;
    Character ch;
    cin >> cases;
    for (int i = 0; i < cases; i++)
    {
        cin >> ch;
        cin >> data;
        cout << (ch + data) << " " << (ch - data) << endl;
    }
}

题目三: 成绩排序

Description

定义Student类:

  1. 数据成员string name和int score表示一个学生的姓名、成绩。

  2. 无参构造函数。

  3. void setStudent(string,int)方法,用于设置一个学生的属性值。

  4. 重载>(大于运算符)。Student类的对象A和B的大小关系,A>B定义为A.score>B.score,或者A.score=B.score但A.name<B.name。

5.重载运算符<<用于输出学生信息:先输出成绩再输出姓名,中间用一个空格隔开。

Input

分多行。第一个M>0表示有M个学生信息。

之后有M行,每一行是一个学生信息。第一部分是学生姓名,第二部分是学生成绩。

Output

输出为M行,按照从大到小的顺序依次输出每个学生的成绩、姓名。假定不存在重名的学生。

Sample Input

5
Tom 98
Jack 97
Mary 98
Sherry 99
Dock 97

Sample Output

99 Sherry
98 Mary
98 Tom
97 Dock
97 Jack

题目给定代码

int main()
{
    int cases;
    string name;
    int score;
    cin>>cases;
    Student students[cases], temp;
    for (int i = 0; i < cases; i++)
    {
       cin>>name>>score;
       students[i].setStudent(name, score);
    }
    for (int i = 0; i < cases; i++)
    {
        for (int j = 0; j < cases - i - 1; j++)
        {
            if (!(students[j] > students[j + 1]))
            {
                temp = students[j];
                students[j] = students[j + 1];
                students[j + 1] = temp;
            }
        }
    }
    for (int i = 0; i < cases; i++)
        cout<<students[i]<<endl;
    return 0;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Student {
private:
    string name;
    int score;
public:
    Student() {
        name = "", score = 0;
    }
    void setStudent(string s, int number) {
        name = s, score = number;
    }
    int operator > (Student& a) {
        if (score > a.score) {
            return 1;
        }
        else if (score == a.score) {
            if (name < a.name) {
                return 1;
            }
        }
        return 0;
    }
    friend ostream& operator << (ostream& os, const Student& S) {
        os << S.score << " " << S.name;
        return os;
    }
};

int main()
{
    int cases;
    string name;
    int score;
    cin >> cases;
    Student students[cases], temp;
    for (int i = 0; i < cases; i++)
    {
        cin >> name >> score;
        students[i].setStudent(name, score);
    }
    for (int i = 0; i < cases; i++)
    {
        for (int j = 0; j < cases - i - 1; j++)
        {
            if (!(students[j] > students[j + 1]))
            {
                temp = students[j];
                students[j] = students[j + 1];
                students[j + 1] = temp;
            }
        }
    }
    for (int i = 0; i < cases; i++)
        cout << students[i] << endl;
    return 0;
}

题目四: 整型数组运算符重载

Description

定义Array类:

1.拥有数据成员int length和int *mems,分别是数组中元素的个数和元素列表。

  1. 无参构造函数,将mems设置为NULL,length为0。

  2. 重载==运算符,用于判断两个Array对象是否相等。相等包括两种情况:(1)两个对象是同一个对象,即它们拥有相同的地址(记住: this指针指向当前对象,是当前对象的地址);(2)两个对象的length相同,且mems中对应元素的值相同。其他情况均为不相等。

  3. 利用友元函数重载<<和>>运算符。输入、输出格式见下。

Input

输入分多行。

第一行是一个正整数M,表示有M个数组。

每个数组是一行,其中第一个非负整数N表示该数组的元素个数,之后有N个整数。

Output

输出有M行。

第一行输出即为第一个数组。

自第二行开始,首先输出对应的数组元素(两两之间用空格隔开,首尾不能有空格),如果数组为空,则不输出元素。之后根据这个数组与上个数组是否相同,输出“unequal to above.”(不相等)和“equal to above”(相等)。

Sample Input

5
3 1 2 3
3 1 2 3
0
7 1 2 3 4 5 6 7
7 1 2 3 4 5 6 8

Sample Output

1 2 3
1 2 3 equal to above.
unequal to above.
1 2 3 4 5 6 7 unequal to above.
1 2 3 4 5 6 8 unequal to above.

题目给定代码

int main()
{
    int cases;
    cin>>cases;
    Array arraies[cases];
    for (int i = 0; i < cases; i++)
    {
        cin>>arraies[i];
    }
    cout<<arraies[0]<<endl;
    for (int i = 1; i < cases; i++)
    {
        if (arraies[i] == arraies[i - 1])
        {
            cout<<arraies[i]<<" "<<"equal to above."<<endl;
        }
        else
        {
            cout<<arraies[i]<<" "<<"unequal to above."<<endl;
        }
    }
    return 0;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Array {
private:
    int length, * mems;
public:
    Array() {
        length = 0, mems = NULL;
    }
    bool operator == (Array& a) {
    	R int i;
        for (i = 0; i < length; ++i) {
            if (mems[i] != a.mems[i]) {
				break;
            }
        }
        if (this == &a) {
            return 1;
        }
        else if (a.length == length && length == i) {
            return 1;
        }
        else {
            return 0;
        }
    }
    friend ostream& operator << (ostream& os, Array& a) {
        for (R int i = 0; i < a.length; ++i) {
            if (i != a.length - 1) {
                os << a.mems[i] << " ";
            }
            else {
                os << a.mems[i];
            }
        }
        return os;
    }
    friend istream& operator >> (istream& is, Array& a) {
        is >> a.length;
        a.mems = new int[a.length];
        for (R int i = 0; i < a.length; ++i) {
            is >> a.mems[i];
        }
        return is;
    }
    ~Array() {
        if (mems != NULL) {
            delete[]mems;
        }
    }
};

int main()
{
    int cases;
    cin >> cases;
    Array arraies[cases];
    for (int i = 0; i < cases; i++)
    {
        cin >> arraies[i];
    }
    cout << arraies[0] << endl;
    for (int i = 1; i < cases; i++)
    {
        if (arraies[i] == arraies[i - 1])
        {
            cout << arraies[i] << " " << "equal to above." << endl;
        }
        else
        {
            cout << arraies[i] << " " << "unequal to above." << endl;
        }
    }
    return 0;
}

题目五: 时间类的流插入、提取和递增、递减运算

Description

封装一个时间类Time,在类上重载以下运算符,使得main()函数能够正确运行。
流插入操作符“>>”,按照输入格式从标准输入读取三个整数:hh,mm,ss,分别表示时、分、秒,其值在int范围内。
流提取操作符“<<”;按照“hh:mm:ss”输出Time类的对象,不合法的时间输出“error!!!”。
前置自增运算符“++”:把时间对象的秒数加1并返回。
前置自减运算符“–”:把时间对象的秒数减1并返回。
后置自增运算符“++”:把时间对象的秒数加1,返回原值。
后置自减运算符“–”:把时间对象的秒数减1,返回原值。
以上4个自增、自减仅对合法的时间操作,并且不会产生不合法的时间。比如:
若原时间对象为“00:00:00”,自减运算后的对象为“23:59:59”;
若原时间对象为“23:59:59”,自增运算后的对象为“00:00:00”;
若原时间对象为“24:60:60”,自增或自减运算后对象仍为“24:60:60”。
函数调用格式见append.cc。
append.cc中已给出main()函数

Input

输入的第一个整数n,表示有n组测试数据,每组3个整数:hh,mm,ss,分别表示时、分、秒,其值都在int范围内。

Output

输出一张表:每列8个字符宽,两列之间有一个空格。
首先,输出一个表头:“++t --t t t-- t++ t ”,
其次,对应每组测试数据在一行内依次以下内容:
前置++、前置–、原值、后置–、后置++、原值。
若输入的日期合法,输出格式为“hh:mm:ss”,不足两位的输出需要前面补0。如果输入的时间不合法,则输出“error!!!”。格式见sample。

Sample Input

6
0 0 1
0 59 59
1 1 60
23 0 0
23 59 59
24 1 0

Sample Output

++t --t t t-- t++ t
00:00:02 00:00:01 00:00:01 00:00:01 00:00:00 00:00:01
01:00:00 00:59:59 00:59:59 00:59:59 00:59:58 00:59:59
error!!! error!!! error!!! error!!! error!!! error!!!
23:00:01 23:00:00 23:00:00 23:00:00 22:59:59 23:00:00
00:00:00 23:59:59 23:59:59 23:59:59 23:59:58 23:59:59
error!!! error!!! error!!! error!!! error!!! error!!!

题目给定代码

int main()
{
    Time t;
    int cases;
    cin>>cases;
    cout<<setw(8)<<left<<"++t"<<" ";
    cout<<setw(8)<<left<<"--t"<<" ";
    cout<<setw(8)<<left<<"t"<<" ";
    cout<<setw(8)<<left<<"t--"<<" ";
    cout<<setw(8)<<left<<"t++"<<" ";
    cout<<setw(8)<<left<<"t"<<right<<endl;
    for(int i = 1; i <= cases; ++i)
    {
        cin>>t;
        cout<<(++t)<<" ";
        cout<<(--t)<<" ";
        cout<<t<<" ";
        cout<<t--<<" ";
        cout<<t++<<" ";
        cout<<t<<endl;
    }
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Time {
private:
    int hour, minute, second;
public:
    Time() {
        hour = 0, minute = 0, second = 0;
    }
    friend ostream& operator << (ostream& os, const Time& T) {
        if ((T.hour >= 0 && T.hour < 24) && (T.minute >= 0 && T.minute < 60) && (T.second >= 0 && T.second < 60)) {
            os << setw(2) << setfill('0') << T.hour << ":" << setw(2) << setfill('0') << T.minute << ":" << setw(2) << setfill('0') << T.second;
        }
        else {
            os << "error!!!";
        }
        return os;
    }
    friend istream& operator >> (istream& is, Time& T) {
        is >> T.hour >> T.minute >> T.second;
        return is;
    }
    Time& operator ++() {
        if ((hour >= 0 && hour < 24) && (minute >= 0 && minute < 60) && (second >= 0 && second < 60)) {
            ++second;
            if (second >= 60) {
                second -= 60, ++minute;
            }
            if (minute >= 60) {
                minute -= 60, ++hour;
            }
            if (hour >= 24) {
                hour -= 24;
            }
        }
        return *this;
    }
    Time operator ++(int) {
        Time q = (*this);
        if ((hour >= 0 && hour < 24) && (minute >= 0 && minute < 60) && (second >= 0 && second < 60)) {
            ++second;
            if (second >= 60) {
                second -= 60, ++minute;
            }
            if (minute >= 60) {
                minute -= 60, ++hour;
            }
            if (hour >= 24) {
                hour -= 24;
            }
        }
        return q;
    }
    Time& operator --() {
        if ((hour >= 0 && hour < 24) && (minute >= 0 && minute < 60) && (second >= 0 && second < 60)) {
            --second;
            if (second < 0) {
                second += 60, --minute;
            }
            if (minute < 0) {
                minute += 60, --hour;
            }
            if (hour < 0) {
                hour += 24;
            }
        }
        return *this;
    }
    Time operator --(int) {
        Time q = (*this);
        if ((hour >= 0 && hour < 24) && (minute >= 0 && minute < 60) && (second >= 0 && second < 60)) {
            --second;
            if (second < 0) {
                second += 60, --minute;
            }
            if (minute < 0) {
                minute += 60, --hour;
            }
            if (hour < 0) {
                hour += 24;
            }
        }
        return q;
    }
};

int main()
{
    Time t;
    int cases;
    cin >> cases;
    cout << setw(8) << left << "++t" << " ";
    cout << setw(8) << left << "--t" << " ";
    cout << setw(8) << left << "t" << " ";
    cout << setw(8) << left << "t--" << " ";
    cout << setw(8) << left << "t++" << " ";
    cout << setw(8) << left << "t" << right << endl;
    for (int i = 1; i <= cases; ++i)
    {
        cin >> t;
        cout << (++t) << " ";
        cout << (--t) << " ";
        cout << t << " ";
        cout << t-- << " ";
        cout << t++ << " ";
        cout << t << endl;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
针对2018年高教社杯全国大学生数学建模竞赛D题,我们可以采取以下步骤进行解题: 1. 确定问题:该题的问题是要求我们设计一种算法,能够在给定的网络拓扑结构下,计算出任意两个节点之间的最短路径长度。 2. 分析问题:该题的难点在于如何处理网络中存在的环路和负权边,这些都会影响到最短路径的计算。因此,我们需要选择一种合适的算法来解决这些问题。常用的算法包括Dijkstra算法、Bellman-Ford算法、Floyd算法等。 3. 确定算法:鉴于本题的网络规模较小,我们可以考虑使用Floyd算法来解决。Floyd算法适用于任意两点之间的最短路径计算,可以同时处理有向图和无向图、带权图和不带权图等多种情况,同时也能够处理负权边和环路。 4. 实现算法:实现Floyd算法的关键是构造一个邻接矩阵,表示网络中各个节点之间的距离。具体实现过程可以参考以下步骤: a. 初始化邻接矩阵:将所有节点之间的距离初始化为正无穷大,将每个节点到自己的距离初始化为0。 b. 利用邻接矩阵进行计算:对于每一对节点i和j,遍历所有节点k,比较节点i到k再到节点j的距离和节点i到节点j的距离,取最小值更新邻接矩阵中的距离值。 c. 输出结果:遍历邻接矩阵,输出任意两个节点之间的最短路径长度。 5. 检验算法:为了验证算法的正确性,可以选择一些节点进行测试,比较计算结果与实际情况是否一致。 综上所述,通过采用Floyd算法,我们可以有效地解决2018年高教社杯全国大学生数学建模竞赛D题中的最短路径问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值