Fraction(辗转相除求最大公约数)(CodeForcesOJ)
来源:互联网 发布:佳能9100cdn网络驱动 编辑:程序博客网 时间:2024/06/05 04:19
Petya is a big fan of mathematics, especially its part related to fractions. Recently he learned that a fraction is calledproper iff its numerator is smaller than its denominator (a < b) and that the fraction is calledirreducible if its numerator and its denominator are coprime (they do not have positive common divisors except1).
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 n instead 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 equalsn. Help Petya deal with this problem.
In the only line of input there is an integer n (3 ≤ n ≤ 1000), the sum of numerator and denominator of the fraction.
Output two space-separated positive integers a andb, numerator and denominator of the maximum possible proper irreducible fraction satisfying the given sum.
3
1 2
4
1 3
12
5 7#include<stdio.h>int gcd(int a,int b){ int r; while(a%b!=0) //关键 { r=a%b; a=b; b=r; } return b;}int main(){ int i,j,n,x,y; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n/2;i++) { if( i!=n-i && gcd(i,n-i)==1) { x=i; y=n-i; } } printf("%d %d\n",x,y); } return 0;}
- Fraction(辗转相除求最大公约数)(CodeForcesOJ)
- 求最大公约数和最小公倍数(辗转相除)
- 欧几里得求最大公约数算法(辗转相除)
- 辗转相除(求最大公约数及最小公倍数)
- gcd (辗转相除) 一般用于求最大公约数 最小公倍数
- 辗转相除求最大公约数
- 辗转相除求最大公约数
- 辗转相除求最大公约数
- 辗转相除求最大公约数
- 求两数最大公约数(辗转相除)
- 最大公约数、最小公倍数(辗转相除)
- 辗转相除求最大公约数,最大公倍数
- 欧几里得辗转相除求最大公约数最小公倍数
- 辗转相除之迭代法求最大公约数
- 辗转相除求最大公约数,最小公倍数
- JAVA辗转相除求最大公约数
- 辗转相除求最大公约数,最小公倍数
- 趣味分数-辗转相除 (欧几里德算法) 递归算法 求最大公约数-java
- 【Ubuntu】安装依赖文件
- SharedPrefence的一个简单工具类
- 文章标题
- Mybatis动态SQL trim
- Eclipse中新建java类自动生成作者和时间设置
- Fraction(辗转相除求最大公约数)(CodeForcesOJ)
- JS中的作用域跟作用域链
- Vim命令详解
- Linux+GCC+GDB
- Redis
- Spring Boot整合RabbitMQ
- sql语句题集
- unity官方内置网络unet的实例教程(八)
- LintCode-(632)二叉树的最大节点