板子:

 int gcd(int a, int b) { return b >  ? gcd(b, a % b) : a; }

POJ1930

题意:给你一个无限循环小数,给到小数点后 9 位,要求这个数的分数形式。

解法:

要想解决这道题,首先应该了解如何将循环小数化为分数:

一,纯循环小数化分数:循环节的数字除以循环节的位数个 9 组成的整数。例如: 
$0.3333……=3/9=1/3$;
$0.285714285714……=285714/999999=2/7$. 
二,混循环小数:(例如:$0.24333333……$)不循环部分和循环节构成的的数减去不循环部分的差,再除以循环节位数个 9 添上不循环部分的位数个  0。例如: 
$0.24333333…………=(243-24)/900=73/300 $
$0.9545454…………=(954-9)/990=945/990=21/22$

这便是循环小数化成分数的方法,知道这个后,解决这道题也好办了。

代码如下:

 #pragma GCC optimize(3)
#include <time.h>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
const double EPS = 1e-;
const int INF = ;
const long long LLINF = ;
const double PI = acos(-1.0); inline int READ() {
char ch;
while ((ch = getchar()) < || < ch)
;
int ans = ch - ;
while ( <= (ch = getchar()) && ch <= )
ans = (ans << ) + (ans << ) + ch - ;
return ans;
} #define REP(i, a, b) for (int i = (a); i <= (b); i++)
#define PER(i, a, b) for (int i = (a); i >= (b); i--)
#define FOREACH(i, t) for (typeof(t.begin()) i = t.begin(); i != t.end(); i++)
#define MP(x, y) make_pair(x, y)
#define PB(x) push_back(x)
#define SET(a) memset(a, -1, sizeof(a))
#define CLR(a) memset(a, 0, sizeof(a))
#define MEM(a, x) memset(a, x, sizeof(a))
#define ALL(x) begin(x), end(x)
#define LL long long
#define Lson (index * 2)
#define Rson (index * 2 + 1)
#define pii pair<int, int>
#define pll pair<LL, LL>
#define MOD ((int)1000000007)
#define MAXN 20 + 5
///**********************************START*********************************///
string ss, sn; char s[MAXN], ns[MAXN];
int nex[MAXN]; void get_next(char* p, int next[]) {
int pLen = strlen(p) + ; //要求循环节长度时这里才+1
next[] = -;
int k = -;
int j = ;
while (j < pLen - ) {
if (k == - || p[j] == p[k]) {
++j;
++k;
if (p[j] != p[k])
next[j] = k;
else
next[j] = next[k];
} else {
k = next[k];
}
}
} int gcd(int a, int b) { return b > ? gcd(b, a % b) : a; } void cal_cal(int len) {
int up = , down = ;
for (int i = len - ; i >= ; i--)
up += int(s[i] - '') * pow(, len - i - );
for (int i = ; i < len; i++) down += * pow(, i);
int g = gcd(up, down);
printf("%d/%d\n", up / g, down / g);
return;
} void cal_cal2(int st, int len) {
char tmp[];
int up = , down = ;
int a, b; for (int i = ; i < st; i++) tmp[i] = s[i];
tmp[st] = '\0';
a = atoi(tmp);
for (int i = st; i < st + len; i++) tmp[i - st] = s[i];
tmp[len] = '\0';
b = atoi(tmp); up = a * pow(, len) + b - a;
int pos = ;
for (int i = ; i < len; i++) tmp[pos++] = '';
for (int i = ; i < st; i++) tmp[pos++] = '';
down = atoi(tmp);
int g = gcd(up, down);
printf("%d/%d\n", up / g, down / g);
return;
} int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif // !ONLINE_JUDGE
while (cin >> ss) {
if (ss.size() == ) break;
int pos = ;
for (int i = ; ss[i] != '.'; i++) s[pos++] = ss[i];
s[pos] = '\0';
int flag = ; // 1为全循环,2为混合循环
get_next(s, nex);
int len = pos - nex[pos];
if (len != pos) {
cal_cal(len);
} else {
int st;
for (st = ; st < pos; st++) {
int nlen = ;
for (int i = st; i < pos; i++) {
ns[nlen++] = s[i];
}
CLR(nex);
get_next(ns, nex);
if (nlen - nex[nlen] != nlen) {
cal_cal2(st, nlen);
break;
} else
continue;
}
}
}
return ;
}

