===
#include<iostream>
using namespace std;
struct Point{
int x, y;
Point(int x=0, int y=0):x(x), y(y){};
};
Point operator + (const Point &A, const Point &B){
return Point(A.x+B.x, A.y+B.y);
}
ostream& operator << (ostream &out, const Point &p){
out << "(" << p.x << "," << p.y << ")";
return out;
}
template <typename T>
T sum(T *begin, T *end){
T ans;
for(T *i=begin; i!=end; ++i) ans = ans + *i;
return ans;
}
int main(){
Point a[] = {Point(1,2), Point(2,2), Point(3,3)};
// if `Point a[] = {(), (), ()}` it can't work.
cout << sum(a, a+2) <<endl;
return 0;
}
现有N个大理石,每个大理石上写了一个非负整数。首先把各数从小到大排序,然后回 答Q个问题。每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上 写着x。排序后的大理石从左到右编号为1~N。
#include<iostream>
#include<algorithm>
using namespace std;
const int N=10000;
int a[N];
int n,q,p,x;
int main()
{
cin>>n;
for(int i=0;i<n;++i) cin>>a[i];
sort(a, a+n);
cin>>q;
while(q--){
cin>>x;
p=lower_bound(a, a+n, x) - a;
if(a[p]==x) cout<<"Found "<<x<<" at "<<p+1<<endl;
else cout<<x<<" not found"<<endl;
}
return 0;
}