题目叙述:
化学很神奇,以下是烷烃基。
假设如上图,这个烷烃基有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每组数据,输出一行,代表烷烃基的英文名Example
Input
2
1 2
2 3
3 4
4 5
5 6
1 4
2 3
3 4
4 5
5 6
Output
n-hexane
3-methylpentane
解题思路:
题目中说与碳键边的编号无关,故可以从C原子连接键的数量下手:
1、n-hexane:四个C原子连接两个键
2、2,3-dimethybutane:两个C原子连接三个键,0个C原子连接2个键
3、2,2-dimethybutane:一个C原子连接四个键
4、2-methylpentane、3-methylpentane:两个C原子连接两个键,一个C原子连接三个键:
对于第四种情况,分析连接三个键的C原子,可以发现对于2-methylpentane,周围的C原子连接键数量总和是4,而对于3-methylpentane,其周围的C原子连接键数量总和是5。
因此,首先通过一个二维矩阵将输入键的左右两个顶点保存下来,并利用数组记录每一个点连接的键的数量。遍历数组,通过C原子连接键的数量进行上述分类,当是第四种情况时,遍历矩阵中对应有三个键的C原子,找到与其相邻的其他C原子,并计算键的数量总和,判断是2-methylpentane还是3-methylpentane。
代码实现:
#include<iostream>
using namespace std;
int main()
{
int x,y,n;
int record[7];
cin>>n;
while(n--)
{
int numf=0,nums=0,numt=0;
int edge[7][7];
for(int i=1;i<7;i++)
for(int j=1;j<7;j++)
edge[i][j]=0;//记录边
for(int i=1;i<=6;i++)
record[i]=0;//记录点
for(int i=0;i<5;i++)
{
cin>>x>>y;
edge[x][y]=1;
edge[y][x]=1;
record[x]++;
record[y]++;
}
int index1=0;
for(int i=1;i<=6;i++)
{
if(record[i]==3)
{
numf++;
for(int j=1;j<=6;j++)
{
if(edge[i][j]==1)
index1+=record[j];
}
}
else if(record[i]==2) nums++;
else if(record[i]==4) numt++;
}
if(numt>0&&nums==1&&numf==0) cout<<"2,2-dimethylbutane"<<endl;
if(nums==4&&numf==0&&numt==0) cout<<"n-hexane"<<endl;
if(nums==2&&numf==1&&numt==0)
{
if(index1==4) cout<<"2-methylpentane"<<endl;
if(index1==5) cout<<"3-methylpentane"<<endl;
}
if(nums==0&&numf==2&&numt==0) cout<<"2,3-dimethylbutane"<<endl;
}
return 0;
}