YTU-OJ-Problem L:問題型を熟知——自由設計(比較サイズ-クラステンプレート)

2744 ワード

Problem L:問題型に詳しい——自由設計(比較サイズ-クラステンプレート)
Time Limit: 1 Sec  
Memory Limit: 128 MB
Submit: 94  
Solved: 79
[ Submit][ Status][ Web Board]
Description
クラステンプレートを宣言し、2つの整数、浮動小数点数、文字の比較をそれぞれ実現し、大数と小数を求めます.説明:クラステンプレートの外に各メンバー関数を定義します.
Input
2つの整数、2つの浮動小数点数、および2つの文字を入力します.
Output
大きいから小さいまで2つの整数、2つの浮動小数点数、2つの文字を出力します.
Sample Input
3 7
45.78 93.6
a A

Sample Output
7 3
93.60 45.78
a A

HINT
前置コードおよびタイプ定義は、コミット時に含める必要がなく、プログラムの前部/*C++コード*/#include#includeusing namespace stdに自動的に追加されます.template class Compare { public: Compare(numtype a,numtype b); numtype max(); numtype min(); private: numtype x,y; }; プライマリ関数は次のように指定されています.コミット時に含める必要はありません.プログラムの末尾/*C++コード*/int main(){int i 1,i 2;cin>>i 1>>i 2;Comparecmp 1(i 1,i 2);cout<>f 1>>f 2;Comparecmp 2(f 1,f 2);cout>c1>>c2; Compare cmp3(c1,c2); cout<#include <iostream> #include <iomanip> using namespace std; template<class numtype> class Compare { public: Compare(numtype a,numtype b); numtype max(); numtype min(); private: numtype x,y; }; // template<class numtype> Compare<numtype>::Compare (numtype a,numtype b) { x=a;y=b; } template<class numtype> numtype Compare<numtype>::max() { return(x>y)?x:y; } template<class numtype> numtype Compare<numtype>::min() { return(x<y)?x:y; } /* C++ */ int main() { int i1,i2; cin>>i1>>i2; Compare<int> cmp1(i1,i2); cout<<cmp1.max()<<" "<<cmp1.min()<<endl; float f1,f2; cin>>f1>>f2; Compare<float> cmp2(f1,f2); cout<<setiosflags(ios::fixed); cout<<setprecision(2); cout<<cmp2.max()<<" "<<cmp2.min()<<endl; char c1,c2; cin>>c1>>c2; Compare<char> cmp3(c1,c2); cout<<cmp3.max()<<" "<<cmp3.min()<<endl; return 0; }