当前位置:网站首页>[pat (basic level) practice] - [simple mathematics] 1062 simplest fraction
[pat (basic level) practice] - [simple mathematics] 1062 simplest fraction
2022-07-06 00:57:00 【IronmanJay】
List of articles
One 【 questions 】
- Class B
Two 【 Title number 】
- 1062 Simplest fraction (20 branch )
3、 ... and 【 Title Description 】
- A fraction is usually written as the division of two integers : N / M N/M N/M, among M M M Not for 0. The simplest fraction is a fraction representation in which the numerator and denominator have no common divisor .
- Now let's give two unequal positive fractions N 1 / M 1 N_1 /M_1 N1/M1 and N 2 / M 2 N_2 /M_2 N2/M2 , You are required to list them in descending order. The denominator between them is K K K The simplest fraction of .
Four 【 Title Example 】
Input format :
Enter and press... In one line N / M N/M N/M The format gives two positive fractions , Followed by a positive integer denominator K K K, Separated by spaces . The title guarantees that all integers given do not exceed 1000.Output format :
Press in a line N / M N/M N/M The format lists the denominator between two given scores K K K All the simplest fractions of , In order from small to large , In the meantime 1 Space separation . There must be no extra space at the beginning and end of the line . The title is guaranteed to have at least 1 Outputs .sample input :
7/18 13/20 12sample output :
5/12 7/12
5、 ... and 【 Their thinking 】
- The idea of this question is quite normal , The general idea is to convert two fractions into decimals , Traverse all the scores between two numbers , You need to ensure that this score is between two numbers , And it is the simplest fraction ( As long as the numerator and denominator have no common divisor, it is the simplest fraction ), Because we should ensure that the denominator is given K K K, So from 1 So let's start walking through K K K, Look for every possible score , Output only when conditions are met . But there are a few things to note , As shown below :
①: The result of division needs to be used d o u b l e double double type , Because there must be scores
②: Note that the two inputs of the topic are not necessarily in order , Need to compare the size , Convenient, the back is quite
③: Pay attention to the spaces , There is no space in front of the first output , There is a space in front of the rest of the output
6、 ... and 【 The final score 】
- 20 branch
7、 ... and 【 Code implementation 】
#include<stdio.h>
#include<stdbool.h>
bool HasCommonDivisor(int i,int k)
{
for(int j = 2;j<=i;j++)
{
if(i % j == 0 && k % j == 0)
{
return true;
}
}
return false;
}
int main()
{
int n1,m1,n2,m2,k;
scanf("%d/%d %d/%d %d",&n1,&m1,&n2,&m2,&k);
double d1 = (double)n1 / m1;
double d2 = (double)n2 / m2;
bool flag = true;
if(d1 > d2)
{
double temp = d2;
d2 = d1;
d1 = temp;
}
for(int i = 1;i<k;i++)
{
if(!HasCommonDivisor(i,k))
{
double d = (double)i / k;
if(d > d1 && d < d2)
{
if(flag == true)
{
printf("%d/%d",i,k);
flag = false;
}
else
{
printf(" %d/%d",i,k);
}
}
}
}
return 0;
}
8、 ... and 【 Submit results 】

边栏推荐
- vSphere实现虚拟机迁移
- Five challenges of ads-npu chip architecture design
- Mobilenet series (5): use pytorch to build mobilenetv3 and learn and train based on migration
- Intensive learning weekly, issue 52: depth cuprl, distspectrl & double deep q-network
- MYSQL---查询成绩为前5名的学生
- Idea远程提交spark任务到yarn集群
- Yolov5, pychar, Anaconda environment installation
- The growth path of test / development programmers, the problem of thinking about the overall situation
- FFT 学习笔记(自认为详细)
- Dede collection plug-in free collection release push plug-in
猜你喜欢
![Cf:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]](/img/9e/c933f454a39d906a407e4d415f0b87.png)
Cf:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]

ADS-NPU芯片架构设计的五大挑战

SAP Spartacus home 页面读取 product 数据的请求的 population 逻辑

I'm interested in watching Tiktok live beyond concert

Problems and solutions of converting date into specified string in date class

cf:D. Insert a Progression【关于数组中的插入 + 绝对值的性质 + 贪心一头一尾最值】

Illustrated network: the principle behind TCP three-time handshake, why can't two-time handshake?

Idea远程提交spark任务到yarn集群

MCU通过UART实现OTA在线升级流程

282. Stone consolidation (interval DP)
随机推荐
[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)
C language programming (Chapter 6 functions)
Gartner发布2022-2023年八大网络安全趋势预测,零信任是起点,法规覆盖更广
Spark获取DataFrame中列的方式--col,$,column,apply
Installation and use of esxi
Five challenges of ads-npu chip architecture design
[groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte
logstash清除sincedb_path上传记录,重传日志数据
NLP text processing: lemma [English] [put the deformation of various types of words into one form] [wet- > go; are- > be]
RAID disk redundancy queue
Problems and solutions of converting date into specified string in date class
Arduino六足机器人
China Taiwan strategy - Chapter 8: digital marketing assisted by China Taiwan
cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
Cloud guide DNS, knowledge popularization and classroom notes
MYSQL---查询成绩为前5名的学生
毕设-基于SSM高校学生社团管理系统
After Luke zettlemoyer, head of meta AI Seattle research | trillion parameters, will the large model continue to grow?
Interview must brush algorithm top101 backtracking article top34
For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet