本文主要是介绍【化学方程式配平 / 3】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目
代码
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
unordered_map<string, int> e;
int eidx, midx; //eidx 元素数, midx 物质数
double matrix[45][45];
int q;
bool check_alpha(char c)
{if(c >= 'a' && c <= 'z') return true;return false;
}
bool gauss(int n, int m)
{//if(n < m) return false;int c, r, t;for(c = 1, r = 1; c <= m; c++){t = r;for(int i = r+1; i <= n; i++) //找主元{if(fabs(matrix[i][c]) > fabs(matrix[t][c])) t = i;}if(fabs(matrix[t][c]) < eps) continue;for(int i = c; i <= m+1; i++) //交换{swap(matrix[r][i], matrix[t][i]);}for(int i = m+1; i >= c; i--) //归一化{matrix[r][i] /= matrix[r][c];}for(int i = r+1; i <= n; i++) //上三角化{for(int j = m+1; j >= c; j--){matrix[i][j] -= matrix[i][c] * matrix[r][j];}}r++;}if((r-1) < m) return true;else return false;
}
int main()
{cin >> q;while (q -- ){memset(matrix, 0, sizeof matrix);e.clear();eidx = 0;cin >> midx;for(int i = 1; i <= midx; i++){string s;cin >> s;string name = "";for(int j = 0; s[j] != '\0'; j++){if(!check_alpha(s[j])){string num = "";num += s[j];while(s[++j] != '\0' && !check_alpha(s[j])){num += s[j];}j--;if(!e.count(name)) e[name] = ++eidx;matrix[e[name]][i] = stoi(num);name = "";}else name += s[j];}}if(gauss(eidx, midx)) cout << "Y" << endl;else cout << "N" << endl;}
}
这篇关于【化学方程式配平 / 3】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!