1、数组的查重(检查各项后是否重复出现)
A - Do Not Be Distracted!
https://vjudge.net/contest/445659#problem/A
题解:利用标记数组vis【maxn】进行解题
#include <bits/stdc++.h>
using namespace std;
char s[55];
int vis[27]; //标记数组,检查是否前面出现过该字母
int main(){
int t, n;
cin >> t;
while(t--){
cin >> n;
for(int i=0; i<n; i++){
cin >> s[i];
}
memset(vis,0,sizeof(vis)); //多项测试,要初始化
int flag=0;
int ans=0;
for(int i=0; i<n; i++){
if(vis[s[i]-65]){
flag=1;
break;
}
vis[s[i]-65]=1;
for(int j=i; j<n; j++){
if(s[j]==s[i]){
if(j==n-1){ //处理当最后一串全是同一字母的情况
ans=1;
break;
}
else continue;
}
else{
i=j-1;//原循环有i++,故要退1
break;
}
}
if(ans) break;
}
if(flag) puts("NO");
else puts("YES");
}
return 0;
}
2、数组的字典序问题
首先,要知道一个问题,
123的字典序小于321,因此要使eg:231这种排序的数组的字典序变小,原理是前面变小。
B - Tit for Tat
https://vjudge.net/contest/445659#problem/B
题解:根据题目要求,最好前面非零数–,最后一个数++,这样就可得到最小字典序的序列。同时注意h<n-1
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 10;
int a[maxn];
int main(){
int t;
cin >> t;
int n, k;
while(t--){
cin >> n >> k;
for(int i=0; i<n; i++){
cin >> a[i];
}
int h=0;
while(k && h<n-1){ //注意不要用for循环,因为不一定要执行K次操作
if(a[h]){//大于0
a[h]--;
a[n-1]++;
k--;
}
else{
h++;
}
}
cout << a[0];
for(int i=1; i<n; i++){
cout << " " << a[i];
}
cout << endl;
}
return 0;
}
3、完全平方数
C - Ordinary Numbers
https://vjudge.net/contest/445659#problem/C
题解:平方数可以分成
11=1x10+1
99=9x10+9…
#include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin >> t;
int n;
while(t--){
scanf("%d",&n);
int sum=0;
for(int i=1; i<=9; i++){ //从1~9处理问题,另一角度解决
long long ans=i;
while(n>=ans){
sum++; //写在前面,处理<10的情况
ans=ans*10+i;
}
}
cout << sum << endl;
}
return 0;
}
4、奇偶矩阵
D - Not Adjacent Matrix
https://vjudge.net/contest/445659#problem/D
题解:题意要上下左右都不能相差1,那么先列出奇矩阵,再列出偶矩阵。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e2 + 10;
int a[maxn][maxn];
int b[maxn*maxn];
int main(){
int t;
cin >> t;
int n;
while(t--){
cin >> n;
if(n==1){
puts("1");
continue;
}
if(n==2){
puts("-1");
continue;
}
else{
for(int i=1; i<=n*n; i++){
b[i]=i;
}
int x=1;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(x<=n*n){
a[i][j]=b[x];
x+=2;
}
else{
x=2;
a[i][j]=b[x];
x+=2;
}
}
}
}
for(int i=0; i<n; i++){
cout << a[i][0];
for(int j=1;j<n; j++){
cout << " " << a[i][j];
}
cout << endl;
}
cout << endl;
}
}
可以不用这么麻烦,直接输出也行。
5、完美对
E - Array Reodering
https://vjudge.net/contest/445659#problem/E
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 10;
int a[maxn];
int main(){
int t;
cin >> t;
int n;
while(t--){
scanf("%d",&n);
for(int i=0; i<n; i++){
cin >> a[i];
}
int x=n-1;
int sum=0;
for(int i=0; i<n; i++){
if(a[i]%2==0) sum+=(x--);
else{
for(int j=i+1; j<n; j++){
if(a[j]%2==1 && __gcd(a[i],a[j])>1) sum++;
}
}
}
cout << sum << endl;
}
return 0;
}
将所有偶数放前,同时对奇数处理,主要是找规律。
6、完全平方数
F - Perfectly Imperfect Array
https://vjudge.net/contest/445659#problem/F
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 10;
int a[maxn];
int main(){
int t;
cin >> t;
int n;
while(t--){
scanf("%d",&n);
for(int i=0; i<n; i++){
cin >> a[i];
}
int flag=0;
for(int i=0; i<n; i++){
int w = (int)sqrt(a[i]);
if(w*w!=a[i]){
flag=1;
break;
}
}
if(flag) puts("YES");
else puts("NO");
}
return 0;
}
因为相乘,所以其中一个不是完全平方数,那么整个数列就不存在积为完全平方数。