20157:补足程序7
描述
补足下面的程序,使得其输出结果是:
18,20
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <vector>
using namespace std;
int main()
{
inta[10] = {1, 3, 2, 10,7, 18,20,31,47,59};
sort(a,a+10);
pair<int*,int*>p;
p=
// 在此处补充你的代码
(a,a+10,18);
cout<< * (p.first) << "," << * (p.second) <<endl;
return0;
}
输入
无
输出
18,20
样例输入
无
样例输出
18,20
提示
只填写一个函数名。
解题思路:由题可知,输出有两个数,分别为lower_bound和upper_bound的值,而在这需要只填写一个函数就能同时得到应用这两个函数得到的结果,则只能用equal_range。
即:
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <vector>
using namespace std;
int main()
{
inta[10] = { 1, 3, 2, 10, 7, 18, 20, 31, 47, 59 };
sort(a,a + 10);//从小到大排序
pair<int*,int*> p;
p=
//在此处补充你的代码
equal_range(a,a + 10, 18);
cout<< *(p.first) << "," << *(p.second) << endl;
return0;
}