《挑战程序设计竞赛》2.6 数学问题-辗转相除法 AOJ0005 POJ2429 1930(1)
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)的更多相关文章
- Aizu 2249Road Construction 单源最短路变形《挑战程序设计竞赛》模板题
King Mercer is the king of ACM kingdom. There are one capital and some cities in his kingdom. Amazin ...
- 《挑战程序设计竞赛》2.3 动态规划-优化递推 POJ1742 3046 3181
POJ1742 http://poj.org/problem?id=1742 题意 有n种面额的硬币,面额个数分别为Ai.Ci,求最多能搭配出几种不超过m的金额? 思路 据说这是传说中的男人8题呢,对 ...
- 挑战程序设计竞赛》P345 观看计划
<挑战程序设计竞赛>P345 观看计划 题意:一周一共有M个单位的时间.一共有N部动画在每周si时 ...
- POJ 2386 Lake Counting 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2386 <挑战程序设计竞赛>习题 题目描述Description Due to recent rains, water has ...
- poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...
- 《挑战程序设计竞赛》2.6 数学问题-快速幂运算 POJ1995
POJ3641 此题应归类为素数. POJ1995 http://poj.org/problem?id=1995 题意 求(A1^B1+A2^B2+ - +AH^BH)mod M. 思路 标准快速幂运 ...
- 《挑战程序设计竞赛》2.6 数学问题-素数 AOJ0009 POJ3126 3421 3292 3641
AOJ0009 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0009 题意 求不大于n的素数个数. 思路 素数筛法可解,筛法过程中 ...
- 《挑战程序设计竞赛》 4.1.1 矩阵 P286
想写几篇挑战的感悟,也有助于自己理解这本书.但这上面大多贴的是书上的代码,主要是为了用的时候后直接复制就好了,这样就很方便了,就相当于黑盒模板了. 1.线性方程组 /** \brief 高斯消元法 * ...
- poj1182食物链_并查集_挑战程序设计竞赛例题
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65534 Accepted: 19321 Description ...
随机推荐
- .NET面试题(一)
1.请编程遍历页面上所有TextBox控件并给它赋值为string.Empty? foreach (System.Windows.Forms.Control control in this.Contr ...
- sql server中Join有几种
JOIN: 如果表中有至少一个匹配,则返回行 (也就是 inner join)LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有 ...
- c++ abs与fabs
在stdlib.h中定义的abs只针对整数取决对值,如果要对浮点数取绝对值,应该用fabs(或fabsf). 而math.h中定义的abs是可以对浮点数取绝对值的. 所以如果包含了stdlib.h和m ...
- Error: Could not find or load main class org.apache.flume.tools.GetJavaProperty
问题: [root@master conf]# flume-ng version Error: Could not find or load main class org.apache.flume.t ...
- 【转载】Oracle之内存结构(SGA、PGA)
[转自]http://blog.itpub.net/25264937/viewspace-694917/ 一.内存结构 SGA(System Global Area):由所有服务进程和后台进程共享: ...
- atitit.流程标准化--- mysql启动不起来的排查流程attilax总结
atitit.流程标准化--- mysql启动不起来的排查流程attilax总结 1. mysql的启动日志文件 1 2. console方式 1 3. 安装为服务 1 3.1. 使用默认配置文件 1 ...
- hsqldb
http://www.hsqldb.org/ HSQLDB (HyperSQL DataBase) is the leading SQL relational database software wr ...
- 转:SNMP 原理及配置简述
SNMP 原理及配置简述 转载 2016年01月13日 16:18:51 随着机器数量的增长,管理员不能像过去那样,一台台机器进行监控.解决问题,而需要借助各方工具进行统一监控和管理.利用SNMP,一 ...
- matplotlib之设置极坐标的方向
#!/usr/bin/env python3 #-*- coding:utf-8 -*- ############################ #File Name: polar.py #Auth ...
- tensolrflow之基础变量
#优化一个乘法算子 #coding:utf- __author__ = 'similarface' import tensorflow as tf sess=tf.Session() #创建一个常量张 ...