标签:int denominator numerator CF fraction Copy its
A. FractionPetya is a big fan of mathematics, especially its part related to fractions. Recently he learned that a fraction is called proper iff its numerator is smaller than its denominator (a < b) and that the fraction is called irreducible if its numerator and its denominator are coprime (they do not have positive common divisors except 1).
During his free time, Petya thinks about proper irreducible fractions and converts them to decimals using the calculator. One day he mistakenly pressed addition button ( + ) instead of division button (÷) and got sum of numerator and denominator that was equal to ninstead of the expected decimal notation.
Petya wanted to restore the original fraction, but soon he realized that it might not be done uniquely. That's why he decided to determine maximum possible proper irreducible fraction such that sum of its numerator and denominator equals n. Help Petya deal with this problem.
InputIn the only line of input there is an integer n (3 ≤ n ≤ 1000), the sum of numerator and denominator of the fraction.
OutputOutput two space-separated positive integers a and b, numerator and denominator of the maximum possible proper irreducible fraction satisfying the given sum.
Examples input Copy3output Copy
1 2input Copy
4output Copy
1 3input Copy
12output Copy
5 7
这道题就是给一个整数n,看看把它拆成分子i和分母n-i,找最大的分子分母互质的真分数
模拟即可
注意是从中间开始往两边,看看是不是gcd(a,b)==1就行了(互质)
一大堆英语读累死了,发现就是这么个...题
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b; 4 5 int gcd(int a,int b) 6 { 7 if(b==0)return a; 8 9 return gcd(b,a%b); 10 } 11 int main(){ 12 int n;cin>>n; 13 14 if(n%2==0)a=n/2,b=n/2; 15 16 else a=n/2,b=n/2+1; 17 18 while(gcd(a,b)!=1) 19 { 20 a--; 21 b++; 22 } 23 cout<<a<<" "<<b; 24 return 0; 25 26 }
数学题啊~还是~水题~还是~某个少女写了15分钟的~绝世好题~
而且,刚开始用的代码比这个麻烦,找揍
标签:int,denominator,numerator,CF,fraction,Copy,its 来源: https://www.cnblogs.com/guaguastandup/p/10340216.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。