总结一下这次没发挥好的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++) 参考的kenamja 的 ccf 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 ; }