第十六次CCF CSP考试代码

第一题:小中大。
题目就是给你一个有序数组,然后计算他的最大值,中位数,最小值。
**如果是小数,则保留一位小数。否则以整数形式输出。**这个很重要,因为中位数可能是小数,也可能是整数。因此需要分情况考虑。

#include <iostream>
using namespace std;

#define llong long long

llong max(llong a, llong b){return a < b ? b : a;}
llong min(llong a, llong b){return a < b ? a : b;}
int main(){
	llong n, arr[100005];
	cin>>n;
	for(int i = 0; i < n; i++)cin>>arr[i];
	cout<<max(arr[0], arr[n-1])<<" ";
	if(n % 2){cout<<arr[n/2]<<" ";}
	else {
		llong a = arr[n/2] + arr[n/2-1];
		if(a % 2)cout<<a*1.0/2<<" ";
		else cout<<a/2<<" ";
	}
	cout<<min(arr[0], arr[n-1])<<endl;
}

第二题:二十四点
其实就是将输入的字符串计算就行,总共三个运算符。我们可以先计算前两个运算符中优先级高的那个(这样不会改变最终结果),这样就可以将三个运算符转换成两个。最后在进行一次两个运算符的计算即可。

注意:这里面的乘法是小写x

#include <iostream>
using namespace std;

int rank(char op){
	if(op == '-' || op == '+') return 1;
	return 2;
}

int cal1(int a, int b, char op){
	if(op == '+') return a + b;
	if(op == '-') return a - b;
	if(op == '/') return a / b;
	return a * b;
}

int cal2(int a, int b, int c, char op1, char op2){
	if(rank(op1) < rank(op2)){
		return cal1(a, cal1(b, c, op2), op1);
	}else {
		return cal1(cal1(a, b, op1), c, op2);
	}
}

int cal3(int a, int b, int c, int d, char op1, char op2, char op3){
	if(rank(op1) < rank(op2)){
		return cal2(a, cal1(b, c, op2), d, op1, op3);
	}else {
		return cal2(cal1(a, b, op1), c, d, op2, op3);
	}
}

int main(){
	int n;
	cin>>n;
	while(n){
		n--;
		string str; cin>>str;
		int a = str[0]-48; char op1 = str[1];
		int b = str[2]-48; char op2 = str[3];
		int c = str[4]-48; char op3 = str[5];
		int d = str[6]-48;
		int r = cal3(a, b, c, d, op1, op2, op3);
		if(r == 24) cout<<"Yes"<<endl;
		else cout<<"No"<<endl;
	}
}

第四题:消息传递接口
题目意思很简单。思路也很简单,就直接模拟这个过程就行。主要就是注意字符串处理即可。需要注意的是,我们最好将消息队列定义为全局量,每个样例前将队列清空即可。这样可避免栈空间不够。

#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstring>
#include <queue>
using namespace std;

#define llong long long

struct node{
	char c; int a;
	node(char _c, int _a){
		c = _c;
		a = _a;
	}
};

queue<node> q[10005];
int main(){
	//ios::sync_with_stdio(false);
	int n, m; string s;
	cin>>n>>m;getchar();
	while(n){
		n--;
		for(int i = 0; i < m; i++){
			q[i] = queue<node>();
			getline(cin, s);
			stringstream ss(s);
			string str;
			while (ss >> str) {
				q[i].push(node(str[0], atoi(str.substr(1).c_str())));
			}
		}
		bool flag = false;
		while(!flag){
			flag = true;
			for(int i = 0; i < m; i++){
				if(q[i].empty())continue;
				node n = q[i].front();
				char c = n.c;
				int p1 = n.a;
				if(q[p1].empty())continue;
				node n2 = q[p1].front();
				char s = n2.c;
				int p2 = n2.a;
				if(p2 == i){
					if(c == 'S' && s == 'R'){q[p2].pop(); q[p1].pop();flag = false;}
					if(c == 'R' && s == 'S'){q[p2].pop(); q[p1].pop();flag = false;}
				}
			}
		}
		for(int i = 0; i < m; i++){
			if(!q[i].empty()){flag = false;break;}
		}
		if(!flag)cout<<1<<endl;
		else cout<<0<<endl;
	}
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值