zzu--2014年11月16日月潭赛 B称号
1229: Rational Resistance
Time Limit: 1 Sec Memory Limit: 128 MB id=1229" style="color:rgb(26,92,200); text-decoration:none">Submit
Submit: 8 Solved: 4
[
Board]
Description
Mad scientist Mike is building a time machine in his spare time. To finish the work, he needs a resistor with a certain resistance value.
However, all Mike has is lots of identical resistors with unit resistance R0 = 1.
Elements with other resistance can be constructed from these resistors. In this problem, we will consider the following as elements:
- one resistor;
- an element and one resistor plugged in sequence;
- an element and one resistor plugged in parallel.

With the consecutive connection the resistance of the new element equals R = Re + R0. With the parallel connection the resistance of the new element equals
.
In this case Reequals the resistance of the element being connected.
Mike needs to assemble an element with a resistance equal to the fraction
.
Determine the smallest possible number of resistors he needs to make such an element.
Input
The single input line contains two space-separated integers a and b
(1 <= a, b <= 1^18). It is guaranteed that the fraction
is
irreducible. It is guaranteed that a solution always exists.
Output
Print a single number — the answer to the problem.
Please do not use the %I64d specifier to read or write 64-bit integers in С++. It is recommended to use the cin, cout streams
or the%lld specifier.
Sample Input
Sample Output
HINT
In the first sample, one resistor is enough.
In the second sample one can connect the resistors in parallel, take the resulting element and connect it to a third resistor consecutively. Then, we get an element with resistance
.
We cannot make this element using two resistors.
Source
题意:就是去找最少要多少个电阻能够达成功能。
思路:假设 a/b > 1 则 a/b 整数部分能够加到ans。a再模b。 假设a == b 则ans += a , a变为0; 假设 a/b < 1 则b/a 整数部分能够加到ans。b再模a
比如:3/5 要先变为1 / ( 2 / 3 +1 ) ,ans=1。然后变为 1 / ( 1 / 2 + 1 ) 。 ans=2,最后1 /(1 + 1),ans =4
AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std; int main()
{
ll a, b;
while(scanf("%lld %lld", &a, &b)!=EOF)
{
ll ans = 0;
while(a)
{
if(a > b)
{
ll t = a / b;
ans+=t;
a=a%b;
}
else if(a == b)
{
ans+=a;
a=0;
}
else if(a < b)
{
ll t = b / a;
ans+=t;
t = b;
b = a;
a=t%a;
}
}
printf("%lld\n", ans);
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
zzu--2014年11月16日月潭赛 B称号的更多相关文章
- zzu--2014年11月16日月潭赛 C称号
1230: Magnets Time Limit: 1 Sec Memory Limit: 128 MB Submit: 24 Solved: 13 [id=1230" style=&q ...
- 11月16日《奥威Power-BI基于SQL的存储过程及自定义SQL脚本制作报表》腾讯课堂开课啦
上周的课程<奥威Power-BI vs微软Power BI>带同学们全面认识了两个Power-BI的使用情况,同学们已经迫不及待想知道这周的学习内容了吧!这周的课程关键词—— ...
- 2016年11月16日 星期三 --出埃及记 Exodus 20:7
2016年11月16日 星期三 --出埃及记 Exodus 20:7 "You shall not misuse the name of the LORD your God, for the ...
- CSDN下载频道2014年11月4日本-5日常维护公告
尊敬的用户: CSDN于2005年推出了下载服务.经过数年的发展.下载频道的用户已经为无数用户提供了帮助,分享500万的技术资源. CSDN下载频道将于2014年11月4日23点至11月5日8点进行积 ...
- 2014年11月17~11月18日,杨学明老师《企业IT需求收集和实施》内训在湖南长沙某酒店成功举办!
2014年11月17至18日,受湖南某软件企业的邀请,杨学明老师<企业IT需求收集和实施>内训在某长沙某五星级酒店成功举办!来自全国各地的IT高管和企业负责人参加了此次培训.杨学明老师分别 ...
- 2014年5月16至24日,杨学明老师为深圳创维RGB事业部提供两天的《软件测试管理》内训服务!
2014年5月16日和24日,<在软件开发流程中构筑软件质量—软件测试管理>内训课程在深圳创维集团成功举办!来自创维研发.测试.生产等部门的管理人员和核心骨干等参加了此次培训,此次培训由研 ...
- 北京Uber优步司机奖励政策(11月16日~11月22日)
用户组:人民优步“关羽组”(适用于11月16日-11月22日)奖励政策: 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/ ...
- OpenShare新功能@2014年11月 [2014-11-30]
新功能@Nov,2014 各门户层次主页布局自由编辑:升级为实时保存,这样用户编辑门户主页时,就算忘了点击“保存布局信息”,也不会丢失已做的工作 全方位支持匿名访问,支持构造公网以及extranet网 ...
- 2014年10月16号--for语句实例
Console.WriteLine("一对小兔一个月之后长成大兔,再过一个月后生新的一对兔子,且两年之后有多少对兔子,就是三兔子幼兔,小兔,成兔"); Console.WriteL ...
随机推荐
- 【linux】 Makefile之make menuconfig /uImage
欢迎转载,转载时请保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http: ...
- linux环境 :Linux 共享库LIBRARY_PATH, LD_LIBRARY_PATH 与ld.so.conf
参考: 1. Linux 共享库:LD_LIBRARY_PATH 与ld.so.conf Linux环境变量名,该环境变量主要用于指定查找共享库(动态链接库)时除了默认路径之外的其他路径.(该路径在默 ...
- 测试 __try, __finally, __except(被__finally捕获的异常, 还会被上一级的__except捕获。反之不行)
C语言标准是没有 try-catch语法 的, M$家自己提供了一组. /// @file ClassroomExamples.c /// @brief 验证C语言的非标准try, catch #in ...
- wince平台下使用log4net记录日志
前面我写过一篇文章是来介绍windows平台下开发软件使用log4net来记录日志的,详情请参考:用一个简单的示例说明日志记录组件log4net的使用. 我们在wince平台下开发程序也一样需要日志文 ...
- 基于visual Studio2013解决C语言竞赛题之1053洗牌
题目 解决代码及点评 /* 功能:洗扑克牌.将54张牌分别编号为1,2,-,54号,并放在数组M中. 洗牌方法如下:产生[1,54]区间内的一个随机数K,将M[1]与M[K]交换: ...
- Oracle Data Guard 创建物理Standby数据库
创建物理备库 机器名 a1 a2 IP: 192 ...
- unix domain IPC 进程间通信简析
Linux系统有多种进程间通信方式,如信号.消息队列.管道等,socket是其中一种,socket使用unix domain 模式进行进程间通信 //服务端代码 #include <stdio. ...
- HTML5 Canvas中9宫格的坑
近期小鸟情人游戏上了手机qq空间,一个3岁的游戏来了她的第二春.为了能有更好的表现,我们对其进行了一次改版. 改版当中一项就是对原来的弹出框样式进行改进.将大块木板材质改成纯色(边框为圆角金属材质)样 ...
- 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters
题目 Given a string, find the length of the longest substring without repeating characters. For exampl ...
- Boost Thread学习笔记
thread自然是boost::thread库的主 角,但thread类的实现总体上是比较简单的,前面已经说过,thread只是一个跨平台的线程封装库,其中按照所使用的编译选项的不同,分别决定使用 W ...