A题
题意:遍历一个字符串,要求前方出现的字符后方不能再出现。符合要求输出YES否则输出NO
思路:暴力遍历即可,可以用去重操作降低时间复杂度,但是没必要了,因为字符串一共就50的长度
#include <bits/stdc++.h>
#define FastIO ios::sync_with_stdio(0);cout.tie(0);cin.tie(0);
using namespace std;
bool cmp(int x,int y)
{return x>y;}
int main()
{
int t;
FastIO
for(cin>>t;t;t--)
{
int n;
cin>>n;
string s;
int flag=1;
cin>>s;
for(int i=1;i<n;i++)
{
if(s[i]!=s[i-1])
{
for(int j=i;j<n;j++)
{
if(s[j]==s[i-1])
{
cout<<"NO"<<endl;
flag=0;
goto o;
}
}
}
}
o:;
if(flag)
{
cout<<"YES"<<endl;
}
}
return 0;
}
B题
题意:给定n,输出从1到n所有满足各位上的数字都相等的数的个数。
思路:手动模拟可发现有公式ans=(位数-1)*9+n/(对应位数的1。(比如10对应10/11,100对应100/111,200对应20/111))
#include <bits/stdc++.h>
#define FastIO ios::sync_with_stdio(0);cout.tie(0);cin.tie(0);
using namespace std;
bool cmp(int x,int y)
{return x>y;}
int main()
{
int t;
FastIO
for(cin>>t;t;t--)
{
int n;
cin>>n;
int n1=n,w=-1;
while(n1)
{
w++;
n1/=10;
}
int cs=0;
for(int i=0;i<=w;i++)
{
cs+=pow(10,i);
}
if(cs%2==0)
{
cs++;
}
int ans=w*9+n/cs;
cout<<ans<<endl;
}
return 0;
}
C题
题意:给定一个n,要求输出一个n*n的矩阵,矩阵相邻的两个元素之差的绝对值大于1;
思路:先输出奇数,后输出偶数。
#include <bits/stdc++.h>
#define FastIO ios::sync_with_stdio(0);cout.tie(0);cin.tie(0);
using namespace std;
bool cmp(int x,int y)
{return x>y;}
int main()
{
int t;
FastIO
for(cin>>t;t;t--)
{
int n,ans=1;
cin>>n;
if(n==2)
{
cout<<"-1"<<endl;
continue;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(ans>n*n)
{
ans=2;
}
cout<<ans<<" ";
ans+=2;
}
cout<<endl;
}
}
return 0;
}
D题
题意:给定一个数组,输出数组中a[j]-j=a[i]-i且满足(j<i)的一对数的个数
思路:因为数组给到了2e5所以暴力双重一定超时,这里用到map来模拟一下就可以了,是一道简单的模拟题
另外:如果没有i>j这个条件,可以直接用map存起来之后累加。
#include <bits/stdc++.h>
#define FastIO ios::sync_with_stdio(0);cout.tie(0);cin.tie(0);
using namespace std;
bool cmp(int x,int y)
{return x>y;}
int main()
{
int t;
FastIO
for(cin>>t;t;t--)
{
map<int,int>maps;
int n;
long long ans=0;
cin>>n;
for(int i=0;i<n;i++)
{
int temp;
cin>>temp;
ans+=maps[temp-i]++;
}
cout<<ans<<endl;
}
return 0;
}
后记:能力目前有限,还需多练多刷。