201912-3 化学方程式 (70分) ccf csp

一、
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、代码
水平有限,本来是想照着80分写的(圆括号不可以出现嵌套),但结果只得了70分,如果有大神能看出其中的问题,请您不吝指出。

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;

public class Main {
	static void count(String s,Map map){
	   Stack stack = new Stack<String>();
 	   int times = 1;//前面的系数
 	   int kuo = 0;//栈中左括号数量
 	   for(int j=0;j<s.length();j++){
 		   char c = s.charAt(j);
 		   if(c=='('){
 			   kuo++;
 			   stack.push(c);
 		   }
 		   if(c >= 65 && c <= 90)//大写字母
 			   stack.push(c);
 		   if(c >= 97 && c <= 122){//小写字母
 			   String temp = ""+stack.pop();
 			   temp = temp+c;
 			   stack.push(temp);
 		   }
 		   if(c >= 48 && c <= 57){//是数字
 			   if(kuo > 0){
 				   stack.push(c-48);
 			   }
 			   else if(stack.empty()){//元素前面的数字
 			   //这个数有可能不止一位
 				   String temp =""+(c-48);
 				   while(j+1<s.length() && s.charAt(j+1) >= 48 && s.charAt(j+1) <= 57){
	 				   temp = temp + (s.charAt(j+1)-48);
	 				   j++;
	 			   }
	 	 		   times = Integer.parseInt(temp);
 			   }else{//元素后面的数字
 				   String key = ""+stack.pop();
 				   String temp = ""+(c-48);
 				   while(j+1<s.length() && s.charAt(j+1) >= 48 && s.charAt(j+1) <= 57){
 	 				   temp = temp + (s.charAt(j+1)-48);
 	 				   j++;
 	 			   }
 	 			   int value = times * Integer.parseInt(temp);
 	 			   if(map.containsKey(key))
 	 				   map.put(key,(Integer)map.get(key)+value);
 	 			   else
 	 				   map.put(key, value);
 			   }
 		   }
 		   if(c == '+'){//碰到+,把栈里剩余的元素都计算一下,加到map里
 			   while(!stack.empty()){
 				   String temp = ""+stack.pop();
 				   if(map.containsKey(temp))
     				   map.put(temp,(Integer)map.get(temp)+times);
     			   else
     				   map.put(temp, times);
 			   }
 			   times=1;
 		   }
 		   if(c == ')'){
 		       //拿到括号后面那个数字(如果有的话)
 			   int value = times;
 			   if(j+1<s.length() && s.charAt(j+1) >= 48 && s.charAt(j+1) <= 57){
 				   String temp = ""+s.charAt(j+1);
 				   j++;
 	 			   while(j+1<s.length() && s.charAt(j+1) >= 48 && s.charAt(j+1) <= 57){
 	 				   temp = temp + (s.charAt(j+1)-48);
 	 				   j++;
 	 			   }
 	 			   value = times * Integer.parseInt(temp);
 			   }
 			   
 			   int jiao = 1;//指下角标
 			   //直到'('
 			   String key = ""+stack.pop();
 			   while( !key.equals("(")){
 				   if(key.length()==1 && key.charAt(0)>=48 && key.charAt(0)<=57){
 					   jiao = key.charAt(0)-48;
 				   }else{
 					   if(map.containsKey(key)){
 	     				   map.put(key,(Integer)map.get(key)+(jiao*value));
 					   }
 	     			   else
 	     				   map.put(key, (jiao*value));
 					   jiao=1;
 				   }
 				   key = ""+stack.pop();
 			   }
 			   kuo--;
 		   }
 	   }
 	   //把栈中剩余的元素处理一下
 	   while(!stack.empty()){
 		   String temp = ""+stack.pop();
		   if(map.containsKey(temp))
			   map.put(temp,(Integer)map.get(temp)+times);
		   else
			   map.put(temp, times);
 	   }
	}
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       int n=sc.nextInt();
       for(int i=0;i<n;i++){
    	   String s = sc.next();
    	   String left = s.split("=")[0];
    	   String right = s.split("=")[1];
    	   Map map_left = new HashMap<String, Integer>();
    	   Map map_right = new HashMap<String, Integer>();
    	   
    	   count(left,map_left);
    	   count(right,map_right);

    	   if(map_left.equals(map_right))
    		   System.out.println("Y");
    	   else
    		   System.out.println("N");
    	   
       }
       
    }
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值