A-化学
题目描述
化学很神奇,以下是烷烃基。
假设如上图,这个烷烃基有6个原子和5个化学键,6个原子分别标号1~6,然后用一对数字 a,b 表示原子a和原子b间有一个化学键。这样通过5行a,b可以描述一个烷烃基
你的任务是甄别烷烃基的类别。
原子没有编号方法,比如
1 2
2 3
3 4
4 5
5 6
和
1 3
2 3
2 4
4 5
5 6
是同一种,本质上就是一条链,编号其实是没有关系的,可以在纸上画画就懂了
Input
输入第一行为数据的组数T(1≤T≤200000)。每组数据有5行,每行是两个整数a, b(1≤a,b≤6,a ≤b)
数据保证,输入的烷烃基是以上5种之一
Output
每组数据,输出一行,代表烷烃基的英文名
Sample Input
2
1 2
2 3
3 4
4 5
5 6
1 4
2 3
3 4
4 5
5 6
Sample Output
n-hexane
3-methylpentane
题解
- 思路总体上来说是根据键最多的元素链接的键数来判断属于哪一类烷烃
- count数组存放各个元素链接的键数,relation二维数组存放第i个点分别与哪一些点相连
- 循环遍历count数组,找出键最多的元素的键数和第二多的元素的键数,根据这两个数的不同即可判别出三种烷烃;剩下的两种烷烃的count数组完全相同;可根据剩下两种烷烃的区别:一个烷烃的所有的二条键元素都被它的三条键的元素连接着,另一个则没有;循环遍历relation数组查看三条键的元素的邻接元素是否包含了所有的二条键元素
- 注意int型变量和数组初始化时最好赋值
代码
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int* count;
int** relation;
int main(int argc, char** argv) {
int n=0;
cin >> n;
for (int ii = 0; ii < n; ii++) {
int* count;
int** relation;
count = new int[7];
relation = new int* [7];
for (int i = 0; i < 7; i++) {
count[i] = 0;
relation[i] = new int[4];
for(int j=0;j<4;j++){
relation[i][j]=0;
}
}
for (int i = 0; i < 5; i++) {
int a=0, b=0;
cin >> a >> b;
relation[a][count[a]] = b;
relation[b][count[b]] = a;
count[a]++;
count[b]++;
}
int second = 0, first = 0;//键最多的和第二多的
for (int i = 1; i < 7; i++) {
if (count[i] > first) {
second = first;
first = count[i];
}
else if (count[i] <= first && count[i] > second) {
second = count[i];
}
}
if (first == 2 && second == 2) { cout << "n-hexane" << endl; }
else if (first == 4 && second == 2) { cout << "2,2-dimethylbutane" << endl; }
else if (first == 3 && second == 3) { cout << "2,3-dimethylbutane" << endl; }
else {
int three = 0, two1 = 0, two2 = 0;
for (int i = 1; i < 7; i++) {
if (count[i] == 3) { three = i; }
else if (count[i] == 2) {
two2 = two1; two1 = i;
}
}
//cout << three << endl;
bool tw1=0, tw2=0;
for (int i = 0; i < 3; i++) {
if (relation[three][i] == two1) { tw1 = 1; }
else if (relation[three][i] == two2) { tw2 = 1; }
}
if (tw1 && tw2) { cout << "3-methylpentane" << endl; }
else { cout << "2-methylpentane" << endl; }
}
}
return 0;
}