代码:
#include <iostream>
#include <cmath>
using namespace std;
const int N = 100010;
int arr[N];
int main(){
int n, a, b, c;
cin >> n;
for(int i = 0; i < n; i ++)
cin >> arr[i];
a = arr[0];
c = arr[n - 1];
if(a < c)
swap(a, c);
if(n & 1)
b = 2 * arr[n / 2];
else
b = arr[n / 2] + arr[n / 2 - 1];
cout << a << " ";
if(b & 1)
printf("%.1lf ", b / 2.0);
else
printf("%d ", b / 2);
cout << c << endl;
return 0;
}
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include <unordered_map>
using namespace std;
stack<int> num;
stack<char> op;
void eval()
{
int b = num.top(); num.pop();
int a = num.top(); num.pop();
char c = op.top(); op.pop();
int x;
if (c == '+') x = a + b;
else if (c == '-') x = a - b;
else if (c == 'x') x = a * b;
else
{
if (a * b >= 0) x = a / b;
else // 向下取整。C++中a/b是向零取整
{
if (a % b == 0) x = a / b;
else x = a / b - 1;
}
}
num.push(x);
}
int main()
{
unordered_map<char, int> pr;
pr['+'] = pr['-'] = 1;
pr['x'] = pr['/'] = 2;
int n;
cin >> n;
while (n -- )
{
string str;
cin >> str;
num = stack<int>();
op = stack<char>();
for (auto c: str)
if (c >= '0' && c <= '9') num

本文探讨了三个编程主题:利用SpFA解决稀疏图最短路径问题、实现24点游戏算法以及代码片段展示了如何处理字符串和数据结构。通过实例展示了算法在实际问题中的应用。




最低0.47元/天 解锁文章
2564

被折叠的 条评论
为什么被折叠?



