项目:
c++语法学习
学习成功展示
文件的读写
ios::in 只读
ios::out 只写
ios::app 从文件末尾开始写,防止丢失文件中原来就有的内容
ios::binary 二进制模式
ios::nocreate 打开一个文件时,如果文件不存在,不创建文件
ios::noreplace 打开一个文件时,如果文件不存在,创建该文件
ios::trunc 打开一个文件,然后清空内容
ios::ate 打开一个文件时,将位置移动到文件尾
ios::beg 文件头
ios::end 文件尾
ios::cur 当前位置
二分
原题链接
#include<iostream>
#include<cstdio>
using namespace std;
int n,m;
int q[100010];
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>q[i];
while(m--)
{
int x;
cin>>x;
int l=0,r=n-1;
while(l<r)
{
int mid=(l+r)/2;
if(q[mid]>=x)
r=mid;
else
l=mid+1;
}
if(q[l]!=x)
cout<<"-1"<<" "<<"-1"<<endl;
else
{
cout<<l<<' ';
int l=0;r=n-1;
while(l<r)
{
int mid=(l+r)/2+1;
if(q[mid]<=x)
l=mid;
else
r=mid-1;
}
cout<<l<<endl;
}
}
}