C 模板練習

2021-09-18 00:04:45 字數 1343 閱讀 4981

總時間限制: 

1000ms

記憶體限制: 

65536kb

// 在此處補充你的**
描述

程式填空,輸出指定結果

#include #include #include #include using namespace std;

template struct closer ;

int distance1(int n1,int n2)

int distance2(const string & s1, const string & s2)

int a[10] = ;

string b[6] = ;

int main()

return 0;

}

輸入

多組資料,每組一行,是乙個整數n和乙個字串s

輸出定義兩個整數的距離為兩個整數差的絕對值

定義兩個字串的距離為兩個字串長度差的絕對值 

對每組資料:

對陣列a按和n的距離從小到大排序後輸出。距離相同的,值小的排在前面。

然後對陣列b,按照和s的距離從小到大輸出。距離相同的,字典序小的排在前面

樣例輸入

2 a123456

4 a12345

樣例輸出

1,3,0,4,7,8,9,10,15,20,

american,peking,123456789,jack,to,abcdefghijklmnop,

4,3,1,7,0,8,9,10,15,20,

peking,american,jack,123456789,to,abcdefghijklmnop,

仔細看題目的主程式

closer(n,distance1) 是乙個建構函式,有兩個引數,第乙個引數是int,第二個是函式指標,容易想到

struct 應該有兩個成員變數t1,t2,分別為對應這兩個引數,再加乙個()過載函式,比較的時候是和成員變數t1進行比較

#include #include #include #include using namespace std;

template struct closer

bool operator()(const t1&a, const t1 &b)

};int distance1(int n1, int n2)

int distance2(const string & s1, const string & s2)

int a[10] = ;

string b[6] = ;

int main()

return 0;

}

C 模板程式設計練習

填寫模板 printarray,使得程式輸出結果是 tomjackmaryjohn 10 不得編寫sumarray函式 include include using namespace std template t sumarray 在此處補充你的 int main cout sumarray arr...

C 模板練習 二

動態陣列類模板 include using namespace std template class t class dynamicarray 給定陣列長度構造動態陣列 dynamicarray size t capacity 從陣列中構造動態陣列 dynamicarray t arr size t...

模板練習 LUOGU

p3183食物鏈 define man 300050 int fa man opt,x,y,fx,fy,n,k,ans 0 int find int x int main if opt 1 y是x的獵物或者天敵,謊言 fa find x find y x的同類是y的同類 fa find x n fi...