csp201409-01 c++
#include<iostream>
using namespace std;
const int N=1000;
int s[N+1];
int main(){
int n,x,count=0;
cin>>n;
for(int i=0;i<n;i++){
cin>>x;
s[i]=x;
}
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
if(s[j]==s[k]-1||s[j]==s[k]+1)
count++;
}
}
cout<<count/2<<endl;
return 0;
}
csp201409-02 c++
注:直接用二维数组表示坐标系,对应出现值即为1,重复赋值仍为1.参考自优秀博文
#include<iostream>
#include<cstdio>
using namespace std;
const int N=100;
int a[N+1][N+1];
int main()
{
int n,i,j,x1,x2,y1,y2;
cin>>n;
while(n--){
cin>>x1;
cin>>y1;
cin>>x2;
cin>>y2;
for(i=x1+1;i<=x2;i++)
for(j=y1+1;j<=y2;j++)
a[i][j]=1;
}
int aver=0;
for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
if(a[i][j])
aver++;
cout<<aver<<endl;
return 0;
}
csp201409-03 c++字符串匹配
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
const int N=100;
int main()
{
char key[N+1],s[N+1],lowerkey[N+1],lowers[N+1];
int option,n;
cin>>key>>option>>n;
strcpy(lowerkey,key);
strlwr(lowerkey);
for(int i=1;i<=n;i++){
cin>>s;
if(option==0){ //大小写不敏感,把匹配和带匹配的字符都转换成小写
strcpy(lowers,s);
strlwr(lowers);
if(strstr(lowers,lowerkey))//查找函数 strstr
cout<<s<<endl;
}
else{
if(strstr(s,key))
cout<<s<<endl;
}
}
return 0;
}