Problem B: Quantity of class templates
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 476 Solved: 348
[Submit][Status][Web Board]
Description
Define a class template Data to wrap the basic data types int and double in C++. It includes:
1. Data member value is the value wrapped by the object.
2. Parametric constructors (initialization value is 0) and parametric constructors.
3. Overloaded operators: >, <, +and <<. Where "+" returns and does not change the value of the two operands.
4. The member function setValue is used to set the value.
Define another class template GetResult, which has only three static member functions (the following "T" is a type parameter):
1. Static Data < T > getSum (Data < T > * arr, int num): Find the sum of num data objects stored in arr and return a data object composed of this sum.
2. Static Data < T > getMax (Data < T > * arr, int num): Find the maximum value of num data objects stored in arr, and return the object corresponding to the maximum value.
3. Static Data < T > getMin (Data < T > * arr, int num): Find the minimum value of num data objects stored in arr, and return the object corresponding to the minimum value.
Input
The input is divided into multiple lines.
The first line M > 0 indicates that there are M test cases.
Only M lines have one letter I or D at the beginning of each line, and the second is a positive integer N > 0. If the first letter is i, it means that the line contains N int-type data; if the first letter is d, it means that the line has N double-type data.
Output
Except for the first six lines of output, the number of lines of other output is equal to M. Each row outputs three data: the maximum, minimum and sum of the corresponding test cases. Real output fixed-point decimal, and only output 2-bit decimal.
Sample Input
Sample Output
HINT
Append Code
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <set> #include <iomanip> using namespace std; template <typename T> class Data { public: T value; Data() { value = (T)0; } Data(T v) { value = v; } bool operator < (const Data & rhs) { return value < rhs.value; } bool operator >(const Data & rhs) { return value > rhs.value; } Data operator + (const Data & rhs) { Data ans; ans.value = value + rhs.value; return ans; } void setValue(T v) { value = v; } friend ostream & operator << (ostream & os, const Data & d) { os << setprecision(2) << fixed << d.value; return os; } }; template <typename T> class GetResult { public: static Data<T> getSum(Data<T> * arr, int num) { Data<T> ans; for (int i = 0; i < num; i++) { ans = ans + arr[i]; } return ans; } static Data<T> getMax(Data<T> * arr, int num) { Data<T> ans; ans.value = arr[0].value; for (int i = 0; i < num; i++) { if (ans < arr[i]) ans.value = arr[i].value; } return ans; } static Data<T> getMin(Data<T> * arr, int num) { Data<T> ans; ans.value = arr[0].value; for (int i = 0; i < num; i++) if (ans > arr[i]) ans.value = arr[i].value; return ans; } }; int main() { Data<int> iData[1001]; Data<double> dData[1001]; int cases, num; char ch; int u; double v; Data<int> a(10), b(20); Data<double> c(3.14), d(-4.1); cout<<"a + b = "<<(a + b)<<endl; cout<<"max(a, b) = "<<(a > b ? a : b)<<endl; cout<<"min(a, b) = "<<(a < b ? a : b)<<endl; cout<<"c + d = "<<(c + d)<<endl; cout<<"max(c, d) = "<<(c > d ? c : d)<<endl; cout<<"min(c, d) = "<<(c < d ? c : d)<<endl; cin>>cases; for (int i = 0; i < cases; i++) { cin>>ch; cin>>num; for (int j = 0; j < num; j++) { if (ch == 'i') { cin>>u; iData[j].setValue(u); } else if (ch == 'd') { cin>>v; dData[j].setValue(v); } } if (ch == 'i') { cout<<GetResult<int>::getMax(iData, num); cout<<" "<<GetResult<int>::getMin(iData, num); cout<<" "<<GetResult<int>::getSum(iData, num)<<endl; } else if (ch == 'd') { cout<<GetResult<double>::getMax(dData, num); cout<<" "<<GetResult<double>::getMin(dData, num); cout<<" "<<GetResult<double>::getSum(dData, num)<<endl; } } return 0; }