这是 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. Java 8十个lambda表达式案例【转】

    1. 实现Runnable线程案例 使用() -> {} 替代匿名类: //Before Java 8: new Thread(new Runnable() { @Override public ...

  2. HDU--2363

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2363 分析:最短路+二分. #include<iostream> #include< ...

  3. 安装黑苹果的config.plist

    前提条件:有mac真机.目前在测试虚拟机可行性 第一步:制作U盘启动盘 1.在 app store 下载 mac OS sierra 镜像 2.格式化 U 盘,gpt 格式 3.执行以下命令(具体名称 ...

  4. 「Python」python日期与字符串互转

    1str -> date import datetime detester = ‘2017-01-01' date = datetime.datetime.strptime(detester,’ ...

  5. myeclipse注册机,自己生成注册码

    话不多少,直接上代码 直接可以运行,自己输入key即可 package com.generate.myeclipse.code; import java.io.BufferedReader; impo ...

  6. jQuery中deferred的对象使用

    什么是deferred对象 开发网站的过程中,我们经常遇到某些耗时很长的javascript操作.其中,既有异步的操作(比如ajax读取服务器数据),也有同步的操作(比如遍历一个大型数组),它们都不是 ...

  7. 【机器学习】K-邻近算法的python 实现

    #!/usr/bin/python # -*- coding: utf-8 -*- from numpy import * import operator def createDataSet(): ' ...

  8. GridControl详解(七)事件

    private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventA ...

  9. 你必须了解Spring的生态

    Spring不止是提供了IOC.AOP的功能,还提供了大量的基于Spring的项目,拿来用就行了,用于一站式开发,大大降低了开发的难度. 下面列举下主要的一些Spring的生态项目: Spring B ...

  10. java规范之checkstyle

    1. 概述 随着中心的代码规范的建立和实施,项目组对代码规范要求,以及软件工程师们对自身代码的编写规范重要性的认知,“代码规范”已经成为了中心的一个“热词”.然后怎么才能写出有规范的代码,怎么才能养成 ...