(一)CCF201903-2 二十四点
- 题目描述
- 题目分析
本题主要是栈stack的使用,以及当遇到加减乘除符号时入栈操作
- 代码
#include <iostream>
#include<cstring>
#include<stack>
using namespace std;
const int N=10;
int main(){
int n;
//char str[N];
string str;
//定义字符栈和数字栈
stack<int> num;
stack<char> s;
cin>>n;
for(int i=0;i<n;i++){
cin>>str;
while(!s.empty()){
s.pop();
}
while(!num.empty()){
num.pop();
}
for(int j=0;j<str.length();j++){
if(str[j]>='0'&&str[j]<='9'){
//入num栈
int number=str[j]-'0';
num.push(number);
}else if(str[j]=='+'){
//入字符栈
char c=str[j];
s.push(c);
}else if(str[j]=='-') {
str[j]='+';
s.push(str[j]);
int number2=(-1)*(str[j+1]-'0');
num.push(number2);
j++;
}else if(str[j]=='x'){
//直接进行计算
int top=num.top();
num.pop();
int sum_part=top*(str[j+1]-'0');
num.push(sum_part);
j++;
}else if(str[j]=='/') {
//直接进行计算
int top2=num.top();
num.pop();
int sum_part2=top2/(str[j+1]-'0');
num.push(sum_part2);
j++;
}
}
//剩下的直接进行相加计算
int sum=0;
int count;
while(!s.empty()) {
int a=num.top();
num.pop();
s.pop();
int b=num.top();
num.pop();
sum=a+b;
num.push(sum);
}
count=num.top();
num.pop();
if(count==24){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
}
return 0;
(二)CCF201604-2 俄罗斯方块
- 题目描述
- 试题分析
该题主要是模拟方块下落,先设置一个数组存储要下落的方块,然后下落,直到有一个方块碰到下面的方块为止,然后再将这些方块进行合并输出
- 代码
#include <iostream>
using namespace std;
const int ROW=15;
const int COL=10;
const int N=4;
int board[ROW+1][COL];
int block[N][N];
struct COORDS {
int row,col;
}coords[N];
int main(){
int row,col;
//输入数据
for(int i=0;i<ROW;i++){
for(int j=0;j<COL;j++){
cin>>board[i][j];
}
}
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
cin>>block[i][j];
}
}
cin>>col;//输入第几列开始
//底边全放1
for(int j=0;j<COL;j++){
board[ROW][j]=1;
}
//提取小方块坐标
int k=0;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(block[i][j]==1){
coords[k].row=i;
coords[k].col=j;
k++;
}
}
}
//模拟小方块落下过程
row=1;//从第一行开始进行下落 ,要是第零行就有则出都出不来
col--;// 1-7的整数
bool checkflag;
//触碰到就结束
for(;;){
checkflag=false;
for(int i=0;i<N;i++){
//某个地方碰见为1
if(board[row+coords[i].row][col+coords[i].col]==1){
checkflag=true;
break;
}
}
if(checkflag) {
break;
}
row++;
}
//因为碰到的是这个图形的下一行,所以要row--
row--;
//合并小方块到方格
for(int i=0;i<N;i++){
board[row+coords[i].row][col+coords[i].col]=1;
}
//输出结果
for(int i=0;i<ROW;i++){
for(int j=0;j<COL;j++){
if(j!=0){
cout<<" ";
}
cout<<board[i][j];
}
cout<<endl;
}
return 0;
}
(三)CCF 201803-2 碰撞的小球
- 题目描述
- 试题分析
1.只可能相邻的两个球相撞,要是碰到就反向(当两个小球在同一个位置上时)
2.只可能第一个小球撞到位置0上,只能第n个小球在L上
3.使用一个数组存储方向,使用一个数组存储位置,若是碰到0或L就那个球就反向,要是两球相碰,两个球就都反向
- 代码
#include<iostream>
using namespace std;
int main(){
//只可能相邻的两个球相撞
//一个数组存储小球的位置,一个存储小球的方向
//要是碰到就反向即两个小球在一个位置上
//只能第一个小球撞到位置0上,只能第n个小球撞到L上
//故仅需两个数组就可以
const int N=100;
int n,L,t;
int step[N];//方向
int pos[N];
//输入数据
cin>>n>>L>>t;
//初始化
for(int i=1;i<N;i++){
step[i]=1;
}
for(int i=1;i<=n;i++){
cin>>pos[i];
}
pos[0]=0;
pos[n+1]=L;
//循环
while(t>0){
t--;
for(int i=1;i<=n;i++){
if(pos[i]==0){
step[i]=-step[i];
}else if(pos[i]==L){
step[i]=-step[i];
}else if(pos[i]==pos[i+1]) {
//碰到就反向
step[i]=-step[i];
step[i+1]=-step[i+1];
}
pos[i]+=step[i];
}
for(int k=1;k<=n;k++){
cout<<"pos"<<"["<<k<<"]"<<"="<<pos[k]<<" ";
}
cout<<endl;
}
for(int i=1;i<=n;i++){
if(i!=1){
cout<<" ";
}
cout<<pos[i];
}
cout<<endl;
return 0;
}
(三)CCF 201709-02 公共钥匙盒
- 问题描述
有一个学校的老师共用N个教室,按照规定,所有的钥匙都必须放在公共钥匙盒里,老师不能带钥匙回家。每次老师上课前,都从公共钥匙盒里找到自己上课的教室的钥匙去开门,上完课后,再将钥匙放回到钥匙盒中。
钥匙盒一共有N个挂钩,从左到右排成一排,用来挂N个教室的钥匙。一串钥匙没有固定的悬挂位置,但钥匙上有标识,所以老师们不会弄混钥匙。
每次取钥匙的时候,老师们都会找到自己所需要的钥匙将其取走,而不会移动其他钥匙。每次还钥匙的时候,还钥匙的老师会找到最左边的空的挂钩,将钥匙挂在这个挂钩上。如果有多位老师还钥匙,则他们按钥匙编号从小到大的顺序还。如果同一时刻既有老师还钥匙又有老师取钥匙,则老师们会先将钥匙全还回去再取出。
今天开始的时候钥匙是按编号从小到大的顺序放在钥匙盒里的。有K位老师要上课,给出每位老师所需要的钥匙、开始上课的时间和上课的时长,假设下课时间就是还钥匙时间,请问最终钥匙盒里面钥匙的顺序是怎样的?
- 输入格式
输入的第一行包含两个整数N, K。
接下来K行,每行三个整数w, s, c,分别表示一位老师要使用的钥匙编号、开始上课的时间和上课的时长。可能有多位老师使用同一把钥匙,但是老师使用钥匙的时间不会重叠。
保证输入数据满足输入格式,你不用检查数据合法性。
- 输出格式
输出一行,包含N个整数,相邻整数间用一个空格分隔,依次表示每个挂钩上挂的钥匙编号。
- 样例输入
5 2
4 3 3
2 2 7
- 样例输出
1 4 3 2 5
- 样例说明
第一位老师从时刻3开始使用4号教室的钥匙,使用3单位时间,所以在时刻6还钥匙。第二位老师从时刻2开始使用钥匙,使用7单位时间,所以在时刻9还钥匙。
每个关键时刻后的钥匙状态如下(X表示空):
时刻2后为1X345;
时刻3后为1X3X5;
时刻6后为143X5;
时刻9后为14325。
- 样例输入
5 7
1 1 14
3 3 12
1 15 12
2 7 20
3 18 12
4 21 19
5 30 9
- 样例输出
1 2 3 5 4
- 试题分析
使用priority_queue进行处理,定义归还与借出规则,使用‘G'代表借出,使用'R'代表归还’
- 代码
#include <iostream>
#include<cstring>
#include<queue>
using namespace std;
struct key{
int num;//钥匙号
char op;
int time;//时间点
bool operator<(key a)const{
if(time!=a.time){
return time>a.time;
}else if(op!=a.op){
return op<a.op;
}else{
return num>a.num;
}
}
};
const int N=1000;
int hook[N+1];
int main(){
int n;
int k;
int w,s,c;
priority_queue<key> q;
key t;
cin>>n>>k;
for(int i=1;i<=n;i++){
hook[i]=i;
}
for(int i=1;i<=k;i++){
cin>>w>>s>>c;
t.num=w;
t.time=s;
t.op='G';
q.push(t);
t.op='R';
t.time=s+c;
q.push(t);
}
while(!q.empty()){
t=q.top();
q.pop();
//若为借钥匙操作
if(t.op=='G'){
for(int i=1;i<=n;i++){
if(hook[i]==t.num){
hook[i]=0;
break;
}
}
}else{
for(int i=1;i<=n;i++){
if(hook[i]==0){
hook[i]=t.num;
break;
}
}
}
}
for(int i=1;i<=n;i++){
if(i!=1){
cout<<" ";
}
cout<<hook[i];
}
cout<<endl;
return 0;
}
(四)参考文献
【1】西安电子科技大学2020年攻读硕士学位研究生复试资料