#include <iostream> using namespace std; /* 请在这里填写答案 */ template<class T> T max(T a,T b) //无法运行 原因是std有max这个东西 解决方法改名 { if(a>b)return a; else return b; } int main( ) { int a,b; cin>>a>>b; cout<<max(a,b)<<" ";//<<min(a,b); return 0; }
template<class T> class MyArray { private: T data[100000]; //用数组出现错误 int size; public: ~MyArray(); MyArray(int size); bool check(); void sort(); void display(); }; template<class T> MyArray<T>::MyArray(int size) { this->size = size; for(int i=0;i<this->size;i++) cin>>data[i]; } template<class T> void MyArray<T>::sort() { for(int i=0;i<size;i++) for(int j=i;j<size;j++) if(data[i]>data[j]) {T temp;temp=data[i];data[i]=data[j];data[j]=temp;} } template<class T> void MyArray<T>::display() { for(int i=0;i<size-1;i++) cout<<data[i]<<" "; cout<<data[size-1]<<endl; }
template<class T> class MyArray { private: T *data; //不用数组,用指针 int size; public: ~MyArray(); MyArray(int size); bool check(); void sort(); void display(); }; template<class T> MyArray<T>::MyArray(int size) { this->size = size; data=new T[100]; for(int i=0;i<this->size;i++) cin>>data[i]; } template<class T> void MyArray<T>::sort() { for(int i=0;i<size;i++) for(int j=i;j<size;j++) if(data[i]>data[j]) {T temp;temp=data[i];data[i]=data[j];data[j]=temp;} } template<class T> void MyArray<T>::display() { for(int i=0;i<size-1;i++) cout<<data[i]<<" "; cout<<data[size-1]<<endl; }
定义一个时间类time,内有数据成员hour,minute,second,另有成员函数:构造函数用于初始化数据成员,输出函数,运算符重载+(加号),。编写主函数:创建时间对象,再输入秒数 n,通过运算符重载+(减号),计算该时间再过 n 秒后的时间值,时间的表示形式为时:分:秒,超过 24 时从 0 时重新开始计时。
测试输入包含若干测试用例,每个测试用例占一行。当读入0 0 0 0时输入结束,相应的结果不要输出。
输入输出示例:括号内为说明
0 0 1 59 (时间为0:0:1,秒数n=59)
11 59 40 30 (时间为11:59:40,秒数n=30)
23 59 40 3011 (时间为23:59:40,秒数n=3011)
0 0 0 0
输出:
time:0:1:0↙(0:0:01加上59秒的新时间)
time:12:0:10↙(11:59:40加上30秒的新时间)
time:0:49:51↙(23:59:40加上3011秒的新时间)
# include<iostream> using namespace std; class Time { private: long long hour,minute,second; public: friend Time operator+(Time &now,long long n); void display(); Time(long long h,long long m,long long s); Time(); }; Time::Time(long long h,long long m,long long s) { hour = h; minute = m; second = s; } void Time::display() { cout<<"time:"<<hour<<":"<<minute<<":"<<second<<endl; } Time operator+(Time &now,long long n) { now.second += n; if(now.second>=60) { now.minute += now.second / 60; now.second = now.second % 60; } else if(now.second<0) //坑点 会出现负数的情况 { now.minute -= (-now.second)/60 +1; now.second += ((-now.second)/60 +1) * 60; } if(now.minute>=60) { now.hour += now.minute / 60; now.minute = now.minute % 60; } else if(now.minute<0) { now.hour-= (-now.minute)/60 +1; now.minute += ((-now.minute)/60 +1) * 60; } if(now.hour>=24) now.hour = now.hour % 24; else if(now.hour<0) now.hour += ((-now.hour)/24 +1 ) * 24; return now; } int main() { long long h,m,s,n; cin>>h>>m>>s>>n; while(h!=0||s!=0||n!=0||m!=0) { Time now(h,m,s); Time he = now + n; he.display(); cin>>h>>m>>s>>n; } return 0; }