input不要輸入重複的數字,我使用sstream做未知長度的輸入存於vector,再依據A、B大小有重疊的就pop掉,output就可以分為五種
1.A,B都是空 == equal
2.A為空 == A是子集
3.B為空 == B是子集
4.有pop過 == confused
5.完全沒pop過 == disjoint
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
string str1,str2;
while(getline(cin,str1)){
getline(cin,str2);
stringstream ss;
ss<<str1;
int temp;
vector<int> A,B;
while(ss>>temp){
auto it = find(A.begin(), A.end(),temp);
if (it == A.end()){
A.push_back(temp);
}
}
ss.clear();
ss<<str2;
while(ss>>temp){
auto it = find(B.begin(), B.end(),temp);
if (it == B.end()){
B.push_back(temp);
}
}
bool set = false;
if(A.size() >= B.size()){
for(int i = 0 ; i < B.size() ; i++){
auto it = find(A.begin(), A.end(),B[i]);
if (it != A.end()){
A.erase(it);
auto it2 = find(B.begin(), B.end(),B[i]);
B.erase(it2);
i--;
set = true;
}
}
}else{
for(int i = 0 ; i < A.size() ; i++){
auto it = find(B.begin(), B.end(),A[i]);
if (it != B.end()){
B.erase(it);
auto it2 = find(A.begin(), A.end(),A[i]);
A.erase(it2);
i--;
set = true;
}
}
}
if(A.size() == 0 && B.size() == 0){
cout<<"A equals B"<<endl;
}else if(A.size() == 0){
cout<<"A is a proper subset of B"<<endl;
}else if(B.size() == 0){
cout<<"B is a proper subset of A"<<endl;
}else if(set){
cout<<"I'm confused!"<<endl;
}else{
cout<<"A and B are disjoint"<<endl;
}
}
return 0;
}
input不要輸入重複的數字,我使用sstream做未知長度的輸入存於vector,再依據A、B大小有重疊的就pop掉,output就可以分為五種
1.A,B都是空 == equal
2.A為空 == A是子集
3.B為空 == B是子集
4.有pop過 == confused
5.完全沒pop過 == disjoint
#include
#include
#include
#include
#include
using namespace std;int main()
{
string str1,str2;
while(getline(cin,str1)){
getline(cin,str2);
stringstream ss;
ss< int temp;
vector A,B;
while(ss>>temp){
auto it = find(A.begin(), A.end(),temp);
if (it == A.end()){
A.push_back(temp);
}
}
ss.clear();
ss< while(ss>>temp){
auto it = find(B.begin(), B.end(),temp);
if (it == B.end()){
B.push_back(temp);
}
}
bool set = false;
if(A.size() >= B.size()){
for(int i = 0 ; i < B.size() ; i++){
auto it = find(A.begin(), A.end(),B[i]);
if (it != A.end()){
A.erase(it);
auto it2 = find(B.begin(), B.end(),B[i]);
B.erase(it2);
i--;
set = true;
}
}
}else{
for(int i = 0 ; i < A.size() ; i++){
auto it = find(B.begin(), B.end(),A[i]);
if (it != B.end()){
B.erase(it);
auto it2 = find(A.begin(), A.end(),A[i]);
A.erase(it2);
i--;
set = true;
}
}
}
if(A.size() == 0 && B.size() == 0){
cout<<"A equals B"< }else if(A.size() == 0){
cout<<"A is a proper subset of B"< }else if(B.size() == 0){
cout<<"B is a proper subset of A"< }else if(set){
cout<<"I'm confused!"< }else{
cout<<"A and B are disjoint"< }
}
return 0;
}
對了本寫法在UVA上也通過