zoey
点击国内主页可以让浏览速度加快哦 ~ !

CCF&&CSP 2019-03 (前2道)

2019-09-02 csp C/C++ 堆栈法
Word count: 745 | Reading time: 3min


    总结一下这次没发挥好的CSP:
    




问题 A: 小中大

题目

问题 A: 小中大





原题链接

More info:Question


cout控制小数点后位数(复习)

头文件为:iomanip

函数为:fixed << setprecision(n)

控制输出小数点后第n位:

1
2
#include<iomanip>
cout << fixed << setprecision(n) << 变量<< endl;

AC源码(c++)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<algorithm>
#include<iomanip>
using namespace std;
const int maxn = 1e5 + 1;
int main() {
int n,a[maxn];
while(cin>>n){
for (int i = 0; i < n; i++) cin >> a[i];
cout << max( a[0], a[n - 1] ) << " ";
if (n % 2 == 1) cout << a[n / 2 ];
else {
if ((a[n / 2] + a[n / 2 - 1]) % 2 == 0) cout << (a[n / 2] + a[n / 2 - 1]) / 2;
else cout << fixed << setprecision(1) << 1.0 *( a[n / 2] + a[n / 2 - 1] ) / 2.0 ;
}
cout << " " << min(a[0], a[n - 1]) << endl;
}
return 0;
}

问题 B: 24点

题目

问题 B: 24点





原题链接

More info:Question


C++ Stack(堆栈) 是一个容器类的改编,为程序员提供了堆栈的全部功能,——也就是说实现了一个先进后出(FILO)的数据结构。
栈(stack)是限制插入和删除只能在一个位置上进行的线性表,该位置在表的末端,叫做栈顶。添加元素只能在尾节点后添加,删除元素只能删除尾节点,查看节点也只能查看尾节点。添加、删除、查看依次为入栈(push)、出栈(pop)、栈顶节点(top)。形象的说,栈是一个先进后出(LIFO)表,先进去的节点要等到后边进去的节点出来才能出来。

c++ stl栈stack的头文件为:

1
2
 #include <stack> 
stack<数据类型> 变量名

c++ stl栈stack的成员函数介绍

操作 比较和分配堆栈

empty() 堆栈为空则返回真

pop() 移除栈顶元素 (删除)

push() 在栈顶增加元素 (增加)

size() 返回栈中元素数目

top() 返回栈顶元素,不删除(获取)


AC源码(c++)

参考的kenamjaccf csp2019.3.17 第二题 24点堆栈解法 ,当时考试的时候用的类似暴力的写法,而且分也没得全,看见这个堆栈法觉得写得很棒,所以想分享一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
#include<stack>
using namespace std;
stack<int> sta;
int res(string x) {
for (int i = 0; i < x.length(); i++) {
if (x[i] > '0' && x[i] <= '9') {
sta.push(x[i] - '0');
}
else if ('-' == x[i]) {
sta.push(0 - x[++i] + '0');
}
else if('x' == x[i] || '/' == x[i]){
char cmp = x[i];
int buf = x[++i]-'0';
if ('x' == cmp) {
buf *= sta.top();
sta.pop();
sta.push(buf);
}
else {
buf = sta.top()/buf;
sta.pop();
sta.push(buf);
}
}
}
int ans = 0;
while (!sta.empty()) {
ans += sta.top();
sta.pop();
}
return ans;
}
int main()
{
int t;
string s;
cin>>t;
while (t--) {
cin >> s;
if (24 == res(s))cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}

Author: Zoey

Link: https://zoey1038569979.github.io/2019/09/02/ccf_201903/

Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

< PreviousPost
CCF&&CSP 2017-09 (前2道)
NextPost >
校内算法考试(第三次)总结
CATALOG
  1. 1. 问题 A: 小中大
    1. 1.1. 题目
    2. 1.2. 原题链接
    3. 1.3. cout控制小数点后位数(复习)
    4. 1.4. AC源码(c++)
  2. 2. 问题 B: 24点
    1. 2.1. 题目
    2. 2.2. 原题链接
    3. 2.3.
    4. 2.4. AC源码(c++)