GCD相关
板子:
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相关的更多相关文章
- CodeForces - 1059C Sequence Transformation (GCD相关)
Let's call the following process a transformation of a sequence of length nn. If the sequence is emp ...
- 4.3 多线程进阶篇<中>(GCD)
更正:队列名称的作用的图中,箭头标注的有些问题,已修正 本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书” 本文源码 Demo 详见 Gith ...
- 一些gcd计数问题
数论什么的全都忘光了吧QAQ 做了几道简单的题练习一下. bzoj1101: [POI2007]Zap 求有多少对数满足 gcd(x,y)=d, 1<=x<=a, 1<=y<= ...
- iOS GCD 编程小结
一.简单介绍 1.GCD简介? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD优势 GCD是苹果公司为多核的并行运算提出的 ...
- iOS开发多线程篇—GCD介绍
iOS开发多线程篇—GCD介绍 一.简单介绍 1.什么是GCD? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD的优势 G ...
- GCD下的几种实现同步的方式
GCD多线程下,实现线程同步的方式有如下几种: 1.串行队列 2.并行队列 3.分组 4.信号量 实例: 去网上获取一张图片并展示在视图上. 实现这个需求,可以拆分成两个任务,一个是去网上获取图片,一 ...
- iOS多线程知识总结--GCD
iOS多线程知识总结--GCD 1. iOS中苹果提供4钟方案来帮助我们实现多线程: (1) 纯C语言的pthread,偏底层,需要程序员手动管理线程的生命周期,基本不用. (2) OC语言的NSTr ...
- ios开发多线程--GCD
引言 虽然GCD使用很广,而且在面试时也经常问与GCD相关的问题,但是我相信深入理解关于GCD知识的人肯定不多,大部分都是人云亦云,只是使用过GCD完成一些很简单的功能.当然,使用GCD完成一些简单的 ...
- IOS 多线程之GCD
参考:http://www.cnblogs.com/wendingding/p/3806821.html <<Objective-C基础教程>> 第二版 一 简介 GCD 全称 ...
随机推荐
- string method 字符串常用方法讲解
st = 'hello ketty ##$ \*'print(st.count('t'))# 输出‘t’的个数print(st.capitalize()) #Hello ketty 将首字母大写pri ...
- 一段关于用户登录 和乘法表的python代码
用户登录代码(低配): name = 1password =11counter = 1while counter <3 : a = int(input ('name:')) #注意这里 inpu ...
- MVC的概念
MVC模式的概念: 1.Model(业务模型):应用程序中用于处理应用程序数据逻辑的部分,通常模型对象负责在数据库中存取数据. 说白了就是确定要打的地基等一系列信息的. 2.view(视图):应用程序 ...
- 【转】spring framework 5以前体系结构及内部各模块jar之间的maven依赖关系
作者:凌承一 出处:http://www.cnblogs.com/ywlaker/ 很多人都在用spring开发java项目,但是配置maven依赖的时候并不能明确要配置哪些spring的jar, ...
- JS刷算法题:二叉树
Q1.翻转二叉树(easy) 如题所示 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 来源:力扣(LeetCode) ...
- Harbor 1.9.x 版本从源码构建和运行
介绍 本指南为开发人员提供了从源代码构建和运行Harbor的说明. 步骤1:为Harbor的构建环境做准备 Harbor被部署为多个Docker容器,并且大多数代码都是用Go语言编写的.构建环境需要D ...
- linux安装mariadb
yum install -y mariadb-server 账号:root 密码:空 本地登录:mysql -u root -p 远程登录:mysql -h 192.168.0.1 -u root - ...
- vue初始化、数据处理、组件传参、路由传参、全局定义CSS与JS、组件生命周期
目录 项目初始化 组件数据局部化处理 子组件 父组件 路由逻辑跳转 案例 组件传参 父传子 子组件 父组件 子传父 子组件 父组件 组件的生命周期钩子 路由传参 第一种 配置:router/index ...
- Disk:磁盘管理之LVM和系统磁盘扩容
简介 小伙伴们好,好久不见,今天想给大家介绍一下关于磁盘管理的方法和心得:磁盘管理可谓运维工作中的重要内容,主要包括磁盘的合理规划以及扩缩容 常用的磁盘管理方法为LVM(Logical Volume ...
- 【题解】P3373 【模板】线段树 2
线段树解法 好丢脸,这个题做了一下午,调试了三个多小时...... 先讲讲解题思路 既然这里是线段树,就要用到lazy-tag.又有加法又有乘法的话,就要用到两个lazy-tag,分别用数组jia[] ...