博客
关于我
PAT-BASIC1062——最简分数
阅读量:215 次
发布时间:2019-02-28

本文共 3314 字,大约阅读时间需要 11 分钟。

我的 PAT-Basic 代码仓:

原题链接:

题目描述:

知识点:欧几里得最大公约数算法

思路一:通分后比较分子

最简分数的定义:分子分母的最大公约数为1,这涉及到欧几里得最大公约数算法。关于欧几里得最大公约数算法的伪码,可以参考中的分析。

将分数的分母都变成M1M2K。

题目规定了给出的所有整数不超过1000,但如果用这个思路,M1M2K最大值为10^9,再加上题给的分数可能是假分数,分子不一定比分母要小,分子可能会超出int型的范围,我们需要用long型变量存储。否则无法通过测试点2。

本题有一个坑点:

N1/M1不一定比N2/M2要小,可能需要我们换一个顺序。如果不考虑到这一点,无法通过测试点1。

时间复杂度和空间复杂度的分析对于本题来说意义不大。

C++代码:

#include

#include
#include
using namespace std;

int calculateIndexSlide(string s);long calculateNumerator(string s);long calculateDenominator(string s);long gcd(long num1, long num2);

int main() {string input1;string input2;cin >> input1 >> input2;

long N1 = calculateNumerator(input1);long M1 = calculateDenominator(input1);long N2 = calculateNumerator(input2);long M2 = calculateDenominator(input2);long K;cin >> K;long minNumerator = K * M2 * N1;long maxNumerator = K * M1 * N2;if(minNumerator > maxNumerator) {    long temp = minNumerator;    minNumerator = maxNumerator;    maxNumerator = temp;}vector
numerators;for(long i = 1;; i++) { if(M1 * M2 * i >= maxNumerator) { break; } if(gcd(K, i) == 1 && M1 * M2 * i > minNumerator) { numerators.push_back(i); }}for(int i = 0; i < numerators.size(); i++) { cout << numerators[i] << "/" << K; if(i != numerators.size() - 1) { cout << " "; }}return 0;

}

int calculateIndexSlide(string s) {for(int i = 0; i < s.length(); i++) {if(s[i] == '/') {return i;}}return -1;}

long calculateNumerator(string s) {int indexSlide = calculateIndexSlide(s);long N = 0;for(int i = 0; i < indexSlide; i++) {N = N * 10 + (s[i] - '0');}return N;}

long calculateDenominator(string s) {int indexSlide = calculateIndexSlide(s);long M = 0;for(int i = indexSlide + 1; i < s.length(); i++) {M = M * 10 + (s[i] - '0');}return M;}

long gcd(long num1, long num2) {if(num2 == 0) {return num1;}return gcd(num2, num1 % num2);}

C++解题报告:

思路二:直接用浮点数进行比较

思路一这么麻烦,还需要通分,何不直接用浮点数进行比较呢?这样也就避免了int型数据越界的问题。

C++代码:

#include

#include
#include
using namespace std;

int calculateIndexSlide(string s);int calculateNumerator(string s);int calculateDenominator(string s);int gcd(int num1, int num2);

int main() {string input1;string input2;cin >> input1 >> input2;

int N1 = calculateNumerator(input1);int M1 = calculateDenominator(input1);int N2 = calculateNumerator(input2);int M2 = calculateDenominator(input2);int K;cin >> K;double min = 1.0 * N1 / M1;double max = 1.0 * N2 / M2;if(min > max) {    double temp = min;    min = max;    max = temp;}vector
numerators;for(int i = 1;; i++) { if(1.0 * i / K >= max) { break; } if(gcd(K, i) == 1 && 1.0 * i / K > min) { numerators.push_back(i); }}for(int i = 0; i < numerators.size(); i++) { cout << numerators[i] << "/" << K; if(i != numerators.size() - 1) { cout << " "; }}return 0;

}

int calculateIndexSlide(string s) {for(int i = 0; i < s.length(); i++) {if(s[i] == '/') {return i;}}return -1;}

int calculateNumerator(string s) {int indexSlide = calculateIndexSlide(s);int N = 0;for(int i = 0; i < indexSlide; i++) {N = N * 10 + (s[i] - '0');}return N;}

int calculateDenominator(string s) {int indexSlide = calculateIndexSlide(s);int M = 0;for(int i = indexSlide + 1; i < s.length(); i++) {M = M * 10 + (s[i] - '0');}return M;}

int gcd(int num1, int num2) {if(num2 == 0) {return num1;}return gcd(num2, num1 % num2);}

转载地址:http://ihli.baihongyu.com/

你可能感兴趣的文章
Python WebDriver如何打印整个页面源(html)
查看>>
Python WebSocket自动化测试:构建高效接口测试框架
查看>>
Python Web开发
查看>>
Redis 配置文件杂项。
查看>>
Python web自动化测试 —— 文件上传
查看>>
python win32api键盘_Python win32api.keybd_event模拟键盘输入
查看>>
python Workbook 表格宽度
查看>>
python 中 dict 的另一种用法
查看>>
Python 中只有一个 True 和一个 False 对象吗?
查看>>
Python 中多线程与多处理之间的区别
查看>>
Python 从入门到精通:30天速成教程到底有多狠?你能坚持下来吗?
查看>>
Python 从数据库中存储和检索密码的最安全方法
查看>>
Python语言及其应用 - 知识点遍历
查看>>
Python 优化提速的 8 个小技巧
查看>>
Python 余弦相似度与皮尔逊相关系数 计算
查看>>
python 使用execjs 报编码错误解决办法,UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xac in position 145: il
查看>>
python 使用filetype校验文件
查看>>
Python 使用flush函数将缓冲区数据立即写磁盘
查看>>
python 使用in判断不准确,in不好使
查看>>
Python 使用pandas 进行查询和统计详解
查看>>