C [GESP202306 三级]密码合规
题目描述
网站注册需要有用户名和密码,编写程序以检查用户输入密码的有效性。合规的密码应满足以下要求:
- 只能由 a∼z 之间 26 个小写字母、A∼Z 之间 26 个大写字母、0∼9 之间 10 个数字以及 !@#$ 四个特殊字符构成。
- 密码最短长度 :6 个字符,密码最大长度 :12 个字符。
- 大写字母,小写字母和数字必须至少有其中两种,以及至少有四个特殊字符中的一个。
思路
- 找出符合条件的字符串
代码
#include<bits/stdc++.h>
using namespace std;
int check(string t)
{
int f=0,f1=0,f2=0,f3=0;
for(int i=0;i<t.size();i++)
{
if(t[i]>='a'&&t[i]<='z')//判断种类
{
f=1; //1有0无
}
else if(t[i]>='A'&&t[i]<='Z')
{
f1=1;
}
else if(t[i]>='0'&&t[i]<='9')
{
f2=1;
}
else if(t[i]=='!'||t[i]=='@'||t[i]=='#'||t[i]=='$')
{
f3=1;
}
else//不符题意的字符直接淘汰
{
return 0;
}
}
int f4=f1+f2+f;//f4记录有几种字符 ,三个必有两个
if(f4>=2&&f3==1)//四个特殊字符必有其一
{
return 1;
}
else
{
return 0;
}
}
int main()
{
string s,t="";
cin >> s;
s+=",";//加一个','好判断
int len=s.size();
for(int i=0;i<len;i++)
{
if(s[i]==',')
{
if(check(t)==1&&t.size()>=6&&t.size()<=12)
{
cout << t << endl;
}
t="";//清空,不清空就只能过一个!!!
}
else
{
t+=s[i];//累积字符
}
}
return 0;
}E eqal_range
题目描述
给定一个数列 a=[a1,a2,…,a n],有 q 次询问,每次询问给出一个整数 x,你要回答:序列 a 里最小的比 x 大的数是多少?a 里最大的比 x 小的数是多少?
思路
- 一个一个回答两个问题
代码
#include<bits/stdc++.h>
using namespace std;
long long 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];
}
//二分的前提:有序排列(因为要输出数列a里最小的比x大的数和最大的比x小的数,所以可以排序)
sort(a+1,a+n+1);
while(m--)
{
int x;
cin >> x;
int l=1,r=n,ans=-1;//双指针法找位置
//问题一:序列a里最小的比x大的数是多少
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];
}
//1.如果x<=中间的数字,
//l=mid+1
//2.如果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;
}
//1.如果x<中间的数字,
//l=mid+1
//ans=a[mid];
//2.如果x>=中间的数字
//r=mid-1
}
cout << ans << "\n";
}
return 0;
}