列表

详情


CPP64. 重载小于号

描述

有一个时间类(Time),成员变量有:小时(hours)、分钟(minutes),补充 Time 类的代码,重载小于号运算符,使得程序能够正确运行。

输入描述

键盘输入两个整数,分别为小时 和分钟 ,其中

输出描述

比较输入时间与6小时6分钟的大小,若输入时间较小则输出"yes",否则输出"no"。

示例1

输入:

6 5

输出:

yes

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 2ms, 内存消耗: 540KB, 提交时间: 2022-03-13

#include <iostream>
using namespace std;

class Time {

    public:
        int hours;      // 小时
        int minutes;    // 分钟

        Time() {
            hours = 0;
            minutes = 0;
        }

        Time(int h, int m) {
            this->hours = h;
            this->minutes = m;
        }

        void show() {
            cout << hours << " " << minutes << endl;
        }

        // write your code here......
        bool operator<(const Time& t){
            return hours*60+minutes < t.hours * 60 + t.minutes;
        }

};

int main() {
    int h, m;
    cin >> h;
    cin >> m;

    Time t1(h, m);
    Time t2(6, 6);
	
    if (t1<t2) cout<<"yes"; else cout<<"no";
    return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 388KB, 提交时间: 2022-05-01

#include <iostream>
using namespace std;

class Time {

    public:
        int hours;      // 小时
        int minutes;    // 分钟

        Time() {
            hours = 0;
            minutes = 0;
        }

        Time(int h, int m) {
            this->hours = h;
            this->minutes = m;
        }

        void show() {
            cout << hours << " " << minutes << endl;
        }

        // write your code here......
        bool operator < (const Time& t )
        {
            return hours * 60 + minutes < t.hours * 60 + t.minutes;
        }
        

};

int main() {
    int h, m;
    cin >> h;
    cin >> m;

    Time t1(h, m);
    Time t2(6, 6);
	
    if (t1<t2) cout<<"yes"; else cout<<"no";
    return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 392KB, 提交时间: 2022-08-01

#include <iostream>
using namespace std;

class Time {

    public:
        int hours;      // 小时
        int minutes;    // 分钟

        Time() {
            hours = 0;
            minutes = 0;
        }

        Time(int h, int m) {
            this->hours = h;
            this->minutes = m;
        }

        void show() {
            cout << hours << " " << minutes << endl;
        }

        // write your code here......
        bool operator < (const Time& t)
        {
            if ((hours *60 + minutes) < (t.hours *60 + t.minutes))
            {
                return true;
            }
            else{
                return false;
            }
        }

};

int main() {
    int h, m;
    cin >> h;
    cin >> m;

    Time t1(h, m);
    Time t2(6, 6);
	
    if (t1<t2) cout<<"yes"; else cout<<"no";
    return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 392KB, 提交时间: 2022-07-27

#include <iostream>
using namespace std;

class Time {

    public:
        int hours;      // 小时
        int minutes;    // 分钟

        Time() {
            hours = 0;
            minutes = 0;
        }

        Time(int h, int m) {
            this->hours = h;
            this->minutes = m;
        }

        void show() {
            cout << hours << " " << minutes << endl;
        }

        // write your code here......
        int operator <(Time& t){
            if(this->hours < t.hours)
                return 1;
            else if(this->hours == t.hours && this -> minutes < t.minutes)
                return 1;
            else 
                return 0;
        }

};

int main() {
    int h, m;
    cin >> h;
    cin >> m;

    Time t1(h, m);
    Time t2(6, 6);
	
    if (t1<t2) cout<<"yes"; else cout<<"no";
    return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 392KB, 提交时间: 2022-05-22

#include <iostream>
using namespace std;

class Time {

    public:
        int hours;      // 小时
        int minutes;    // 分钟

        Time() {
            hours = 0;
            minutes = 0;
        }

        Time(int h, int m) {
            this->hours = h;
            this->minutes = m;
        }

        void show() {
            cout << hours << " " << minutes << endl;
        }

        // write your code here......
        bool operator<(const Time& t){
            return this->hours*60+this->minutes<t.hours*60+t.minutes;
        }
    

};

int main() {
    int h, m;
    cin >> h;
    cin >> m;

    Time t1(h, m);
    Time t2(6, 6);
	
    if (t1<t2) cout<<"yes"; else cout<<"no";
    return 0;
}

上一题