65,189
社区成员




#include<iostream>
#include<stack>
using namespace std;
bool cmp(char top, char out)
{
if ((top == '+' || top == '-') && (out == '+' || out == '-'))
return true;
else if ((top == 'x' || top == '/') && (out == 'x' || out == '/'))
return true;
else if ((top == 'x' || top == '/') && (out == '+' || out == '-'))
return true;
else
return false;
}
int calculate(int nx, int nh, char sign)
{
switch (sign)
{
case '+': return nx + nh;break;
case '-': return nx - nh;break;
case 'x': return nx * nh;break;
case '/': return nx / nh;break;
}
}
char s[8];
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
stack<int> num;
stack<char> sym;
while (!num.empty())
num.pop();
while (!sym.empty())
sym.pop();
for (int j = 0; j < 8; j++)
{
scanf("%c", &s[j]);
if (s[j] != '\n')
{
if (s[j] >= '0' && s[j] <= '9')
num.push(s[j] - '0');
else if (sym.empty())
{
sym.push(s[j]);
}
else
{
if (cmp(sym.top(), s[j]))
{
while (cmp(sym.top(), s[j]))
{
int nh = num.top();
num.pop();
int nx = num.top();
num.pop();
num.push(calculate(nx, nh, sym.top()));
sym.pop();
}
sym.push(s[j]);
}
else
sym.push(s[j]);
}
}
}
while (!sym.empty())
{
int nh = num.top();
num.pop();
int nx = num.top();
num.pop();
num.push(calculate(nx, nh, sym.top()));
sym.pop();
}
if (num.top() == 24)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
#include <stdio.h>
int main()
{
int n, x[4], p, q, s;
char o[3];
scanf("%d", &n);
while (n--)
{
scanf("%1d%c%1d%c%1d%c%1d", &x[0], &o[0], &x[1], &o[1], &x[2], &o[2], &x[3]);
for (p = q = s = 0; p < 3; p++)
o[p] == 'x' ? x[p + 1] = s = x[p] *= x[p + 1], o[p] = 0, q++ :
o[p] == '/' ? x[p + 1] = s = x[p] /= x[p + 1], o[p] = 0, q++ : 0;
for (p = 0; q < 3; p++)
o[p] == '+' ? x[p + 1] = s = x[p] += x[p + 1], o[p] = 0, q++ :
o[p] == '-' ? x[p + 1] = s = x[p] -= x[p + 1], o[p] = 0, q++ : 0;
if (s == 24) puts("Yes"); else puts("No");
}
return 0;
}
https://bbs.csdn.net/topics/380157851
预测老赵正在迅速赶来~~~