T1-瑞神的序列
题目
题意解析
首先明白这道题是求序列的段数,且分段的标准是连续区间一个段里元素值相同。
所以我们不妨遍历这个数组中的元素,相邻相同则继续循环,不同则更新答案,让sum++,最终得到我们要的答案
test data
Input
12
2 3 3 6 6 6 1 1 4 5 1 4
Output
8
Codes
#include<iostream>
using namespace std;
const int maxn=1e3+10;
int n,a[maxn];
int main(){
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
int ans=1;
for(int i=0;i<n;i++){
if(i<n-1&&a[i]!=a[i+1])
ans++;
else continue;
}
cout<<ans<<endl;
return 0;
}
T2-消消乐大师Q老师
题目
题意解析
本题类似于消消乐,每连成3个及3个以上横向纵向便可以消除,那么我解本题的思路就是找中心点。
何为中心点,就是中心点的上下或者左右,必然连成3个,比如****四个心号,2号位和3号位就是它的中心点。
所以只需要按行遍历和按咧遍历就可以找出所有的中心点,若按行,则把中心点的左右置0,若按列,则把中心点上下置0
test data
Input 1
4 5
2 2 3 1 2
3 4 5 1 4
2 3 2 1 3
2 2 2 4 4
Output 1
2 2 3 0 2
3 4 5 0 4
2 3 2 0 3
0 0 0 4 4
Input 2
4 5
2 2 3 1 2
3 1 1 1 1
2 3 2 1 3
2 2 3 3 3
Output 2
2 2 3 0 2
3 0 0 0 0
2 3 2 0 3
2 2 0 0 0
Input 3
1 1
5
Output 3
5
Inuput 4
2 3
1 2 6
3 3 3
Output 4
1 2 6
0 0 0
Input 5
6 7
1 2 3 4 5 6 7
1 3 2 5 6 4 7
1 1 1 5 2 3 7
1 1 1 3 4 5 7
1 3 1 2 5 6 6
1 1 1 2 5 6 6
Output 5
0 2 3 4 5 6 0
0 3 2 5 6 4 0
0 0 0 5 2 3 0
0 0 0 3 4 5 0
0 3 0 2 5 6 6
0 0 0 2 5 6 6
Codes
#include<iostream>
using namespace std;
int n,m,a[40][40],f[40][40];
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
f[i][j]=1;
}
}
for(int i=1;i<=n;i++){
for(int j=2;j<m;j++){
if(a[i][j]==a[i][j-1]&&a[i][j]==a[i][j+1]){
f[i][j]=0;f[i][j-1]=0;f[i][j+1]=0;
}
}
}
for(int i=2;i<n;i++){
for(int j=1;j<=m;j++){
if(a[i][j]==a[i-1][j]&&a[i][j]==a[i+1][j]){
f[i][j]=0;f[i-1][j]=0;f[i+1][j]=0;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(f[i][j]==1)
cout<<a[i][j];
else if(f[i][j]==0)
cout<<"0";
if(j<m) cout<<" ";
}
if(i<n)
cout<<endl;
}
return 0;
}