文章

61

粉丝

98

获赞

17

访问

15.1k

头像
集合中的相同元素 题解:题目没有要求循环输入,我没用循环输入AC为0,用了循环输入AC100,奇怪
P5105
发布于2024年3月27日 10:25
阅读数 109

#include<bits/stdc++.h>
using namespace std;

int main(){
    int n;
	while(cin >> n){
		int sum = 0;
		int myset1[n];
		int myset2[n];

		for(int i = 0; i < n; i++)
			cin >> myset1[i];
		 for(int i = 0; i < n; i++)
			cin >> myset2[i];
		sort(myset1, myset1+n);

		bool res[n] = {0};
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				if(myset1[i] == myset2[j]){
					res[i] = true;
					sum++;
					break;
				}
			}
		}

		cout << sum << endl;
		for(int i = 0; i < n; i++)
			if(res[i])
				cout <<myset1[i] << " ";
        cout << endl;
	}
    return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发