1062. 最简分数(20)
来源:互联网 发布:jquery.checktree.js 编辑:程序博客网 时间:2024/06/01 12:02
这是一道不错的题目。
考到了分数化简的求最大公因数算法。
再次记忆:
int gcd(int a, int b) { //自己未曾接触过的求两个数最大公因数的算法
return b == 0 ? a : gcd(b, a%b);
}
// 2017/10/14 NCU // PAT-B 1065// scienceZ#include <cstdio>#include <cstring>#include <iostream>#include <sstream>#include <algorithm>using namespace std;int n, a1, a2, b1, b2;double a, b;char arr[100];bool key = true;int gcd(int a, int b) { //自己未曾接触过的求两个数最大公因数的算法 return b == 0 ? a : gcd(b, a%b);}int main(){ gets(arr); int l = strlen(arr); for(int i = 0; i<l; i++){ if(arr[i] == '/') arr[i] = ' '; } stringstream ss(arr); ss >> a1 >> a2 >> b1 >> b2 >> n; a = double(a1)/a2; b = double(b1)/b2; if (a > b){ double t; t = a; a = b; b = t; } for (int i = 1; ;i++){ double c = double(i)/n; if(c>a && c<b && gcd(i, n) == 1) if (key) {printf("%d/%d", i, n); key = false;} else printf(" %d/%d", i, n); if(c>b) break; } return 0;}
阅读全文
0 0
- 1062. 最简分数(20)
- 1062. 最简分数(20)
- 1062. 最简分数(20)
- 1062. 最简分数(20)
- 1062. 最简分数(20)
- 1062. 最简分数(20)
- 1062. 最简分数(20)
- 1062. 最简分数(20)
- 1062. 最简分数(20)
- 1062. 最简分数(20)
- 1062. 最简分数(20)
- 1062. 最简分数(20)
- 1062. 最简分数(20)
- 1062. 最简分数(20)
- 1062. 最简分数(20)
- 1062. 最简分数(20)
- 1062. 最简分数(20)
- 1062. 最简分数(20)
- 最流行的java 工具类
- 作为一名准备进军c的新手有一些问题
- 移动矩形代码和一个非常重要的windows消息队列机制的坑
- OpenGL_4:画一个长方形
- SQL case语句
- 1062. 最简分数(20)
- centos7搭建完nginx后禁用firewall防火墙和selinux
- python测试模块pytest之坑
- springmvc 之DispatcherServlet逻辑处理
- Solr的基本使用
- ios-Swift实现自定义输出
- Mex(线段树)
- PAT乙级1033
- 复习:全局变量、局部变量、静态全局变量、静态局部变量在内存中如何存储,有什么区别,栈和堆的区别等知识点(非原创)