解题思路:
注意事项:
C++是世界上最好的语言
参考代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#define N 1000
using namespace std;
vector<int> f, g, minDist;
int dist(const int m, const int n)
{
int distArr = 0;
int minF = f[0], maxF = f[f.size() - 1], minG = g[0], maxG = g[g.size() - 1];
if (maxF <= minG)
distArr = minG - maxF;
else if (minF >= maxG)
distArr = minF - maxG;
else
{
for (int i = 0; i < m; i++)
{
vector<int> eachMinDist;
for (int j = 0; j < n; j++)
eachMinDist.push_back(abs(f[i] - g[j]));
minDist.push_back(*min_element(eachMinDist.begin(), eachMinDist.end()));
}
distArr = *min_element(minDist.begin(), minDist.end());
}
return distArr;
}
int main(int argc, char** argv)
{
int m, n, temp;
cin >> m >> n;
for (int i = 0; i < m; i++)
{
cin >> temp;
f.push_back(temp);
}
for (int j = 0; j < n; j++)
{
cin >> temp;
g.push_back(temp);
}
cout << dist(m, n) << endl;
return 0;
}
0.0分
0 人评分
【偶数求和】 (C语言代码)浏览:588 |
WU-整除问题 (C++代码)浏览:648 |
C语言程序设计教程(第三版)课后习题8.7 (C语言代码)浏览:609 |
C语言程序设计教程(第三版)课后习题10.4 (C语言代码)浏览:583 |
a+b浏览:452 |
母牛的故事 (C语言代码)浏览:1045 |
C语言训练-自守数问题 (C语言代码)浏览:798 |
上车人数 (C语言代码)浏览:753 |
C语言程序设计教程(第三版)课后习题10.1 (C++代码)浏览:529 |
多组数据新方法浏览:368 |