这是 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((-*c+h+*o)>= && (-*c+h+*o)%== &&
(-h+*o)>= && (-h+*o)%== &&
(*c+h-*o)>= && (*c+h-*o)%==) {
printf("%lld %lld %lld", (-*c+h+*o)/, (-h+*o)/, (*c+h-*o)/);
}
else {
printf("Error");
}
return ;
}

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 原创,请务必以链接形式注明 本文地址

IEEEXtreme 10.0 - Counting Molecules的更多相关文章

  1. IEEEXtreme 10.0 - Inti Sets

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Inti Sets 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...

  2. IEEEXtreme 10.0 - Painter's Dilemma

    这是 meelo 原创的 IEEEXtreme极限编程比赛题解 Xtreme 10.0 - Painter's Dilemma 题目来源 第10届IEEE极限编程大赛 https://www.hack ...

  3. IEEEXtreme 10.0 - Ellipse Art

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Ellipse Art 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank ...

  4. IEEEXtreme 10.0 - Checkers Challenge

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Checkers Challenge 题目来源 第10届IEEE极限编程大赛 https://www.hac ...

  5. IEEEXtreme 10.0 - Game of Stones

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Game of Stones 题目来源 第10届IEEE极限编程大赛 https://www.hackerr ...

  6. IEEEXtreme 10.0 - Playing 20 Questions with an Unreliable Friend

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Playing 20 Questions with an Unreliable Friend 题目来源 第1 ...

  7. IEEEXtreme 10.0 - Full Adder

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Full Adder 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank. ...

  8. IEEEXtreme 10.0 - N-Palindromes

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - N-Palindromes 题目来源 第10届IEEE极限编程大赛 https://www.hackerra ...

  9. IEEEXtreme 10.0 - Mysterious Maze

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Mysterious Maze 题目来源 第10届IEEE极限编程大赛 https://www.hacker ...

随机推荐

  1. oracle 数据库记录

    /*----------------------------------------------------------------------------*/ 问题1[--------] Selec ...

  2. 二次封装bootstrap-table及功能优化

    /** * 设置bootstrat-table * @param params */ function setBootstrapTable (target, params) { // 默认设置表格内容 ...

  3. Win8Metro(C#)数字图像处理--2.40二值图像轮廓提取

    http://dongtingyueh.blog.163.com/blog/static/4619453201271481335630/ [函数名称] 二值图像轮廓提取         Contour ...

  4. windows环境利用semophore机制进行线程同步

    semophore是信号量的意思,常用于PV操作,所谓PV操作就是pend(等待,直到有资源可用,并且消耗资源) V就是释放资源. semophore和mutex区别,mutex本意为互斥,用于线程独 ...

  5. 题解【bzoj2440 [中山市选2011]完全平方数】

    Description 求第 \(k\) 个不含平方因子的正整数.多组询问.\(k \leq 10^9, T \leq 50\) Solution 网上的题解几乎都是容斥,这里给一个简单的也挺快的做法 ...

  6. Qt ------ QString 操作

    QStringList QString::arg     ------- 字符串的格式化处理,类始于sprintf 比如:QString("%1").arg(10,2,16,QLa ...

  7. excel自带频率分析

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...

  8. Ubuntu 火狐浏览器中,鼠标选择文字被删除的解决办法

    copy from :http://blog.csdn.net/shadow066/article/details/50628019 在终端中输入命令:ibus-setup 将 “在应用程序窗口中启用 ...

  9. codevs 2796 最小完全图

    2796 最小完全图 http://codevs.cn/problem/2796/  时间限制: 1 s  空间限制: 128000 KB     题目描述 Description 若一个图的每一对不 ...

  10. BZOJ2588:LCA+主席树来实现树上两点之间第K大点权查询

    对于每个节点维护这个节点到根的权值线段树 对于每个询问(x,y),这条路径上的线段树 tree[x]+tree[y]-tree[lca(x,y)]-tree[fa[lca(x,y)]] #includ ...