把#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int n,k,a[100005],b[100005],shu,s1,s2,gs,to; int main() { scanf("%d%d",&n,&k);//n个人获得科技创新奖,m个人获得特殊贡献奖 for(int i=1;i<=n;i++)scanf("%d",&a[i]);//科技创新奖的人的编号 for(int i=1;i<=k;i++)scanf("%d",&b[i]);//特殊贡献奖的人的编号 sort(b+1,b+1+k);//用特殊贡献奖的人的编号排序 for(int i=1;i<=n;i++)//用科技创新奖的人的编号去寻找特殊贡献奖的人的编号有无相同 { int low=1,high=k; while(low<=high)//二分,看看有无相同 { int mid=(low+high)/2;//中间值 if(b[mid]==a[i])//有无相同? { cout<<a[i]<<" "; break; else if(b[mid]<a[i])low=mid+1;//在右区间找 else high=mid-1;//在左区间找 } } return 0; }改写用python
时间: 2024-04-19 21:25:21 浏览: 17
你想将这段C++代码改写为Python代码,是这样吗?以下是将其转换为Python的代码:
```python
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.sort()
for i in range(n):
low, high = 0, k - 1
while low <= high:
mid = (low + high) // 2
if b[mid] == a[i]:
print(a[i], end=" ")
break
elif b[mid] < a[i]:
low = mid + 1
else:
high = mid - 1
```
这段代码的作用是从科技创新奖的人员中找出获得特殊贡献奖的人员。首先,对特殊贡献奖的人员编号进行排序。然后,对于每个科技创新奖的人员编号,使用二分查找在特殊贡献奖的人员编号中寻找是否有相同的。如果找到相同的编号,则输出该编号。希望对你有帮助!
相关问题
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cstring> #include <stack> #include <ctime> 怎么配置这些文件
这些文件是C++的头文件,不需要配置。在编写C++程序时,只需要在代码中包含这些头文件即可使用它们提供的函数和类。这些头文件一般都已经被安装在你的C++编译器中了,你只需要在代码中添加`#include`指令即可。比如,如果你想使用`iostream`头文件中的`cout`和`cin`,你可以这样写代码:
```
#include <iostream>
using namespace std;
int main(){
cout<<"Hello World"<<endl;
return 0;
}
```
如果你使用的是Visual Studio等IDE,一般会自动帮你添加这些头文件,你只需要专注于代码的编写即可。
#include <iostream> #include <algorithm> #include <string> #include <cstdio> #include <cmath> #include <set> #include <map> using namespace std; //const int N = 1e5 + 10; //typedef long long ll; int main() { int t; cin >> t; while (t--) { int n; set<string>a; string s; cin >> n >> s; int t = 0; for (int i = 0; i < s.size(); i++) { t = s.find(s[t]); a.insert(s.substr(i, 1)); } for (int i = 0; i < a.size(); i++) cout << a[i]; cout << endl; } return 0; }有什么问题
这段代码存在一些问题:
1. 在第二个 for 循环中,使用了下标访问 set 容器,这是错误的,因为 set 容器不支持下标访问。
2. 在第二个 for 循环中,使用了 a.size(),这也是错误的,因为 set 容器没有 size() 方法,应该使用 a.size()。
3. 在第一个 for 循环中,应该使用 s.substr(i, 1) 而不是 s.substr(0, t+1),因为后者会将 s 的前 t+1 个字符作为子串,而不是从 i 开始的一个字符。
4. 在第一个 for 循环中,应该将 a.insert() 放在 t = s.find(s[t]) 的后面,因为插入的是 s.substr(i, 1),插入顺序应该是从前往后。
修正后的代码如下:
```c++
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
set<string> a;
string s;
cin >> n >> s;
int t = 0;
for (int i = 0; i < s.size(); i++) {
a.insert(s.substr(i, 1));
t = s.find(s[t]);
}
for (auto it = a.begin(); it != a.end(); it++) cout << *it;
cout << endl;
}
return 0;
}
```