WEEK2实验 A化学

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值