zoukankan      html  css  js  c++  java
  • [CCF] 201903-2 二十四点 Apaer_xzc

    [CCF] 201903-2 二十四点


    题面


    在这里插入图片描述

    思路:计算中缀表达式,然后判断24即可


    我的代码

    //CCF 201903-2 二十四点  
    #include <bits/stdc++.h>
    using namespace std;
    char str[10],in[20];
    int a[10]; 
    int super(char ch)
    {
    	if(ch=='+'||ch=='-') return 2;
    	if(ch=='x'||ch=='/'||ch=='*') return 3;
    	if(ch=='#') return 0;
    	return -1; 
    }
    int cal(int x,int y,char op)
    {
    	if(op=='+') return x+y;
    	else if(op=='-') return x-y;
    	else if(op=='/') return x/y;
    	else return x*y;	
    } 
    void solve()
    {	
    	stack<int> num;
    	stack<char> alp;
    	alp.push('#');
    	for(int i=0;i<7;++i)
    	{
    		if(isdigit(in[i]))	
    		{
    			num.push(in[i]-'0');
    		}
    		else
    		{
    			char now = in[i];
    			while(super(now)<=super(alp.top()))
    			{
    				char tp = alp.top();   alp.pop();
    				int right = num.top(); num.pop();
    				int left  = num.top(); num.pop();
    				num.push(cal(left,right,tp));
    			}
    			alp.push(now);
    		} 
    	} 
    	while(!alp.empty()&&alp.top()!='#')
    	{
    		char tp = alp.top();
    		alp.pop();
    		int right = num.top();num.pop();
    		int left  = num.top();num.pop();
    		num.push(cal(left,right,tp));
    	}
    	if(num.top()==24) puts("Yes");
    	else puts("No");
    }
    int main()
    {
    	int T;cin>>T;
    	while(T--)
    	{
    		scanf("%s",in); 
    		solve();
    	}
    	
    	return 0;	
    } 
    

  • 相关阅读:
    MongoDB数据库学习总结
    windows及linux环境下永久修改pip镜像源的方法
    enumerate枚举函数
    sum求和函数
    zip拉链函数
    python实现面对面快传
    redis实现发布订阅
    python自定义classmethod
    Nginx为什么支持那么高的并发量?
    java进阶(28)--Map集合
  • 原文地址:https://www.cnblogs.com/Apare-xzc/p/12243623.html
Copyright © 2011-2022 走看看