这次不涉及什么算法,都是模拟策略。
29 显示时间
注:这道题应该是出错了,时间都是显示成0:00:00这样的形式的,可以在测试数据里竟然有0:0:0这样的答案,导致我多提交了一次。
#include <iostream> #include <iomanip> using namespace std; template<class Tp> class typTime { private: Tp hour,minute,second; public: typTime() { hour=0; minute=0; second=0; } typTime(typTime<Tp> &_t) { hour=_t.hour; minute=_t.minute; second=_t.second; } void settime(Tp _hour,Tp _minute,Tp _second) { hour=_hour; minute=_minute; second=_second; } void show() { cout<<hour<<':'<<setfill('0')/*<<setw(2)*/<<minute<<':'/*<<setw(2)*/<<second<<endl; } }; int main() { int h,m,s; cin>>h>>m>>s; typTime<int> Time; Time.settime(h,m,s); Time.show(); //system("pause"); return 0; }
30 求三个数中的量大值
#include <iostream> #include <iomanip> using namespace std; template<class Tp> Tp max(Tp _a,Tp _b,Tp _c) { Tp _t=_a; if (_t<_b) _t=_b; if (_t<_c) _t=_c; return _t; } int main() { int a,b,c; cin>>a>>b>>c; cout<<"Maximum:"<<max(a,b,c)/*<<"."*/<<endl; //system("pause"); return 0; }
31 分解5位整数
注:这道题本来我是写成了边读边输出的形式,结果不知道为什么输出结果反序了,可能是由于”<<“运算是右结合吧。 还有更囧的,就是题上说五位整数,我就真当成了五位整数,结果其实它是低于五位的整数。
#include <iostream> #include <iomanip> using namespace std; int main() { int i; char c; for (i=0;i<5;i++) { c=cin.get(); if (c=='\n') break; else { if (i) cout<<" "; cout<<(char)c; } } cout<<endl; return 0; }
32 求圆柱体体积
#include <iostream> #include <iomanip> #define PI 3.14 using namespace std; template<class Tp,class Tp2> class theStuff{ private: Tp _r,_h; Tp2 _s,_v; public: theStuff(Tp __r,Tp __h){ set(__r,__h); } void set(Tp __r,Tp __h) { _r=__r; _h=__h; _s=_r*_r*PI; _v=_s*_h; } Tp volume() { return _v; } Tp Square() { return _s; } }; int main() { double r,h; cin>>r>>h; theStuff<double,double> stuff(r,h); cout<<"v="<<stuff.volume()<<endl; system("pause"); return 0; }
33 设置、输出日期
注:这道题更恶心,输出的日期竟然是1900/1/1/形式的……
#include <iostream> #include <iomanip> using namespace std; template <class Tp> class Date{ private: Tp year,month,day; public: Date(){ year=1900; month=1; day=1; } Date(Tp y,Tp m,Tp d) { SetDate(y,m,d); } void SetDate(Tp y,Tp m,Tp d) { year=y; month=m; day=d; } Tp GetDay() { return day; } Tp GetMonth() { return month; } Tp GetYear() { return year; } void ShowDate() { cout<<year<<"/"<<month<<"/"<<day<<"/"<<endl; } }; int main() { int y,m,d; cin>>y>>m>>d; Date<int> date(y,m,d); date.ShowDate(); return 0; }
Leave a Reply
You must be logged in to post a comment.