#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int a,n,m, sum;
sum = 0;
cin >> n >> m;
vector <int> nums;
vector <int> selected;
vector <int> res;
for(int x =0; x< n; x++){
for (int y =0;y<m; y++){
cin >> a;
nums.push_back(a);
}
sort(nums.begin(), nums.end());
selected.push_back(nums[m-1]);
nums.clear();
}
for(int i=0; i < selected.size(); i++){
sum += selected[i];
}
for(int i=0; i < selected.size(); i++){
if (sum%selected[i] == 0) {
res.push_back(selected[i]);
}
}
cout << sum <<endl;
if (res.size() == 0){
cout << -1 ;
}
else {
for(int i=0; i < res.size(); i++){
cout << res[i] << " ";
}
}
return 0;
}
更正下代碼:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int a,n,m, sum;
sum = 0;
cin >> n >> m;
vector <int> nums;
vector <int> selected;
vector <int> res;
for(int x =0; x< n; x++){
for (int y =0;y<m; y++){
cin >> a;
nums.push_back(a);
}
sort(nums.begin(), nums.end());
selected.push_back(nums[m-1]);
nums.clear();
}
for(int i=0; i < selected.size(); i++){
sum += selected[i];
}
for(int i=0; i < selected.size(); i++){
if (sum%selected[i] == 0) {
res.push_back(selected[i]);
}
}
cout << sum <<endl;
if (res.size() == 0){
cout << -1 <<endl;
}
else {
for(int i=0; i < res.size(); i++){
if (i == res.size()){
cout << res[i] << endl;
}
else {
cout << res[i] << " ";
}
}
}
return 0;
}
if (i == res.size()){
if (i == res.size() -1){