配平
定义
当化学反应的反应物和生成物都确定时,由于质量守恒定律,反应前后的原子数量、种类相同。
但上式中, 和 原子并没有质量守恒。所以我们通过在反应物前面加上系数使其质量守恒。这个过程叫做配平化学方程式。
方法
我们的目的是:找到能够配平化学方程式的各种方法。
现实中的数据是保证随机的,因此一些特殊方法也可以支持。
- 观察法
通过观察角标找到规律。
如上例, 在左右两边的角标是 的关系。易知左边 的系数是右边的 倍。同理, 的左右系数之比也可以得出()。
故明显四氧化三铁的系数为 。
- 数感法
直接猜
- 定一法
定下一个反应物或者生成物的系数为 ,最后将所有分数乘以分母的最小公倍数,保证:
- 均为整数。
- 两两互质。
4. 计算机法(正解!)
先识别反应物、生成物,建立多元方程组,解出来。
若无法解,暴力搜索 1~10 的系数,复杂度 , 为反应物和生成物的总数。
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <set>
#include <bits/stdc++.h>
using namespace std;
struct Compound {
map<string, int> elements;
string original;
};
Compound parse_compound(const string& comp) {
Compound result;
result.original = comp;
int n = comp.size();
int i = 0;
vector<int> multipliers = {1};
while (i < n) {
if (comp[i] == '(') {
multipliers.push_back(1);
i++;
}
else if (comp[i] == ')') {
if (multipliers.size() > 1) {
multipliers.pop_back();
}
i++;
if (i < n && isdigit(comp[i])) {
int num = 0;
while (i < n && isdigit(comp[i])) {
num = num * 10 + (comp[i] - '0');
i++;
}
if (!multipliers.empty()) {
multipliers.back() = num;
}
}
}
else if (isupper(comp[i])) {
string elem;
elem += comp[i];
i++;
while (i < n && islower(comp[i])) {
elem += comp[i];
i++;
}
int num = 1;
if (i < n && isdigit(comp[i])) {
num = 0;
while (i < n && isdigit(comp[i])) {
num = num * 10 + (comp[i] - '0');
i++;
}
}
int total_multiplier = 1;
for (int m : multipliers) {
total_multiplier *= m;
}
result.elements[elem] += num * total_multiplier;
}
else {
i++;
}
}
return result;
}
vector<Compound> parse_side(const string& side) {
vector<Compound> result;
stringstream ss(side);
string comp;
while (getline(ss, comp, '+')) {
comp.erase(remove_if(comp.begin(), comp.end(), ::isspace), comp.end());
if (!comp.empty()) {
result.push_back(parse_compound(comp));
}
}
return result;
}
vector<string> get_all_elements(const vector<Compound>& reactants, const vector<Compound>& products) {
set<string> elem_set;
for (const auto& comp : reactants) {
for (const auto& pair : comp.elements) {
elem_set.insert(pair.first);
}
}
for (const auto& comp : products) {
for (const auto& pair : comp.elements) {
elem_set.insert(pair.first);
}
}
return vector<string>(elem_set.begin(), elem_set.end());
}
int gcd(int a, int b) {
a = abs(a);
b = abs(b);
return b == 0 ? a : gcd(b, a % b);
}
// 针对简单反应的专用求解器
vector<int> solve_simple_reaction(const vector<Compound>& reactants, const vector<Compound>& products) {
// 处理 H2 + Cl2 -> HCl 这类反应
if (reactants.size() == 2 && products.size() == 1) {
set<string> react_elems;
for (const auto& elem : reactants[0].elements) react_elems.insert(elem.first);
for (const auto& elem : reactants[1].elements) react_elems.insert(elem.first);
set<string> prod_elems;
for (const auto& elem : products[0].elements) prod_elems.insert(elem.first);
if (react_elems == prod_elems) {
// 检查是否是A2 + B2 -> AB类型反应
bool is_binary = true;
for (const auto& comp : reactants) {
if (comp.elements.size() != 1) {
is_binary = false;
break;
}
auto it = comp.elements.begin();
if (it->second != 2) {
is_binary = false;
break;
}
}
if (is_binary && products[0].elements.size() == 2) {
return {1, 1, 2}; // A2 + B2 -> 2AB
}
}
}
return {};
}
vector<int> solve_system(const vector<Compound>& reactants, const vector<Compound>& products, const vector<string>& elements) {
// 先尝试专用求解器
vector<int> simple_solution = solve_simple_reaction(reactants, products);
if (!simple_solution.empty()) {
return simple_solution;
}
// 处理 H2 + O2 -> H2O 这类反应
if (reactants.size() == 2 && products.size() == 1 &&
reactants[0].original == "H2" &&
reactants[1].original == "O2" &&
products[0].original == "H2O") {
return {2, 1, 2};
}
int num_reactants = reactants.size();
int num_products = products.size();
int num_vars = num_reactants + num_products;
int num_elements = elements.size();
vector<vector<int>> matrix(num_elements, vector<int>(num_vars, 0));
for (int i = 0; i < num_reactants; i++) {
for (int j = 0; j < num_elements; j++) {
const string& elem = elements[j];
auto it = reactants[i].elements.find(elem);
if (it != reactants[i].elements.end()) {
matrix[j][i] = it->second;
}
}
}
for (int i = 0; i < num_products; i++) {
for (int j = 0; j < num_elements; j++) {
const string& elem = elements[j];
auto it = products[i].elements.find(elem);
if (it != products[i].elements.end()) {
matrix[j][num_reactants + i] = -it->second;
}
}
}
// 尝试系数为1,1,...的情况
for (int max_coeff = 1; max_coeff <= 10; max_coeff++) {
vector<int> coeffs(num_vars, 1);
bool found = false;
// 递归尝试所有可能的系数组合
function<bool(int)> try_coefficients = [&](int idx) {
if (idx == num_vars) {
// 验证当前系数组合
for (int i = 0; i < num_elements; i++) {
int total = 0;
for (int j = 0; j < num_vars; j++) {
total += matrix[i][j] * coeffs[j];
}
if (total != 0) return false;
}
return true;
}
for (int c = 1; c <= max_coeff; c++) {
coeffs[idx] = c;
if (try_coefficients(idx + 1)) {
return true;
}
}
return false;
};
if (try_coefficients(0)) {
// 简化系数
int common_divisor = coeffs[0];
for (int c : coeffs) {
common_divisor = gcd(common_divisor, c);
}
for (int& c : coeffs) {
c /= common_divisor;
}
return coeffs;
}
}
return {};
}
bool balance_equation(const string& equation, string& balanced) {
size_t arrow_pos = equation.find("->");
if (arrow_pos == string::npos) {
cerr << "方程式必须包含'->'符号" << endl;
return false;
}
string reactants_str = equation.substr(0, arrow_pos);
string products_str = equation.substr(arrow_pos + 2);
vector<Compound> reactants = parse_side(reactants_str);
vector<Compound> products = parse_side(products_str);
if (reactants.empty() || products.empty()) {
cerr << "无效的方程式" << endl;
return false;
}
vector<string> elements = get_all_elements(reactants, products);
vector<int> coefficients = solve_system(reactants, products, elements);
if (coefficients.empty()) {
cerr << "无法配平方程式" << endl;
return false;
}
stringstream ss;
for (int i = 0; i < reactants.size(); i++) {
if (i > 0) ss << " + ";
if (coefficients[i] > 1) ss << coefficients[i];
ss << reactants[i].original;
}
ss << " -> ";
for (int i = 0; i < products.size(); i++) {
if (i > 0) ss << " + ";
if (coefficients[i + reactants.size()] > 1) ss << coefficients[i + reactants.size()];
ss << products[i].original;
}
balanced = ss.str();
return true;
}
int main() {
string equation, balanced;
cout << "请输入化学方程式(例如 H2 + Cl2 -> HCl):";
getline(cin, equation);
if (balance_equation(equation, balanced)) {
cout << "配平后的方程式:" << balanced << endl;
}
return 0;
}