CCF 201912-3 化学方程式
简短的分析
- "="拆分方程式;
- "+"继续拆分;
- 首先判断一个项的首位,若不是数字,则默认总的倍数为1,若是数字则倍数为数字(代码中变量为zong);
- 继续判断,若是字母,判断后面是否有小写字母,有,则作为一个整体存入栈a,无则直接存入栈a;继续判断是否有数字,若有则存入栈b,若无则将1存入栈b。若是左括号,存入栈a。若是右括号,判断右括号后的数字count(没有则默认为1),弹出a栈栈顶元素m,弹出b栈栈顶元素n,用count*n存入辅助栈t,同时m存入辅助栈p,重复此过程直到遇到左括号(左括号要弹出),再将p、t分别压入栈a、b。
5.一个项判断完后,弹出栈a栈顶元素n、b栈顶元素m,n、m*zong存入Map。
代码:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String args[]) {
Main g = new Main();
Scanner in = new Scanner(System.in);
int n=in.nextInt();
in.nextLine();
for(int i=0;i<n;i++) { String str = in.nextLine(); char[] s = str.toCharArray(); String[] arr = str.split("="); if (g.a(arr[0]).equals(g.a(arr[1]))) { System.out.println("Y"); } else { System.out.println("N"); }
}
}
public Map<String, Integer> a(String o) { Map<String, Integer> map = new HashMap<>();
String[] p = o.split("\\+");
for (int i = 0; i < p.length; i++) { if (i == 0) { map = b(p[i]); }else { map = add(map, b(p[i])); }
}
return map;
}
public Map<String, Integer> b(String x) {
Map<String, Integer> map = new HashMap<>();
char[] s = x.toCharArray();
Stack stack_a = new Stack();
Stack stack_b = new Stack();
int zong=1;
for (int i = 0; i < s.length; i++) { if (i == 0) { if (s[i] >= '0' && s[i] <= '9') { zong = s[i]-'0'; i++; while (true) { if (s[i] >= '0' && s[i] <= '9') { zong = zong * 10 + (s[i]-'0'); i++; } else { break; } } i--; } } if(s[i]>='A'&&s[i]<='Z') { if((i+1)>=s.length){ stack_a.push(String.valueOf(s[i])); }else if(s[i+1]>='a'&&s[i+1]<='z') { String t=String.valueOf(s[i])+String.valueOf(s[i+1]); stack_a.push(t); i++; }else { stack_a.push(String.valueOf(s[i])); } if((i+1)>=s.length) { stack_b.push(1); }else if(s[i+1]>='0'&&s[i+1]<='9') { int count=s[i+1]-'0'; i++; while(true) { if((i+1)>=s.length) { break; }else if(s[i+1]>='0'&&s[i+1]<='9') { count=count*10+(s[i+1]-'0'); i++; }else { break; } } stack_b.push(count); } else { stack_b.push(1); } } if(s[i]=='(') { stack_a.push(String.valueOf(s[i])); } if(s[i]==')') { Stack w=new Stack(); Stack q=new Stack(); int r=0; int count=0; int y=0; if((i+1)<s.length&&s[i+1]>='0'&&s[i+1]<='9') { count=s[i+1]-'0'; i++; while(true) { if(s.length<=(i+1)) { break; } else if(s[i+1]>='0'&&s[i+1]<='9') { count=count*10+(s[i+1]-'0'); i++; }else { break; } } }else { count=1; } while(!stack_a.empty()&&!stack_b.empty()) { if(((String)stack_a.peek()).equals("(")) { stack_a.pop(); break; }else { w.push((String) stack_a.pop()); q.push(((int)stack_b.pop())*count); } } while(!w.empty()&&!q.empty()) { stack_a.push((String)w.pop()); stack_b.push((int)q.pop()); } }
}
while(!stack_a.empty()&&!stack_b.empty()) { if(map.containsKey((String)stack_a.peek())) { map.put((String)stack_a.peek(),((int)stack_b.pop())*zong+map.get((String)stack_a.peek())); stack_a.pop(); }else { map.put((String)stack_a.pop(),((int)stack_b.pop())*zong); }
}
return map;
}
public Map<String, Integer> add(Map<String, Integer> map_a, Map<String, Integer> map_b) { for(Map.Entry<String,Integer> m:map_a.entrySet())
{ if(map_b.containsKey((String)m.getKey())) { map_b.put((String)m.getKey(),(int)(m.getValue()+map_b.get((String)m.getKey()))); }else { map_b.put((String)m.getKey(),(int)m.getValue()); }
}
return map_b;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
解法不怎么样,赚点积分。( ̄▽ ̄)"
文章来源: blog.csdn.net,作者:qq_43741026,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_43741026/article/details/115049050