2017年12月11日 星期一

[C_ST83-易] 聯集(C++)

[C_ST83-易] 聯集

問題描述 :
聯集:由幾個集合的所有元素所形成的集合,稱為這幾個集合的聯集, A 、 B 兩集合的聯集以 A ∪ B 表之。
範例 :
A 、 B 分別為兩個集合,集合裏有不同的元素,如下列所示,求出其聯集之集合內容 :
A = 1,3,5,7,9
B = 1,2,3,4,6,8,10
聯集: 1 2 3 4 5 6 78 9 10
求輸入任 2 個集分別為合 A 集合及 B 集合,求其聯集 ?
輸入說明 :
輸入兩列的數字代表 A 及 B 兩集合,第一列為 A 集合,第一列為 B 集合,每一列集合中的元素以逗號分隔開來。
例如:
輸入 2 列字串 ( 第一列為 A 集合,第一列為 B 集合 ) :
1,3,5,7,9
1,2,3,4,6,8,10
輸出說明 :
輸出 A 及 B 兩集合之 聯 集 , 輸出元素以空白分隔開來, 須按照小到大排列 . 若空集合則輸出 null
例如:
按照小到大排列 輸出為:
1 2 3 4 5 6 7 8 9 10
範例 :

輸入範例輸出範例
1,3,5,7,9
2,4,6,8,10
1 2 3 4 5 6 7 8 9 10
7,3,5,8,15
11, 9,7,6,8,10
3 5 6 7 8 9 10 11 15
33,15,37,91
11,31,6,9,12,88
6 9 11 12 15 31 33 37 91 88



  1. #include <iostream>    
  2. #include <string>    
  3. #include <sstream>    
  4. #include <algorithm>    
  5.      
  6. using namespace std;    
  7.      
  8. int main() {    
  9.     // [C_ST83-易] 聯集    
  10.     // g++(c++11)  
  11.     string A, B;    
  12.     cin >> A >> B;    
  13.     int i_A[1000000];    
  14.     int i_B[1000000];    
  15.     //切割字串A    
  16.     stringstream ssa(A);    
  17.     string token;    
  18.     int countA = 0;    
  19.     while (getline(ssa, token, ','))    
  20.     {    
  21.         i_A[countA] = stoi(token);    
  22.         countA++;    
  23.     }    
  24.     //切割字串B    
  25.     stringstream ssb(B);    
  26.     int countB = 0;    
  27.     while (getline(ssb, token, ','))    
  28.     {    
  29.         i_B[countB] = stoi(token);    
  30.         countB++;    
  31.     }    
  32.       
  33.     if(countA + countB == 0)    
  34.     {    
  35.         cout << "null" << endl;    
  36.     }    
  37.     else    
  38.     {    
  39.         int fin[countA+countB];    
  40.         fill(fin, fin+countA+countB,0);    
  41.         bool exist = true;    
  42.         int fpos = 0;    
  43.         //A跟fin比對    
  44.         for (int i = 0; i < countA; i++)    
  45.         {    
  46.             for(int j = 0;j < countA+countB;j++)    
  47.             {    
  48.                 if(i_A[i] == fin[j])//如果存在在fin就false    
  49.                 {    
  50.                     exist = false;    
  51.                 }    
  52.             }    
  53.             if(exist == true)//不存在在fin就放入fin    
  54.             {    
  55.                 fin[fpos] = i_A[i];    
  56.                 fpos++;    
  57.             }    
  58.             exist = true;    
  59.         }    
  60.         //B跟fin比對    
  61.         for (int i = 0; i < countB; i++)    
  62.         {    
  63.             for(int j = 0;j < countA+countB;j++)    
  64.             {    
  65.                 if(i_B[i] == fin[j])//如果存在在fin就false    
  66.                 {    
  67.                     exist = false;    
  68.                 }    
  69.             }    
  70.             if(exist == true)//不存在在fin就放入fin    
  71.             {    
  72.                 fin[fpos] = i_B[i];    
  73.                 fpos++;    
  74.             }    
  75.             exist = true;    
  76.         }    
  77.   
  78.         sort(fin, fin+fpos);    
  79.         for (int i = 0; i < fpos; i++)    
  80.         {    
  81.             if(i != fpos-1)    
  82.             {    
  83.                 cout << fin[i] << " ";    
  84.             }    
  85.             else    
  86.             {    
  87.                 cout << fin[i] << endl;    
  88.             }    
  89.         }    
  90.     }    
  91.     return 0;    
  92. }  

沒有留言:

張貼留言