IEEEXtreme 10.0 - Counting Molecules
这是 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 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.
题目解析
这题就是求解一个三元方程组。用矩阵的形式可以表示成下面的样子:

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

由于物质的个数是非负整数,约束方程组的解是非负整数。
判断一个分数是否是一个整数,有以下两种办法
- 判断 分母 % 分子 == 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的更多相关文章
- IEEEXtreme 10.0 - Inti Sets
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Inti Sets 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...
- IEEEXtreme 10.0 - Painter's Dilemma
这是 meelo 原创的 IEEEXtreme极限编程比赛题解 Xtreme 10.0 - Painter's Dilemma 题目来源 第10届IEEE极限编程大赛 https://www.hack ...
- IEEEXtreme 10.0 - Ellipse Art
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Ellipse Art 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank ...
- IEEEXtreme 10.0 - Checkers Challenge
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Checkers Challenge 题目来源 第10届IEEE极限编程大赛 https://www.hac ...
- IEEEXtreme 10.0 - Game of Stones
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Game of Stones 题目来源 第10届IEEE极限编程大赛 https://www.hackerr ...
- IEEEXtreme 10.0 - Playing 20 Questions with an Unreliable Friend
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Playing 20 Questions with an Unreliable Friend 题目来源 第1 ...
- IEEEXtreme 10.0 - Full Adder
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Full Adder 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank. ...
- IEEEXtreme 10.0 - N-Palindromes
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - N-Palindromes 题目来源 第10届IEEE极限编程大赛 https://www.hackerra ...
- IEEEXtreme 10.0 - Mysterious Maze
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Mysterious Maze 题目来源 第10届IEEE极限编程大赛 https://www.hacker ...
随机推荐
- C/C++中如何计算程序运行的时间
一个程序的功能通常有很多种方法来实现,怎么样的程序才算得上最优呢?举个例子,如果实现同一个功能的两个程序,一个一点按钮就给出运行结果,而另一个则需要漫长的时间去等待,就像安装WINDOWS XP一样( ...
- 省选模拟赛 LYK loves graph(graph)
题目描述 LYK喜欢花花绿绿的图片,有一天它得到了一张彩色图片,这张图片可以看做是一张n*m的网格图,每个格子都有一种颜色去染着,我们用-1至n*m-1来表示一个格子的颜色.特别地,-1代表这个颜色是 ...
- bash shell输出颜色
http://note.youdao.com/noteshare?id=9de6d5fac6380447df112dff5bc404a1
- 前端PHP入门-013-变量作用域
目前,我们知道了几个不同的规矩: 函数定义时后括号里面接的变量是形式上的参数(形参),与函数体外的变量没有任何关系.仅仅是在函数内部执行 函数内声明的变量也与函数外的变量没关系. 但是,我们实际的处理 ...
- 「LibreOJ β Round #4」多项式 (广义欧拉数论定理)
https://loj.ac/problem/525 题目描述 给定一个正整数 kkk,你需要寻找一个系数均为 0 到 k−1之间的非零多项式 f(x),满足对于任意整数 x 均有 f(x)modk= ...
- 树dp...吧 ZOJ 3949
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5568 Edge to the Root Time Limit: 1 Secon ...
- 莫队+分块 BZOJ 3809
3809: Gty的二逼妹子序列 Time Limit: 80 Sec Memory Limit: 28 MBSubmit: 1634 Solved: 482[Submit][Status][Di ...
- 复制自身程序到windows目录和system32目录下
功能:复制自身到windows目录和system32目录下. 参考代码: #include <stdio.h> #include <windows.h> void CopySe ...
- 你知道吗?31种 CSS 选择器的应用
选择器(selector)是CSS中很重要的概念,所有HTML语言中的标记都是通过不同的CSS选择器进行控制的.用户只需要通过选择器对不同的HTML标签进行控制,并赋予各种样式声明,即可实现各种效果. ...
- ASP.Net中自定义Http处理及应用之HttpModule篇
HttpHandler实现了类似于ISAPI Extention的功能,他处理请求(Request)的信息和发送响应(Response).HttpHandler功能的实现通过实现IHttpHandle ...