CCF-CSP真题《201912-3 化学方程式》java满分题解

真题来源:化学方程式


Java满分题解:

import java.io.*;
import java.util.*;
import java.util.Map.Entry;

public class Main {

	
	
	static final int N=2510;
    static int n,m;
    static int id;
    static int len,mid;

    
    public static void main(String[] args) throws IOException {
       BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
       PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
       
       String str = bf.readLine();
       n=Integer.parseInt(str);
       for(int z=0;z<n;z++) {
    	   str=bf.readLine();
    	   char[] s=str.toCharArray();
    	   len=s.length;
           
           for(int i=0;i<len;i++) {
        	   if(s[i]=='=') {
        		   mid=i;
        		   break;
        	   }
           }
           
           id=0;
           Map<String,Integer> mp = new HashMap<>();
           List<Pair<String,Integer> > arr = calculate(s,mid);
           for(Pair<String,Integer> u:arr) {
        	   String key = u.first;
        	   Integer val = u.second;
//        	   pw.print(u.first+" "+u.second+" ");
        	   if(mp.containsKey(key)){
        		   mp.replace(key, mp.get(key)+val);
        	   }
        	   else mp.put(key,val);
           }
//           pw.println();
     
           id=mid+1;
           List<Pair<String,Integer> > arr2 =calculate(s,len);
           for(Pair<String,Integer> u:arr2) {
        	   String key = u.first;
        	   Integer val = u.second;
//        	   pw.print(u.first+" "+u.second+" ");
        	   if(mp.containsKey(key)){
        		   mp.replace(key, mp.get(key)-val);
        	   }
        	   else mp.put(key,val);
           }
//           pw.println();
           boolean flag=true;
           for(Entry<String,Integer> u:mp.entrySet()) {
        	   Integer val=u.getValue();
        	   if(val!=0) {
        		   pw.println("N");
        		   flag=false;
        		   break;
        	   }
           }
           if(flag)pw.println("Y");
       }
       pw.flush();
       
    }

    public static List<Pair<String,Integer> > calculate(char[] str,int len) {
    	List<Pair<String,Integer> > arr = new ArrayList<>();
    	
    	while(id<len) {
    		if(str[id]=='+')id++;
    		int coef=getDigit(str,len);
    		List<Pair<String,Integer> > arr2 = calculateFormula(str,len);
    		add2All(arr2,arr,coef);
    	}
    	return arr;
    }
    
    public static List<Pair<String,Integer> > calculateFormula(char[] str,int len){
    	List<Pair<String,Integer> > arr = new ArrayList<>();
    	
    	while(id<len) {
    		if(str[id]=='+')break;
    		if(str[id]=='(') {
        		int left=1;
        		int t=id+1;
        		while(true) {
        			id++;
        			if(str[id]=='(')left++;
        			else if(str[id]==')')left--;
        			if(left==0) {
        				int len2=id;
        				id=t;
        				List<Pair<String,Integer> > arr2 =calculateFormula(str,len2);
        				id=len2+1;
        				int coef2 = getDigit(str,len);
        				add2All(arr2,arr,coef2);
        				break;
        			}
        		}
        	}
        	else {
        		String key = getElement(str,len);
        		int coef2 = getDigit(str,len);
        		arr.add(new Pair(key,coef2));
        	}
		}
    	return arr;
    }
    
    public static void add2All(List<Pair<String,Integer> > a,List<Pair<String,Integer> > b ,int x) {
    	for(Pair<String,Integer> u:a) {
			b.add(new Pair(u.first,u.second*x));
		}
    }
    public static int getDigit(char[] str,int len) {
    	int coef=1;
    	if(id<len&&isDigit(str[id])) {
    		coef=0;
    		while(id<len&&isDigit(str[id])) {
    			coef=coef*10+str[id]-'0';
    			id++;
    		}
    	}
    	
    	return coef;
    }
    
    public static String getElement(char[] str,int len) {
    	StringBuffer sb= new StringBuffer();
    	sb.append(str[id]);
    	id++;
    	while(id<len&&isLowerCase(str[id])) {
			sb.append(str[id]);
			id++;
		}
    	return sb.toString();
    }
    
    public static boolean isDigit(char c) {
    	if(c>='0'&&c<='9')return true;
    	return false;
    }
    
    public static boolean isUpperCase(char c) {
    	if(c>='A'&&c<='Z')return true;
    	return false;
    }
    
    public static boolean isLowerCase(char c) {
    	if(c>='a'&&c<='z')return true;
    	return false;
    }
   
    public static class Pair<F,S>{
    	F first;
    	S second;
    	public Pair(F first,S second) {
    		this.first = first;
    		this.second = second;
    	}
    }
    
    
}

运行结果:

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值