zoukankan      html  css  js  c++  java
  • IEEEXtreme 10.0

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解

    Xtreme 10.0 - Counting Molecules

    题目来源 第10届IEEE极限编程大赛

    https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/counting-molecules

    Your task is to count the number of molecules in a cup of soda which contains distilled watercarbon dioxide, andglucose. You have a machine that counts the number of atoms of carbonhydrogen, and oxygen in a given sample.

    Input Format

    The input consists of a single line with three space separated integers: ch, and o

    where

    c is the count of carbon atoms

    h is the count of hydrogen atoms

    o is the count of oxygen atoms

    Constraints

    0 ≤ cho < 1010

    Output Format

    If the number of atoms is consistent with a mixture containing only water, carbon dioxide, and glucose molecules, the output should consist of a single line containing three space separated integers: the number of water molecules, the number of carbon dioxide molecules, and the number of glucose molecules.

    If the number of atoms is not consistent with a mixture containing only water, carbon dioxide, and glucose molecules, the output should consist of a line containing the word Error

    Sample Input

    10 0 20
    

    Sample Output

    0 10 0
    

    Explanation

    The input indicates that there are 10 carbon atoms and 20 oxygen atoms. The only way that this could occur would be if there were 0 water molecules, 10 carbon dioxide molecules, and 0 glucose molecules.

    Note that there are additional sample inputs available if you click on the Run Code button.

    题目解析

    这题就是求解一个三元方程组。用矩阵的形式可以表示成下面的样子:

    三个未知数,三个方程。同时三个方程线性无关,有唯一解。

    由于物质的个数是非负整数,约束方程组的解是非负整数。

    判断一个分数是否是一个整数,有以下两种办法

    • 判断 分母 % 分子 == 0
    • 用浮点数的除法,然后取整,判断是否相等

    程序

    C++

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    int main() {
        long long c, h, o;
        cin >> c >> h >> o;
        if((-4*c+h+2*o)>=0 && (-4*c+h+2*o)%4==0 &&
          (-h+2*o)>=0 && (-h+2*o)%4==0 &&
          (4*c+h-2*o)>=0 && (4*c+h-2*o)%24==0) {
            printf("%lld %lld %lld", (-4*c+h+2*o)/4, (-h+2*o)/4, (4*c+h-2*o)/24);
        }
        else {
            printf("Error");
        }
        return 0;
    }

     Python2

    x, y, z = map(int, raw_input().split())
    a = ((2 * z) - (4 * x) + y) / 4.0
    b = ((2 * z) - y) / 4.0
    c = (x - b) / 6.0
    
    if a != a // 1 or a < 0:
        print "Error"
    elif b != b // 1 or b < 0:
        print "Error"
    elif c != c // 1 or c < 0:
        print "Error"
    else:
        print int(round(a)),int(round(b)),int(round(c))

    from: hackerranksolutionsforprogrammers.blogspot.com/2016/10/counting-molecules-by-ieeextreme.html

    博客中的文章均为 meelo 原创,请务必以链接形式注明 本文地址

  • 相关阅读:
    shell脚本积累
    while,shift,until,case
    条件测试命令,if命令,双圆括号,双中括号
    seq命令,tr命令,sort命令,cut命令
    正则,grep命令详解
    Ansible实现批量管理服务器
    实时同步服务知识梳理
    RHEL7破解密码操作步骤
    运维核心基础知识之——MD5sum校验文件
    Linux运维基础提高之RAID卡和磁盘分区
  • 原文地址:https://www.cnblogs.com/meelo/p/6083262.html
Copyright © 2011-2022 走看看