TestDataTest \quad DataTestData
输入 | 值 |
---|---|
数组大小 | 3 |
A组元素 | 5 15 18 |
B组元素 | 3 14 21 |
此代码是从文件读取数据
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int searchMid(int A[], int B[], int n){
int start1 = 0, end1 = n - 1;
int start2 = 0, end2 = n - 1;
int mid1, mid2;
while (start1 < end1 && start2 < end2) {
mid1 = (start1 + end1) / 2;
mid2 = (start2 + end2) / 2;
if (A[mid1] == B[mid2]) {
return A[mid1];
}
if (A[mid1] < B[mid2]) {
if ((start1 + end1)%2 == 0) {
start1 = mid1;
}
else{
start1 = mid1 + 1;
}
end2 = mid2;
}
else{
if ((start2 + end2) % 2 == 0) {
start2 = mid2;
}
else{
start2 = mid2 + 1;
}
end1 = mid1;
}
}
if (A[start1] < B[start2]) {
return A[start2];
}
else{
return B[start1];
}
}
int main(int argc, const char * argv[]) {
ifstream in("/median_num/input.txt");
string s;
istringstream first;
int n;
if (in) {
getline(in, s);
first.str(s);
first>>n;
}
int A[n];
int B[n];
string line;
istringstream instr;
for (int i = 0; i < 2; ++i) {
getline(in, line);
instr.str(line);
for (int j = 0; j < n; ++j) {
if (i == 0){
instr>>A[j];
}
else{
instr>>B[j];
}
}
}
int re;
re = searchMid(A, B, n);
cout<<re<<endl;
return 0;
}