【C++ | PTA】Easy chemistry

该文是一道编程题,要求编写程序检查输入的化学方程式是否平衡,即等号两侧元素数量相等。程序需处理多个测试用例,每个用例为一个化学方程式,考虑元素和化合物的数量。若方程式平衡输出Easy!,否则输出Hard!。

题目要求

In this question, you need to write a simple program to determine if the given chemical equation is balanced. Balanced means that the amount of elements on both sides of the “=” sign is the same.

H 2 O + C O 2 = H 2 C O 3 H_2O+CO_2=H_2CO_3 H2O+CO2=H2CO3
We guarantees that each chemical equation satisfies the basic rules.

If you don’t know the basic rules of chemical equation writing, you can refer to the following conditions:

  • The chemical equation contains exactly one “=” sign.
  • For every chemical element, the first letter of each element is uppercase, and the rest of the letters are lowercase.
  • The number at the end of the element represents the amount of this element needed, If there is no number, it means only one element is needed.
  • Every chemical object may contain several chemical elements like NaCl.
  • The number at the beginning of every chemical object represents the number of this object, If there is no number, it means only one object is needed.
  • Every chemical object will be connected by “+” sign.
  • Chemical reaction is considered to rearrange the atoms after they are broken up

Input
The first line contains a single integer T(1≤T≤100), indicating the number of test cases.
In the next T lines, each line contains a string S(1≤∣S∣≤100), representing a Chemical equation.
It is guaranteed that S only contains uppercase letters, lowercase letters, “=” sign and “+” sign, the sum of the amount of all elements will not exceed 2147483647.

Output
If the given chemical equation is balanced, please output “Easy!”, otherwise output “Hard!” (Without quotes).

Sample Input
3
H2O+CO2=H2CO3
2NaHCO3=Na2CO3+H2O+CO2
3Cu+8HNO3=3CuNO3+2NO+4H2O

Sample Output
Easy!
Easy!
Hard!

题目翻译

在这道题目中,你需要编写一个简单的程序来判断给定的化学方程式是否平衡。平衡指的是等号两侧所含各元素的数量相等。

例如,对于方程式 H 2 O + C O 2 = H 2 C O 3 H_2O+CO_2=H_2CO_3 H2O+CO2=H2CO3,它是平衡的。

我们保证每个化学方程式都满足以下基本规则:

  • 化学方程式中恰好有一个等号。
  • 每个元素的第一个字母大写,其余字母小写。
  • 元素后面的数字表示该元素需要的数量,如果没有数字,则表示只需要一个该元素。
  • 每个化学物质可能包含多个元素,如 NaCl。
  • 每个化学物质前面的数字表示该物质的个数,如果没有数字,则表示只需要一个该物质。
  • 化学反应被认为是将原子打散重新组合。

输入格式:

第一行包含一个整数 T ( 1 ≤ T ≤ 100 ) (1\leq T\leq100) (1T100),表示测试用例的数量。
接下来有 T 行,每行包含一个字符串 S ( 1 ≤ ∣ S ∣ ≤ 100 ) (1\leq |S|\leq100) (1S100),表示一个化学方程式。
保证 S 只包含大写字母、小写字母、等号和加号,所有元素数量之和不会超过 2 31 − 1 2^{31}-1 2311

输出格式:

对于每个测试用例,如果其对应的化学方程式平衡,则输出 “Easy!”,否则输出 “Hard!”。

输入样例:
3
H2O+CO2=H2CO3
2NaHCO3=Na2CO3+H2O+CO2
3Cu+8HNO3=3CuNO3+2NO+4H2O

输出样例:
Easy!
Easy!
Hard!

代码

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
using namespace std;

map<string, int> m;

void GetElement(string str, int div) {
    string s0, s1;
    int coe = 1;
    int back = 1;

    int i = 0;
    while (i < str.size() && str[i] >= '0' && str[i] <= '9')
        s0 += str[i++];
    if (!s0.empty())
        coe = stoi(s0);
    s0.clear();
    for (; i < str.size(); i++) {
        if (str[i] >= 'A' && str[i] <= 'Z') {
            s0 += str[i];
            while (i + 1 < str.size() && str[i + 1] >= 'a' && str[i + 1] <= 'z')
                s0 += str[++i];

            int tag = 0;
            while (i + 1 < str.size() && str[i + 1] >= '0' && str[i + 1] <= '9')
                tag = 1, s1 += str[i + 1], i++;
            if (tag) back = stoi(s1), i--;

            if (div)
                m[s0] -= coe * back;
            else
                m[s0] += coe * back;
            s0.clear();
            s1.clear();
            back = 1;
        }
    }
}

bool isEqual(string str) {
    vector<string> v;
    string s0;
    for (int i = 0; i < str.size(); i++) {
        if (str[i] == '=') v.push_back("-1");
        int tag = 0;
        while (i < str.size() && str[i] != '+' && str[i] != '=') tag = 1, s0 += str[i], i++;
        if (tag) v.push_back(s0), i--;
        s0.clear();
    }

    int div = 0;
    for (int i = 0; i < v.size(); i++) {
        if (v[i] == "-1") {
            div = 1;
            continue;
        }
        GetElement(v[i], div);
    }

    for (auto it = m.begin(); it != m.end(); it++)
        if (it->second) return false;
    return true;
}

int main() {
    int T;
    string str;
    cin >> T;
    while (T--) {
        cin >> str;
        if (isEqual(str))
            cout << "Easy!" << endl;
        else
            cout << "Hard!" << endl;
        m.clear();
    }

    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋说

感谢打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值