【前言】题库:OJ:CCF-CSP 模拟考试
1 201912-1 报数
题目描述
样例
笔记
- 简单计数问题,4 人喊数, 7 的倍数和含 7 的数字跳过,分别记录四人跳过次数
- 见代码注释
代码
#include <iostream>
using namespace std;
bool jump(int n){
if(n%7==0) return true;
while(n){
if(n%10==7) return true;
n/=10;
}
return false;
}
int main(){
int n;
cin>>n;
int cur=1,a=0,b=0,c=0,d=0;
while(n){ // n 次循环
if(jump(cur)){ //如果跳过 计数,注意 此时轮到谁加一?
if(cur%4==0) d++;
else if(cur%4==1) a++;
else if(cur%4==2) b++;
else if(cur%4==3) c++;
}
else n--;
cur++;
}
cout<<a<<endl<<b<<endl<<c<<endl<<d;
return 0;
}
2 201912-2 回收站选址
题目描述
样例
笔记
- 数据结构 :利用 set 自动排序,+ 结构体
或 vector + sort + pair - 所有操作分为两部分:确定是否满足选址条件 和 为满足条件的选址打分
- 设 find 函数判断是否存在此坐标,简化代码
代码
#include <iostream>
#include <set>
using namespace std;
struct Point{
int x,y;
Point(int _x,int _y):x(_x),y(_y){}
bool operator < (Point p) const{
return x==p.x?y<p.y:x<p.x;
}
};
set<Point> pts;
bool find(int x,int y){
if(pts.find(Point(x,y))!=pts.end()) return true;
return false;
}
int main(){
int n,x,y;
cin>>n;
for(int i=0;i<n;i++){
cin>>x>>y;
pts.insert(Point(x,y));
}
int ans[5]={0};
set<Point>::iterator it;
for(it=pts.begin();it!=pts.end();it++){
x=(*it).x; y=(*it).y;
if(find(x+1,y)&&find(x-1,y)&&find(x,y-1)&&find(x,y+1)){
int hps=0;
if(find(x+1,y+1)) hps++;
if(find(x-1,y+1)) hps++;
if(find(x+1,y-1)) hps++;
if(find(x-1,y-1)) hps++;
ans[hps]++;
}
}
for(int i=0;i<5;i++){
cout<<ans[i]<<endl;
}
return 0;
}