GCD相关的更多相关文章

  1. CodeForces - 1059C Sequence Transformation (GCD相关)

    Let's call the following process a transformation of a sequence of length nn. If the sequence is emp ...

  2. 4.3 多线程进阶篇<中>(GCD)

    更正:队列名称的作用的图中,箭头标注的有些问题,已修正 本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书” 本文源码 Demo 详见 Gith ...

  3. 一些gcd计数问题

    数论什么的全都忘光了吧QAQ 做了几道简单的题练习一下. bzoj1101: [POI2007]Zap 求有多少对数满足 gcd(x,y)=d, 1<=x<=a, 1<=y<= ...

  4. iOS GCD 编程小结

    一.简单介绍 1.GCD简介? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD优势 GCD是苹果公司为多核的并行运算提出的 ...

  5. iOS开发多线程篇—GCD介绍

    iOS开发多线程篇—GCD介绍 一.简单介绍 1.什么是GCD? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD的优势 G ...

  6. GCD下的几种实现同步的方式

    GCD多线程下,实现线程同步的方式有如下几种: 1.串行队列 2.并行队列 3.分组 4.信号量 实例: 去网上获取一张图片并展示在视图上. 实现这个需求,可以拆分成两个任务,一个是去网上获取图片,一 ...

  7. iOS多线程知识总结--GCD

    iOS多线程知识总结--GCD 1. iOS中苹果提供4钟方案来帮助我们实现多线程: (1) 纯C语言的pthread,偏底层,需要程序员手动管理线程的生命周期,基本不用. (2) OC语言的NSTr ...

  8. ios开发多线程--GCD

    引言 虽然GCD使用很广,而且在面试时也经常问与GCD相关的问题,但是我相信深入理解关于GCD知识的人肯定不多,大部分都是人云亦云,只是使用过GCD完成一些很简单的功能.当然,使用GCD完成一些简单的 ...

  9. IOS 多线程之GCD

    参考:http://www.cnblogs.com/wendingding/p/3806821.html <<Objective-C基础教程>> 第二版 一 简介 GCD 全称 ...

随机推荐

  1. GitHub Pages 与 Gitee Pages 上的 Jekyll

    GitHub 与 Gitee 提供的 Pages 服务中,均内嵌了 Jekyll 支持(Gitee 还提供了 Hugo 与 Hexo 支持).所谓「支持」,即指这些生成工具挂在云端:你只需要提供原始代 ...

  2. Docker底层架构之控制组

    概述 控制组(cgroups)是 Linux 内核的一个特性,主要用来对共享资源进行隔离.限制.审计 等. 只有能控制分配到容器的资源,才能避免当多个容器同时运行时的对系统资源的竞争. 控制组技术最早 ...

  3. js原型和原型链的简单理解

    构造函数创建对象: function Person() { } var person = new Person(); person.name = 'Tian'; console.log(person. ...

  4. 8、OSPF

    OSPF ---最短路径优先 用于在单一自治系统(Autonomous System-AS)内决策路由 自制系统(AS)AS: 执行统一路由策略的一组网络设备的组合可适应大规模的网络: ·    路由 ...

  5. 死磕mysql(2)

    想测试自己的查询语句,导入批量的数据,一开始很慢以为是自己的语句有问题,后来是这个autocommit,效率相差好多好多............ delimiter // create procedu ...

  6. 视觉slam十四讲第七章课后习题6

    版权声明:本文为博主原创文章,转载请注明出处: http://www.cnblogs.com/newneul/p/8545450.html 6.在PnP优化中,将第一个相机的观测也考虑进来,程序应如何 ...

  7. POJ_1485_dp

    题目描述: 每组数据给n个点,点按一维坐标升序给出,要求划分成k块,在每一块中,取一个站,要求每个块中所有的点到站的距离的和的总和最小. 思路: dp题,dp[i][j]表示i个点分成j块的最小距离, ...

  8. Codeforces_490_E

    http://codeforces.com/problemset/problem/490/E dfs,过程要注意细节,特别是当前位置取了与上个数当前位置相同是,若后面不符合条件,则当前位置要重置'?' ...

  9. vuejs之vue和springboot后端进行通信

    一.新建一个vue项目,建立好后的相关文件 查看一下新建好的vue项目的结构: 当前各个文件中的内容: App.vue:主入口 <template> <div id="ap ...

  10. Method Resolve Order (MRO) - 类对象属性的解析顺序

    Method Resolve Order (MRO) - 类对象属性的解析顺序 Python 支持多重继承, 此时就需要解决按照何种顺序来解析属性的问题.类的继承关系在一个特殊的类属性中指定(__mr ...