Xtreme 10.0 - Counting Molecules

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

题目来源 第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 water, carbon dioxide, andglucose. You have a machine that counts the number of atoms of carbon, hydrogen, and oxygen in a given sample.

Input Format

The input consists of a single line with three space separated integers: c, h, 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 ≤ c, h, o < 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.

题目解析
这题就是求解一个三元方程组。


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

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

程序
C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    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;
}

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 苏盈袖阅读 1,549评论 0 0
  • 睁大曈仁 盯着臆想中的囊中之物 静谧的背后 悬着一面巨大的锣鼓 喧天几声 慌乱了暗夜魅影 仓促之际 逃进一间没有门...
    澄默时节阅读 2,241评论 9 20

友情链接更多精彩内容