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. XP win2003系统 微软雅黑字体的使用方法

    微软雅黑是微软公司为其新一代操作系统Vista开发的中文字体,据说它将是迄今为止个人电脑上可以显示的最清晰的中文字体.       微软公司表示,在新一代操作系统中为了能够更加清晰的显示文字,目前正在 ...

  2. linux中的系统服务--daemon

    简单的说,系统为了某些功能必须要提供一些服务 (不论是系统本身还是网络方面),这个服务就称为 service . 但是 service 的提供总是需要程序的运行吧!否则如何运行呢?所以达成这个 ser ...

  3. 动态加载图片的Adapter

    package com.example.cc.ecustapp.Adapter; import android.app.Activity;import android.content.Context; ...

  4. deepin linux下markdown实时预览

    # deepin linux下markdown实时预览 ## 参考文章------------------------------ [vim安装markdown插件](http://www.jians ...

  5. 关于dbutils中QueryRunner看批量删除语句batch

    //批量删除 public void delBooks(String[] ids) throws SQLException { QueryRunner qr = new QueryRunner(C3P ...

  6. c# 获取Excel内容的分析

    现在主流的Excel文档有2003和2007 c#获取 Excel2003 连接字符串 string strConn = "Provider=Microsoft.Jet.OLEDB.4.0; ...

  7. 修改linux系统的时间EDT为CST

    今早看到一台机器时间对不上,本以为系统时间与网络北京时间不同步,就在终端命令执行网络时间同步 [root@localhost ~]# ntpdate time.windows.com 执行完之后,在执 ...

  8. Tuning 04 Sizing the Buffer Cache

    Buffer Cache 特性 The buffer cache holds copies of the data blocks from the data files. Because the bu ...

  9. 在java中,List是个接口,那实现List接口的类有哪些,有什么区别?

    在java中,List是个接口,那实现List接口的类有哪些,有什么区别? 解答: ArrayList是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引 ...

  10. Java反射机制的作用?

    Java反射机制的作用? 解答:Java反射机制的作用是: 1)在运行时判断任意一个对象所属的类. 2)在运行时构造任意一个类的对象. 3)在运行时判断任意一个类所具有的成员变量和方法. 4)在运行时 ...