当前位置:网站首页>1-2 24:00 (20 points) [CSP certification true question]
1-2 24:00 (20 points) [CSP certification true question]
2022-06-13 09:23:00 【GAOJIN painting】
Define each game by 4 From the 1-9 And 3 It consists of four operators , Make sure that the four operators separate numbers , There are no brackets and other characters , The order of operation follows the order of four operations . In addition, the sign + Express , Subtraction uses the symbol - Express , Multiplication in small letters x Express , Divide with the sign / Express . In the game, division is called integral division , for example 2 / 3 = 0,3 / 2 = 1, 4 / 2 = 2. The teacher gave you n The solution to the game , Please write a program to verify whether the result of each game is 24 .
Input format :
Reading data from standard input . Enter an integer in the first line n, From 2 OK, we're going to start with n + 1 In line , Each line contains a length of 7 String , For the above 24 Click game , Make sure the data format is legal .
Output format :
Output to standard output . contain n That's ok , For every game , If the result is 24 Then the output string Yes, Otherwise output string No.
Examples "> Examples "> Examples "> sample input :
10
9+3+4x3
5+4x5x5
7-9-9+8
5x6/5x4
3+5+7+9
1x1+9-9
1x9-5/9
8/5+6x9
6x7-3x6
6x4+4/5
sample output :
Yes
No
No
Yes
Yes
No
No
No
Yes
Yes
This topic is mainly Consider the priority of the four operations . But I don't know the right conventional way of thinking , Just think for yourself , It turned out to be stuck bug, I spent the whole afternoon looking for bug Spend in .....
Own code :(.. No comments at all. Hahaha , Don't look at this ..
Problems in my thinking , Record as a reminder .
#include <iostream>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<cstring>
#include<map>
#include<stack>
using namespace std;
int sum ;
int main()
{
int n;
cin >> n;// Number of formulas
cin.get();
char ch;
stack<char> zhan1;// Originally, I thought that operators and numbers should be used char To express , The numbers are just -‘0’, That's all right. , but
stack<int>zhan2; // forget char Type can only represent 0-9 Array of , When it is a two digit number , One digit will be lost .
for (int i = 0; i < n; i++)
{
sum = 0;
int j = 0;
while (true)
{
for (; j < 7; ) // Here I was while((ch=cin.get()!='\n'), I thought I could finish reading "enter"
{
ch = cin.get(); Very good , But it's actually out of circulation , But when the outer loop executes
if ((ch >= '0'&&ch <= '9')) Next time , Because there is the next expression , So I began to receive
{ cin>>ch; But the current formula is not finished yet , card bug, I also read it
zhan2.push(ch - 48); For a long time ...
}
else
{
if ((ch == '+') || ch == '-')
{
zhan1.push(ch);
}
else
{
int a = zhan2.top();
zhan2.pop();
char b;
b = cin.get();
j++;
int c = b - 48;
if (ch == '/')// Here I used the time *, But the reality is ×, The question is , Both are not
{ // equally , I've been reporting mistakes all the time , If you use the division sign in the future, it will be ÷ Also pay attention to , can
a = a / c;// You can find the corresponding decimal number , As a basis for judgment .
}
else
{
a = a *c;
}
zhan2.push(a);
}
}
j++;
}
int re;
if (zhan2.size()==1&&zhan1.size()==0)
{
sum = zhan2.top();
zhan2.pop();
if (sum < 0)
sum = 0 - sum;
break;
}
int a = zhan2.top();
zhan2.pop();
int c = zhan2.top();
zhan2.pop();
char b = zhan1.top();
zhan1.pop();
if (b == '+')// Here is what I found in the data given in the experimental topic , When I'm doing calculations , I didn't pay attention to the sequence of operations
re = a + c;// Sequential , Advanced stack in front , The last one in the stack is at the back .
else
re = a - c;
zhan2.push(re);
}
if (sum == 24)
cout << "Yes" << endl;
else
cout << "No" << endl;
getchar();
}
return 0;
}
The right way of thinking : First, for numbers and operators , Use it separately char The type and int Type of stack To store , Because if it is stored only by inserting ,char It can only mean 0~9; So when the result is two digits , One will be lost , I watched it for a long time . Mainly for operator priority analysis : At the top of the stack (* 、/)> Received (*、/)> At the top of the stack (+、-)> Received (+、-). Such priority operations are constantly compared with the operators of the operation stack , For example, receive as +, The priority of the operator at the top of the stack is lower than that of the operator at the top of the stack , Then let the operation at the top of the stack , At the same time, the top of the stack element pops up , After taking part in the calculation , Again, compare with the new stack top element in the pop-up stack , Until the operation participates in the operation or is pushed onto the stack .
At the same time, we can also analyze , Such a priority , The operators in the stack cannot exist at the same time (+ -) And (* /) Of .
Thought code :
It can be noted that :num1,num2 There is a sequence of operations , The last one in the stack is at the back , First in first .
边栏推荐
- 20211108 det(AB)=det(A)det(B)
- C language: minesweeping
- LeetCode 剑指 Offer 30. 包含min函数的栈
- C language: deep understanding of character functions and string functions (2)
- C language time difference calculation
- JUC原子引用与ABA问题
- Online debugging tool Arthas advanced
- LeetCode 202. 快乐数
- 阿里高级专家剖析 | Protocol的标准设计
- CAS NO lock
猜你喜欢
Simulink variant model and variant subsystem usage
BGP 联邦+Community
Neo4j - CQL使用
20220524 how to install coppeliasim to disk D
Jenkins accédant à l'authentification de l'utilisateur openldap
Storage mode of drawings
Library management system based on wechat applet Rar (thesis + source code)
IP address introduction
Figure introduction to database neo4j
BGP Federation +community
随机推荐
20211115 any n-order square matrix is similar to triangular matrix (upper triangle or lower triangle)
Neo4j - CQL use
JUC 字段更新器
Overview of common layers of image recognition neural network (under update)
Jenkins accédant à l'authentification de l'utilisateur openldap
Heap
Storage mode of drawings
Routing - static routing
C language: Simulated Implementation of library function strcpy
JUC Unsafe
@Value不生效及extend/implement其他类无法注入bean的手动注入
LeetCode 343. 整数拆分
Lecture par lots de tous les fichiers vocaux sous le dossier
JUC field Updater
I set up a blog
Online debugging tool Arthas advanced
计算两个时间相差的天数(支持跨月、跨年)
BGP 联邦+Community
JUC atomic array
Neo4j - CQL使用