1. 程式人生 > >PAT 甲級 A1063 (2019/02/22)

PAT 甲級 A1063 (2019/02/22)

遍歷集合 不能 集合 find com sca end a10 sam

 1 #include<cstdio>
 2 #include<set>
 3 using namespace std;
 4 const int MAXN = 51;
 5 set<int> ST[MAXN];    //MAXN個集合 
 6 void compare(int x, int y){        //比較集合 ST[x] 和 ST[y] 
 7     int DiffNumber = ST[y].size(), SameNumber = 0;    //不同數的個數,相同數的個數
 8     //遍歷集合 ST[x]
 9     for
(set<int>::iterator it = ST[x].begin(); it != ST[x].end(); it++){ 10 if(ST[y].find(*it) != ST[y].end()) //在 ST[y] 中能找到該元素 11 SameNumber++; 12 else //在 ST[y] 中不能找到該元素 13 DiffNumber++; 14 } 15 printf("%.1f%%\n", SameNumber * 100.0 / DiffNumber);
16 } 17 int main(){ 18 int N, K, IntegerNumber, Number, ST1, ST2; 19 scanf("%d", &N); //集合個數 20 //因為集合次序為1,2,3,4,...N,所以不能從0開始 21 for(int i = 1; i <= N; i++){ 22 scanf("%d", &IntegerNumber); //集合 i 中的元素個數, 23 for(int j = 0; j < IntegerNumber; j++){ //
此循環執行 IntegerNumber 次即可 24 scanf("%d", &Number); //集合 i 中的元素 Number 25 ST[i].insert(Number); //將元素 Number 加入集合 ST[i] 中 26 } 27 } 28 scanf("%d", &K); // K 個查詢 29 for(int i = 0; i < K; i++){ 30 scanf("%d%d", &ST1, &ST2); //需要對比的兩個集合編號 31 compare(ST1, ST2); //比較兩個集合 32 } 33 return 0; 34 }

PAT 甲級 A1063 (2019/02/22)