Problem A: 等しい合計点
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1153&lang=jp
解法:一個づつ交換していって判定する
#include <vector> #include <algorithm> #include <iostream> #include <cmath> using namespace std; int main() { int n,m; while (cin >> n >> m) { if(n == 0 && m == 0) break; vector<int> data,data2,sum,r_data,r_data2; for (int i=0; i < n; i++) { int num; cin >> num; data.push_back(num); } for (int i=0; i < m; i++) { int num; cin >> num; data2.push_back(num); } //ここまで入力 for (int i=0;i < n;i++) { for (int j=0;j < m;j++) { vector<int> tmp=data,tmp2=data2; swap(tmp[i],tmp2[j]); int s = 0,s2 = 0; for (int k=0;k < n;k++) s += tmp[k]; for (int k=0;k < m;k++) s2 += tmp2[k]; if(s == s2) { sum.push_back(tmp[i] + tmp2[j]); r_data.push_back(tmp2[j]); r_data2.push_back(tmp[i]); } } } int min = 100000; int iti = 0; for (int i=0;i < sum.size();i++) { if(min >= sum[i]) { min = sum[i]; iti = i; } } if(sum.size()> 0) cout << r_data[iti] << " " << r_data2[iti] << endl; else cout << -1 << endl; } }