第三题
思路
遍历字符串。 设置以下判断条件
- t2大写字母标记变量
- t1小写字母标记变量
- t3数字标记变量
- t4特殊字符标记变量
记录每一个参数在遇到逗号时输出
代码
#include<bits/stdc++.h>
using namespace std;
int main(){
string a,b="";
cin>>a;
a+=',';
bool z=true;//这一个z用于判断是否有违规字符
long long len=a.size(),len2,t1=0,t2=0,t3=0,t4=0;
for(int i=0;i<len;i++){
if(a[i]==','){
len2 = b.size();
if(len2>=6 and len2<=12 and t1+t2+t3>=2 and t4>0 and z){
cout<<b<<endl;
}
b="";
t1=0;
t2=0;
t3=0;
t4=0;
z=true;
}
else if(a[i]>='a' and a[i]<='z'){
b+=a[i];
t1=1;
}
else if(a[i]>='A' and a[i]<='Z'){
b+=a[i];
t2=1;
}
else if(a[i]>='0' and a[i]<='9'){
b+=a[i];
t3=1;
}
else if(a[i]=='!' or a[i]=='@' or a[i]=='#' or a[i]=='$'){
b+=a[i];
t4=1;
}
else{
z=false//检测到违规字符,则z变为false
}
}
if(len2>=6 and len2<=12 and t1+t2+t3>=2 and t4>0 and z){
cout<<b<<endl;
}
}第五题
思路
用二分的方法,查找两次
第一次:收集比x大的中间数
第二次:收集比x小的中间数
这样就可以最小的比x大的数 and最大的比x小的数
代码
#include<bits/stdc++.h>
using namespace std;
int a[1000005];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a+1,a+1+n);
while(m--){
int x;
cin>>x;
int l=1,r=n,ans=-1;
while(l<=r){
int mid=(l+r)/2;
if(a[mid]<=x){
l=mid+1;
}else if(a[mid]>x){
r=mid-1;
ans=a[mid];
}
}
cout<<ans<<" ";
l=1,r=n,ans=-1;
while(l<=r){
int mid=(l+r)/2;
if(a[mid]<x){
l=mid+1;
ans=a[mid];
}else if(a[mid]>=x){
r=mid-1;
}
}
cout<<ans<<"\n";
}
return 0;
}