PAT 乙 1062 最简分数(1测试点未通过)

此博客介绍了一个算法问题,即找出两个指定分数间所有分母为特定值的最简分数。通过输入两个分数及目标分母,程序将输出位于两分数之间的所有符合条件的最简分数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1062 最简分数 (20 分)

一个分数一般写成两个整数相除的形式:N/M,其中 M 不为0。最简分数是指分子和分母没有公约数的分数表示形式。

现给定两个不相等的正分数 N​1​​/M​1​​ 和 N​2​​/M​2​​,要求你按从小到大的顺序列出它们之间分母为 K 的最简分数。

输入格式:

输入在一行中按 N/M 的格式给出两个正分数,随后是一个正整数分母 K,其间以空格分隔。题目保证给出的所有整数都不超过 1000。

输出格式:

在一行中按 N/M 的格式列出两个给定分数之间分母为 K 的所有最简分数,按从小到大的顺序,其间以 1 个空格分隔。行首尾不得有多余空格。题目保证至少有 1 个输出。

输入样例:

7/18 13/20 12

输出样例:

5/12 7/12
#include<iostream>
#include<math.h>
using namespace std;
int main(){
  int fz1,fz2,fm1,fm2,pub;
  scanf("%d/%d%d/%d%d",&fz1,&fm1,&fz2,&fm2,&pub);
double max,min,temp;
min=(fz1/(fm1*1.0/pub));
max=(fz2/(fm2*1.0/pub));
if(min>max){
	temp=min;
	min=max;
	max=temp;
} 
//cout<<min<<endl;
//cout<<max<<endl;
min=(int)(min)+1;
max=(int)max;
//cout<<max<<endl;
int cnt=0;
for(int i=min;i<=max;i++){
int flag=0;
	for(int j=2;j<=i;j++){
		if(i%j==0&&pub%j==0)
		flag=1;

	}
		if(flag==0){
			
			if(cnt!=0)printf(" ");
		printf("%d/%d",i,pub);
		cnt++;
		}
    
	
}
  

}

 

### 关于PAT1034题目的解析 对于PAT1034题目——有理数四则运算,此题旨在考察对有理数加减乘除操作的理解以及实现能力。该类问题通常涉及分数的表示方法及其基本运算逻辑的设计。 #### 题目概述 给定两个有理数,执行指定的操作(加法、减法、乘法或除法),并返回简化后的结果。需要注意的是,在处理过程中应当考虑如何有效地化简最终得到的结果,确保分子分母之间不存在公约数[^5]。 #### 示例代码展示 下面是一个简单的Python版本解决方案: ```python from math import gcd def simplify(numerator, denominator): if denominator < 0: numerator *= -1 denominator *= -1 common_divisor = abs(gcd(numerator, denominator)) simplified_numerator = int(numerator / common_divisor) simplified_denominator = int(denominator / common_divisor) return f"{simplified_numerator}/{simplified_denominator}" class RationalNumber: def __init__(self, num, den=1): self.num = num self.den = den @staticmethod def parse(input_str): parts = input_str.split('/') try: num = int(parts[0]) den = int(parts[-1]) or 1 except ValueError as e: raise Exception('Invalid rational number format') from e return RationalNumber(num=num, den=den) def add(self, other): new_num = self.num * other.den + other.num * self.den new_den = self.den * other.den result = simplify(new_num, new_den) return result def subtract(self, other): new_num = self.num * other.den - other.num * self.den new_den = self.den * other.den result = simplify(new_num, new_den) return result def multiply(self, other): new_num = self.num * other.num new_den = self.den * other.den result = simplify(new_num, new_den) return result def divide(self, other): if not isinstance(other, RationalNumber) and other != 0: raise ZeroDivisionError("Cannot divide by zero.") new_num = self.num * other.den new_den = self.den * other.num result = simplify(new_num, new_den) return result if __name__ == "__main__": operation_map = { '+': 'add', '-': 'subtract', '*': 'multiply', '/': 'divide' } expression = "1/2 + (-1/3)" op_index = next((i for i, char in enumerate(expression) if char in "+-*/"), None) first_operand = RationalNumber.parse(expression[:op_index].strip()) second_operand = RationalNumber.parse(expression[op_index+1:].strip()) method_to_call = getattr(first_operand, operation_map.get(expression[op_index])) print(method_to_call(second_operand)) ``` 上述程序定义了一个`RationalNumber`类来封装有理数对象,并实现了四个主要的方法用于完成相应的算术运算。此外还提供了一个辅助函数`simplify()`用来约分化简所得的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值