#include <iostream> #include <string> #include <vector> #include <algorithm> #include<list> #include<queue> using namespace std; int main() { int n,m; int a[1000]; int i; while(cin>>n>>m) { for(i=0;i<n;i++) { cin>>a[i]; } std::sort(a,a+n); for(int j=0;j<m;j++) { int b; cin>>b; int*it=std::lower_bound(a,a+n,b); if (it != a + n && *it == b) { std::cout<<b<<" found at "<<it-a+1<<endl; } else { std::cout<<b<<" not found"<<endl; } }} return 0; }怎么修改可以让运行时不需要另外输回车键继续
时间: 2023-12-20 19:03:20 浏览: 27
你可以在输入完 `n` 和 `m` 后,使用 `cin.ignore()` 来清除输入缓冲区中的换行符,使程序可以直接继续读取下一个输入。修改后的代码如下所示:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <queue>
using namespace std;
int main() {
int n, m;
int a[1000];
int i;
while (cin >> n >> m) {
for (i = 0; i < n; i++) {
cin >> a[i];
}
std::sort(a, a + n);
for (int j = 0; j < m; j++) {
int b;
cin >> b;
int* it = std::lower_bound(a, a + n, b);
if (it != a + n && *it == b) {
std::cout << b << " found at " << it - a + 1 << endl;
} else {
std::cout << b << " not found" << endl;
}
}
cin.ignore(); // 清除输入缓冲区中的换行符
}
return 0;
}
```
这样,程序就可以连续读取多组输入了,而不需要再输入回车键继续。