24 o'clock (transfer gate)
This problem is relatively convoluted. At the beginning of violent cracking, there were a lot of steps. Later, stacks were used to solve it, which is relatively simple
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
Output:
Yes No No Yes Yes No No No Yes Yes
Full Score code 1
#include <bits/stdc++.h> using namespace std; int n; int result = 0; bool judge[102]; string str; int call(int a,int b,char c){ int res; switch(c){ case 'x': res=a*b; break; case '/': res=a/b; break; case '+': res=a+b; break; case '-': res=a-b; break; } return res; } bool cal(){ char c[6]; int coun = 0; int j = -1; c[1]=str[1];c[3]=str[3];c[5]=str[5]; for(int i = 1; i < 6; i+=2){ if(c[i]=='x' || c[i]=='/'){ c[i-1] = '1'; coun++; }else{ c[i-1] = '0'; } } if(coun == 3 || coun == 0){ //Example 1 + 2 + 3 + 4 1 * 2 * 3 * 4 1 / 2 / 3 / 4 result = call(str[0]-'0',str[2]-'0',str[1]); result = call(result,str[4]-'0',str[3]); result = call(result,str[6]-'0',str[5]); }else{ if(coun == 1){ for(int i = 1; i < 6; i+=2){ if(c[i-1] == '1'){ result = call(str[i-1]-'0',str[i+1]-'0',str[i]); j = i; } } if(j == 1){ //Example 1*2+3-4 result = call(result,str[4]-'0',str[3]); result = call(result,str[6]-'0',str[5]); }else if(j == 3){ //Example 1+2*3-4 result = call(str[0]-'0',result,str[1]); result = call(result,str[6]-'0',str[5]); }else{ //Example 1+2+3*4 int res = call(str[0]-'0',str[2]-'0',str[1]); result = call(res,result,str[3]); } }else{ for(int i = 1; i < 6; i+=2){ if(c[i-1] == '0'){ j = i; } } if(j == 1){ //Example 1+2*3/4 result = call(str[2]-'0',str[4]-'0',str[3]); result = call(result,str[6]-'0',str[5]); result = call(str[0]-'0',result,str[1]); }else if(j == 3){ //Example 1*2+3*4 int res = call(str[0]-'0',str[2]-'0',str[1]); result = call(str[4]-'0',str[6]-'0',str[5]); result = call(res,result,str[3]); }else{ //Example 1*2*3-4 result = call(str[0]-'0',str[2]-'0',str[1]); result = call(result,str[4]-'0',str[3]); result = call(result,str[6]-'0',str[5]); } } } if(result == 24){ return true; } return false; } int main(){ cin >> n; for(int i = 0 ; i < n; i++){ cin >> str; if(cal()){ judge[i] = true; } } for(int i = 0; i < n; i++){ if(judge[i]){ cout << "Yes\n"; }else{ cout << "No\n"; } } return 0; }
Full Score code 2 -------- using stack implementation -------- referring to other people's code to know how to use stack
#include <iostream> using namespace std; int main(){ int n, num[5], temp;//Number of lines - num [top of stack, --] char op[5]; //operator num[0] = 0; //Initializing the operand stack op[0] = 0; //Initializing the operator stack cin>>n; int result[n]; for(int i=0; i<n; ++i){ for(int j=0; j<7; ++j){ if(j%2 == 0){ //Stack of operands cin>>num[++num[0]]; if (op[op[0]] == '-') //If the top of the operator stack is - convert the operand + (- x) num[num[0]] *= -1; else if (op[op[0]] == 'x'){ //x operation required temp = num[num[0]] * num[num[0]-1]; num[--num[0]] = temp; --op[0]; } else if(op[op[0]] == '/'){ //Need / operation temp = num[num[0]-1] / num[num[0]]; num[--num[0]] = temp; --op[0]; } } else{ //Operator stack cin>>op[++op[0]]; } } while(op[0] > 0){ //Finish the remaining + sign temp = num[num[0]] + num[num[0]-1]; num[--num[0]] = temp; --op[0]; } if(num[1] == 24) //Decision result result[i] = 1; else result[i] = 0; num[0] = 0; //Initializing the operand stack } for(int i=0; i<n; ++i){ //Print results if(result[i] == 1) cout<<"Yes\n"; else cout<<"No\n"; } return 0;
Here is the title O(∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩
Test question No.: |
201903-2 |
---|---|
Test question Name: | Twenty-four points |
time limit: | 1.0s |
Memory limit: | 256.0MB |
Problem Description: |
![]() ![]() |