TopCoder

User's AC Ratio

NaN% (0/0)

Submission's AC Ratio

NaN% (0/0)

Tags

Description

● 題目敘述

函式題,實作函式即可,輸入輸出已經寫好了。題目中使用的code在最下面。

定義一個類別 class Pair ,其包含的成員如下 (:)

template<class T1,class T2> 
class Pair{
private:
    T1 first;
    T2 second;
    int index;
public:
    // Pair(); 本題不需要實作這個
    Pair(T1,T2,int);
    ~Pair();
    void print_first_and_second();
};
你需要實作三個函式。

. Pair 的 constructor : Pair(T1,T2,int);
. Pair 的 destructor, : ~Pair();
. Pair 的 member function , 用來輸出first,second的值 : void print_first_and_second();

● Code

#include <iostream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;

template<typename T1,typename T2>
class Pair{
private:
    T1 first;
    T2 second;
    int index;
public:
    // Pair(); 本題不需要實作這個
    Pair(T1,T2,int);
    ~Pair();
    void print_first_and_second();
};

/******************************/
/* Your code will be put here */









/******************************/

int main() {
    /* str表示int或double */
    char str[10];
    cin >> str;

    /* N表示輸入的組數 */
    int N;
    cin >> N;

    /* 
    兩個if都會建立Pair的物件      
    分別傳入                     
    1. <int,int>                
    2. <double,double>          
    作為Pair的first和second型態的引數 
    E.g., typeof(first) = int    , typeof(second) = int 
    Or    typeof(first) = double , typeof(second) = double 
    */
    if(!(strcmp(str,"int"))){
        int first,second;
        for(int i=0;i<N;i++){
            cin >> first >> second;
            Pair<int,int> p(first,second,i);
            p.print_first_and_second();
        }
    }
    if(!(strcmp(str,"double"))){
        double first,second;
        for(int i=0;i<N;i++){
            cin >> first >> second;
            Pair<double,double> p(first,second,i);
            p.print_first_and_second();
        }
    } 

    return 0;
}

Input Format

第一行輸入 \(int\) or \(double\),代表 \(Pair.first\) 和 \(Pair.second\) 的型態。

第二行輸入一個整數 \(N\) , 代表接下來將要創建 \(N\) 個 \(Pair\) 的物件 \(p_i\)。

剩下的 \(N\) 行,每行輸入兩數 \(fisrt_i\)、\(second_i\),代表物件 \(p_i\) 的 \(first\) 和 \(second\) 值。

Output Format

每次創建 \(p_i\) 時,輸出 construct pair[i] ,並換一行。這裡的 i 請代換成實際的數字,e.g.,i=0,1,2,......

每次 \(p_i\) 被釋出記憶體時,輸出 destruct pair[i] ,並換兩行。這裡的 i 請代換成實際的數字,e.g.,i=0,1,2,......

呼叫 print_first_and_second( ) 時,輸出 \(p_i\) 的 \(first\)、\(second\) 值,並換一行

若是看不懂輸出說明歡迎向講師詢問 >w<

也可以參考範例測資的輸出喔。

Sample Input 1

int
2
0 0
1 1

Sample Output 1

construct pair[0]
p[0].first=0 and p[0].second=0
destruct pair[0]

construct pair[1]
p[1].first=1 and p[1].second=1
destruct pair[1]

Sample Input 2

double
3
1.5 5.5
5.5 3.5
10.5 20.5

Sample Output 2

construct pair[0]
p[0].first=1.5 and p[0].second=5.5
destruct pair[0]

construct pair[1]
p[1].first=5.5 and p[1].second=3.5
destruct pair[1]

construct pair[2]
p[2].first=10.5 and p[2].second=20.5
destruct pair[2]

Hints

● 輸出補充

testdata 1. 是範測 1 。可以利用這個來確認輸出格式有沒有正確

testdata 2. 是範測 2 。可以利用這個來確認輸出格式有沒有正確

testdata 3. 全部是 int 。

testdata 4. 全部是 double。

Problem Source

NEOJ Problem 597

Subtasks

No. Testdata Range Constraints Score
1 0 0
2 1 0
3 2~11 50
4 12~21 50

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 1000 65536 65536 1
1 1000 65536 65536 2
2 1000 65536 65536 3
3 1000 65536 65536 3
4 1000 65536 65536 3
5 1000 65536 65536 3
6 1000 65536 65536 3
7 1000 65536 65536 3
8 1000 65536 65536 3
9 1000 65536 65536 3
10 1000 65536 65536 3
11 1000 65536 65536 3
12 1000 65536 65536 4
13 1000 65536 65536 4
14 1000 65536 65536 4
15 1000 65536 65536 4
16 1000 65536 65536 4
17 1000 65536 65536 4
18 1000 65536 65536 4
19 1000 65536 65536 4
20 1000 65536 65536 4
21 1000 65536 65536 4