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 ...
随机推荐
- ibatis中:org.springframework.jdbc.UncategorizedSQLException:异常
SQL 查询语句异常,可能是你的查询语句写错了,或者是你的映射的类和或数据中与表不对应,检查你的映射配置文件.
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) D 构造烟花
D. High Load time limit per test 2 seconds memory limit per test 512 megabytes input standard input ...
- [NOI2010] 能量采集 (数学)
[NOI2010] 能量采集 题目描述 栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量后,栋栋再使用一个能量汇集机器把这些植物采集到的能量汇集到一起. ...
- CSS基础复习
重新撸一遍CSS的基础,因为以前面试的时候被问到,突然发现某些概念搞不清楚,瞬间懵逼了,其实我都知道的,就是因为不会炒概念,导致面试官觉得我很low,你特么连这个都不知道还敢来面试,回家种田去好嘛! ...
- .net core 中 identity server 4 之Topic --定义Client
客户端指能够从id4获取Token的角色. 客户端的共性: a unique client ID a secret if needed the allowed interactions with th ...
- 【BZOJ】1066: [SCOI2007]蜥蜴
[算法]网络流-最大流(dinic) [题解] 构图思路: 因为石柱高度是可以被消耗的,即一根石柱可通过的蜥蜴数量有限,取舍问题中这样表示容量的属性显然可以作为网络流中的边. 于是将一根石柱拆成顶部和 ...
- python学习笔记(十三)之lambda表达式
lambda表达式: 用法 lambda x : 2 * x + 1 其中:前面是参数,后面是返回值. >>> def ds(x): ... return 2 * x + 1 ... ...
- 【leetcode 简单】第三题 回文数
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...
- 关于linux的一些基础知识
一.基础 1.linux所有内容以文件形式保存,包括硬件. 2.linux 不区分扩展名,靠权限区分. #但是,约定 .sh脚本文件 .conf配置文件. 3.-rw-r--r-- ...
- python3学习笔记.2.基础
1.编码 默认编码是 utf-8 # -*- coding: utf-8 -*- 2.注释 单行注释 # 多行注释,用三个单引号或双引号 3.关键字 可在交互窗口查询. >>> i ...