注意題目的數字最大是7 而能整除的只有 1,2,3,4,6,故構成的組合只能是1,2,4 或1,2,6或1,3,6,故分別統計1,2,3,4,6的個數,然後再分配
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <string> 5 6 using namespace std; 7 8 int main(){ 9 int n; 10 cin >> n ; 11 vector<int> a1,a2,a3,a4,a6; 12 int tmp; 13 for(int i = 0 ; i < n; i ++ ){ 14 cin >>tmp; 15 if(tmp != 1 && tmp !=2 && tmp != 3 && tmp != 4 && tmp!=6) { cout<<-1<<endl; return 0;} 16 switch(tmp){ 17 case 1: 18 a1.push_back(tmp); 19 break; 20 case 2: 21 a2.push_back(tmp); 22 break; 23 case 3: 24 a3.push_back(tmp); 25 break; 26 case 4: 27 a4.push_back(tmp); 28 break; 29 case 6: 30 a6.push_back(tmp); 31 break; 32 } 33 } 34 int len1 = a1.size(),len2 = a2.size(),len3 = a3.size(),len4 = a4.size(),len6 = a6.size(); 35 if( (len1=len1-len4)>=0 && (len2=len2 - len4 )>= 0 && 36 (len1= len1-len3)>=0 && (len6 = len6 - len3) >=0 && 37 (len1== len2 && len2 == len6)){ 38 for(int i = 0; i < a4.size(); ++ i) 39 cout<<1<<" "<<2<<" "<<4<<endl; 40 for(int i = 0; i < a3.size(); ++ i) 41 cout<<1<<" "<<3<<" "<<6<<endl; 42 for(int i = 0; i < a6.size()-a3.size(); ++ i) 43 cout<<1<<" "<<2<<" "<<6<<endl; 44 } 45 else{ 46 cout<<-1<<endl; 47 } 48 49 return 0; 50 }