AOJ0005

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0005

题意

给定两个数,求其最大公约数GCD以及最小公倍数LCM。

思路

求最大公约数一般用辗转相除法,然后就得到了最小公倍数。

更详细的分析参见我的博客文章:

数论——最大公约数和最小公倍数算法

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; unsigned GCD(unsigned a, unsigned b)
{
if (!b) return a;
return GCD(b, a%b);
} unsigned LCM(unsigned a, unsigned b)
{
return a / GCD(b, a-b) * b;
} int main(void)
{
unsigned a, b;
while (scanf("%u %u", &a, &b) != EOF) {
if (a < b) swap(a, b);
printf("%u %u\n", GCD(a, b), LCM(a, b));
} return 0;
}

POJ2429

POJ1930

http://poj.org/problem?id=1930

题意

将一个无限循环小数转化成分母最小的精确分数值。

注意:循环的部分不一定是最后一位,有可能从小数点后面全是循环部分。

思路

将分数分成循环的部分和非循环的部分:

设分数为0.i1 i2 i3 i4 .. ik j1 j2 j3 .. jc,其中i1 ~ ik 为非循环的部分,j1 ~ jc为循环部分。

非循环的部分可以拆成 b / a 其中 b = ( i1…ik) a = 10 ^ (k);

循环的部分可以拆成 bb / aa 其中 bb = (j1 .. jc) aa = 10 ^ (k + c) - 10 ^ ( k);

则所求分数为 b / a + bb / aa,通分得 (b * aa + bb * a) / a * aa,约分得答案(用最大公约数求)。

另外,据说数据会有全是0的坑爹数据,但我没有被坑到。

代码

Source Code

Problem: 1930       User: liangrx06
Memory: 172K Time: 16MS
Language: C++ Result: Accepted
Source Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <algorithm>
using namespace std; int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a%b);
} int exp10(int x)
{
int res = 1;
while (x--)
res *= 10;
return res;
} int main(void)
{
int i, j, a, b, a10, b10, g, len;
int m, n;
string s, sa, sb; while(cin >> s)
{
if(s == "0")
break; len = s.size();
for(i = 1; i <= len-5; i++) {
sa = s.substr(len-3-i, i);
a = atoi(sa.c_str());
a10 = exp10(i); j = len-5-i;
sb = s.substr(2, j);
b = atoi(sb.c_str());
b10 = exp10(j); int tn = (a10-1)*b10;
int tm = b*(a10-1) + a;
g = gcd(tn, tm);
tn /= g;
tm /= g;
if (i == 1 || i > 1 && tn < n) {
n = tn;
m = tm;
}
}
printf("%d/%d\n", m, n);
} return 0;
}

《挑战程序设计竞赛》2.6 数学问题-辗转相除法 AOJ0005 POJ2429 1930(1)的更多相关文章

  1. Aizu 2249Road Construction 单源最短路变形《挑战程序设计竞赛》模板题

    King Mercer is the king of ACM kingdom. There are one capital and some cities in his kingdom. Amazin ...

  2. 《挑战程序设计竞赛》2.3 动态规划-优化递推 POJ1742 3046 3181

    POJ1742 http://poj.org/problem?id=1742 题意 有n种面额的硬币,面额个数分别为Ai.Ci,求最多能搭配出几种不超过m的金额? 思路 据说这是传说中的男人8题呢,对 ...

  3. 挑战程序设计竞赛》P345 观看计划

                                                 <挑战程序设计竞赛>P345 观看计划 题意:一周一共有M个单位的时间.一共有N部动画在每周si时 ...

  4. POJ 2386 Lake Counting 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2386 <挑战程序设计竞赛>习题 题目描述Description Due to recent rains, water has ...

  5. poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...

  6. 《挑战程序设计竞赛》2.6 数学问题-快速幂运算 POJ1995

    POJ3641 此题应归类为素数. POJ1995 http://poj.org/problem?id=1995 题意 求(A1^B1+A2^B2+ - +AH^BH)mod M. 思路 标准快速幂运 ...

  7. 《挑战程序设计竞赛》2.6 数学问题-素数 AOJ0009 POJ3126 3421 3292 3641

    AOJ0009 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0009 题意 求不大于n的素数个数. 思路 素数筛法可解,筛法过程中 ...

  8. 《挑战程序设计竞赛》 4.1.1 矩阵 P286

    想写几篇挑战的感悟,也有助于自己理解这本书.但这上面大多贴的是书上的代码,主要是为了用的时候后直接复制就好了,这样就很方便了,就相当于黑盒模板了. 1.线性方程组 /** \brief 高斯消元法 * ...

  9. poj1182食物链_并查集_挑战程序设计竞赛例题

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65534   Accepted: 19321 Description ...

随机推荐

  1. sklearn 随机森林方法

    Notes The default values for the parameters controlling the size of the trees (e.g. max_depth, min_s ...

  2. 在同一台服务器上启动多个 FreeSWITCH 实例

    有时候,需要用到多个FreeSWITCH进行测试,或者需要在一台服务器上部署多个“不兼容”的系统.我们在这一节探讨一下怎么做. 背景故事 几年前我还在Idapted工作的时候,由于需要连接Skype及 ...

  3. AlarmManager研究

    1.概述 在Android系统中,闹钟和唤醒功能都是由Alarm Manager Service控制并管理的.我们所熟悉的RTC闹钟以及定时器都和它有莫大的关系.为了便于称呼,我常常也把这个servi ...

  4. python学习之split()

    定义: Python split()通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串 语法: str.split(str="", num=st ...

  5. Windows/OS X下制作Mac安装U盘

    Windows下制作: 方法一:(适用于OSX 10.9以前) 前期准备:一台windows电脑 UltraISO软件 Mac系统镜像dmg(这里使用Mac os x 10.8.4) 至少8GB的U盘 ...

  6. 如何查看iis的连接数量

    引用:http://jingyan.baidu.com/article/54b6b9c0f3c2002d583b470d.html 运行,输入,perfmon.msc.   在系统监视器,区域点击,添 ...

  7. ajax 传递参数中文乱码解决办法

    /********Start***********/ /*获取地址栏参数*/ function getRequest(){ var url = location.search; //获取url中&qu ...

  8. centos 6.5 安装mysql 5.7.21 community

    Step1: 检测系统是否自带安装mysql # yum list installed | grep mysql Step2: 删除系统自带的mysql及其依赖命令: # yum -y remove ...

  9. 基于Spring框架的简单多数据源切换解决办法

    基于Spring框架的简单多数据源切换解决办法 Spring框架JDBC包提供了一个抽象类AbstractRoutingDataSource提供了动态切换数据库的基础方法.我们仅仅需要实现一个简单的数 ...

  10. curl myip.ipip.net curl ip.cn curl cip.cc

    [命令行] curl查询公网出口IP 2016年07月22日 14:27:02 阅读数:19022 不管是在家里还是办公室,或者是公司的主机,很多时候都是在内网中,也就是说很多都是通过 NAT上网的, ...