化学-模拟题(原题Codeforces Gym-270437A)

问题描述

\hspace{17pt} 化学很神奇,以下是烷烃基。
在这里插入图片描述
\hspace{17pt} 假设如上图,这个烷烃基有6个原子和5个化学键,6个原子分别标号1~6,然后用一对数字 a,b 表示原子a和原子b间有一个化学键。这样通过5行a,b可以描述一个烷烃基。
\hspace{17pt} 你的任务是甄别烷烃基的类别。
\hspace{17pt} 原子没有编号方法,比如:
\hspace{17pt} 1 2
\hspace{17pt} 2 3
\hspace{17pt} 3 4
\hspace{17pt} 4 5
\hspace{17pt} 5 6
\hspace{17pt}
\hspace{17pt} 1 3
\hspace{17pt} 2 3
\hspace{17pt} 2 4
\hspace{17pt} 4 5
\hspace{17pt} 5 6
\hspace{17pt} 是同一种,本质上就是一条链,编号其实是没有关系的,可以在纸上画画就懂了。

Input

\hspace{17pt} 输入第一行为数据的组数T(1≤T≤200000)。每组数据有5行,每行是两个整数a, b(1≤a,b≤6,a ≤b)。
\hspace{17pt} 数据保证,输入的烷烃基是以上5种之一。

Output

\hspace{17pt} 每组数据,输出一行,代表烷烃基的英文名。

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

解题思路

我的做法

\hspace{17pt} 这个题是一个找规律的题,观察上面五种烷烃的不同,无非就是每个原子度数的不同。
n − h e x a n e \hspace{17pt}n-hexane nhexane有两个一度原子,四个二度原子。
2 , 3 − d i m e t h y l b u t a n e \hspace{17pt}2,3-dimethylbutane 2,3dimethylbutane有四个一度原子,两个三度原子。
2 , 2 − d i m e t h y l b u t a n e \hspace{17pt}2,2-dimethylbutane 2,2dimethylbutane有四个一度原子,一个两度原子,一个四度原子。
2 − m e t h y l p e n t a n e \hspace{17pt}2-methylpentane 2methylpentane 3 − m e t h y l p e n t a n e 3-methylpentane 3methylpentane都是有三个一度原子,两个二度原子,一个三度原子。但是 2 − m e t h y l p e n t a n e 2-methylpentane 2methylpentane的三度原子和两个一度原子相邻,而 3 − m e t h y l p e n t a n e 3-methylpentane 3methylpentane的三度原子和一个一度原子相邻。
\hspace{17pt} 代码中,使用num[x]表示x号原子的度数是num[x],ans[y]表示度数为y的原子出现了ans[y]次,代码如下:

if(ans[1]==2 && ans[2]==4) cout<<"n-hexane"<<endl;
else if(ans[4]==1) cout<<"2,2-dimethylbutane"<<endl;
else if(ans[3]==2) cout<<"2,3-dimethylbutane"<<endl;
else {
    int flag=0;
    for (int j=1; j<=6; j++){
        if((num[a[j]]==1 && num[b[j]]==3) || (num[a[j]]==3 && num[b[j]]==1))
            flag++;
    }
    if(flag==2) cout<<"2-methylpentane"<<endl;
    else cout<<"3-methylpentane"<<endl;
}
学长做法

\hspace{17pt} 利用deg[x][y]表示有几对度数为x的点与度数为y的点相连,这样代码就非常精简。代码如下:

if(deg[4][1]==3) printf("2,2-dimethylbutane\n");
else if(deg[3][3]==2) printf("2,3-dimethylbutane\n");
else if(deg[3][2]==2) printf("3-methylpentane\n");
else if(deg[3][2]==1) printf("2-methylpentane\n");
else printf("n-hexane\n");

完整代码

//#pragma GCC optimize(2)//比赛禁止使用!
//#pragma G++ optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;


int getint()
{
    int x=0,s=1;
    char ch=' ';
    while(ch<'0' || ch>'9')
    {
        ch=getchar();
        if(ch=='-') s=-1;
    }
    while(ch>='0' && ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*s;
}
const int maxn=100;
int num[maxn];//统计各个数据出现的次数
int ans[maxn];//统计出现次数的次数
int a[maxn],b[maxn];
int main()
{
//    ios::sync_with_stdio(false);
//    cin.tie(0);

    int T;
    cin>>T;
    for (int i=1; i<=T; i++)
    {
        for (int j=1; j<=6; j++)
        {
            num[j]=0; ans[j]=0;
        }
        for (int j=1; j<=5; j++)
        {
            cin>>a[j]>>b[j];
            num[a[j]]++;
            num[b[j]]++;
        }
        for (int j=1; j<=6; j++)
            ans[num[j]]++;
        if(ans[1]==2 && ans[2]==4) cout<<"n-hexane"<<endl;
        else if(ans[4]==1) cout<<"2,2-dimethylbutane"<<endl;
        else if(ans[3]==2) cout<<"2,3-dimethylbutane"<<endl;
        else {
            int flag=0;
            for (int j=1; j<=6; j++){
                if((num[a[j]]==1 && num[b[j]]==3) || (num[a[j]]==3 && num[b[j]]==1))
                    flag++;
            }
            if(flag==2) cout<<"2-methylpentane"<<endl;
            else cout<<"3-methylpentane"<<endl;
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值