文章

166

粉丝

68

获赞

825

访问

49.6k

头像
乘法 题解:注意重复,map标记即可
P1909 华东师范大学2022年机试
发布于2025年3月9日 11:04
阅读数 218

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

int main() {
	int n,m,k;
	while(cin>>n>>m>>k){
	    int n1=n,m1=m;
	    vector<int>a,b,c;
	    while(n--){
	        int x;cin>>x;
	        a.push_back(x);
	    }
	    while(m--){
	        int x;cin>>x;
	        b.push_back(x);
	    }
	    map<int,int>mp;
	    for(int i=0;i<n1;i++){
	        for(int j=0;j<m1;j++){
	            if(mp[a[i]*b[j]]==1)continue;
	            mp[a[i]*b[j]]=1;
	            c.push_back(a[i]*b[j]);
	        }
	    }
	    sort(c.begin(),c.end());
	    cout<<c[c.size()-k]<<endl;
	}
}

 

登录查看完整内容


登录后发布评论

2 条评论
P3anut
2025年3月18日 14:23

20 30 121
-1 0 2 1 -3 -1 -7 -6 -3 10 -1 9 1 -10 -8 -10 -4 0 3 -10
8 9 10 -7 3 8 1 -8 -2 -6 -6 1 4 5 -8 8 -2 -10 6 5 -6 2 -2 1 -8 7 8 -4 -6 3

这个数据过不了,答案应该20,但是代码输出的是1

赞(0)

RingoCrystal : 回复 P3anut: 并未出现未通过的情况,请仔细检查你的代码,是不是哪里改写出现了错误 这里附上我的测试代码,和上述一致 #include <bits/stdc++.h> using namespace std; int main() { int n,m,k; while(cin>>n>>m>>k){ int n1=n,m1=m; vector<int>a,b,c; while(n--){ int x;cin>>x; a.push_back(x); } while(m--){ int x;cin>>x; b.push_back(x); } map<int,int>mp; for(int i=0;i<n1;i++){ for(int j=0;j<m1;j++){ if(mp[a[i]*b[j]]==1)continue; mp[a[i]*b[j]]=1; c.push_back(a[i]*b[j]); } } sort(c.begin(),c.end()); cout<<c[c.size()-k]<<endl; } }

2025年3月18日 15:34