1081. Rational Sum (20)
the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1081
the code is as followed:
#include<stdio.h>
#include<math.h>
long int gongyue(long int num1, long int num2)
{
long int gcd=0;
if (num1==num2)
{
gcd = num1;
}
if (num1>num2)
{
long int tmp = num1;
num1 = num2;
num2 = tmp;
}
if (num2 % num1 == 0)
{
gcd = num1;
}
else
{
long int tmp = num1;
num1 = num2 % num1;
num2 = tmp;
gcd = gongyue(num1, num2);
}
return gcd;
}
long int gongbei(long int x, long int y)
{
return x * y / gongyue(x,y);
} int main()
{
long int fenzi,fenmu;
long int tempzi,tempmu;
int n;
//printf("%d",gongbei(3,8));
scanf("%d",&n);
scanf("%ld/%ld",&fenzi,&fenmu);
n -= 1;
long int temp = fenzi;
if (fenzi == 0)
{
fenmu = 1;
}
else
{
fenzi = fenzi / gongyue(abs(temp), fenmu);
fenmu = fenmu / gongyue(abs(temp), fenmu);
}
while (n--)
{ scanf("%ld/%ld",&tempzi,&tempmu);
fenzi = fenzi * gongbei(fenmu,tempmu)/fenmu + tempzi * gongbei(fenmu,tempmu)/tempmu;
fenmu = gongbei(fenmu,tempmu);
long int tempfenzi = fenzi;
if (fenzi == 0)
{
fenmu = 1;
}
else
{
fenzi = fenzi / gongyue(abs(tempfenzi), fenmu);
fenmu = fenmu / gongyue(abs(tempfenzi), fenmu);
} }
if (abs(fenzi)>=fenmu)
{
printf("%ld",fenzi/fenmu);
if (fenzi%fenmu != 0)
{
printf(" %ld/%ld",abs(fenzi)%fenmu,fenmu);
}
printf("\n");
}
else
{
if (fenzi == 0)
{
printf("0");
}
else
{
printf("%ld/%ld",fenzi,fenmu);
}
printf("\n");
}
}
the time complexity is O(n) .
1081. Rational Sum (20)的更多相关文章
- 1081. Rational Sum (20) -最大公约数
题目如下: Given N rational numbers in the form "numerator/denominator", you are supposed to ca ...
- PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]
题目 Given N rational numbers in the form "numerator/denominator", you are supposed to calcu ...
- PAT甲题题解-1081. Rational Sum (20)-模拟分数计算
模拟计算一些分数的和,结果以带分数的形式输出注意一些细节即可 #include <iostream> #include <cstdio> #include <algori ...
- 【PAT甲级】1081 Rational Sum (20 分)
题意: 输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数.输出N个分数的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPE ...
- PAT (Advanced Level) 1081. Rational Sum (20)
简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- PAT 1081 Rational Sum
1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppo ...
- pat1081. Rational Sum (20)
1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N ...
- PAT 1081 Rational Sum[分子求和][比较]
1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppose ...
- 1081 Rational Sum(20 分)
Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...
随机推荐
- <译>Selenium Python Bindings 4 - Locating Eelements
有各种不同的策略来定位页面中的元素.你可以使用最合适定位方式用于你的用例.Selenium提供了以下方法来定位页面中的元素: find_element_by_id find_element_by_na ...
- net中前台javascript与后台c#函数相互调用
问: 1.如何在JavaScript访问C#函数? 2.如何在JavaScript访问C#变量? 3.如何在C#中访问JavaScript的已有变量? 4.如何在C#中访问JavaScript函数? ...
- Chapter5:语句
表达式语句:一个表达式+一个分号 表达式语句的作用是执行表达式并丢弃掉求值结果. 空语句:单独一个分号 Best Practice:使用空语句时应该加上注释,从而令读这段代码的人知道该语句是有意省略的 ...
- CSS基础(背景、文本、列表、表格、轮廓)
CSS 背景属性 属性 描述 background 简写属性,作用是将背景属性设置在一个声明中. background-attachment 背景图像是否固定或者随着页面的其余部分滚动. backgr ...
- Redis3.0 Install
Installation Download, extract and compile Redis with: $ wget http://download.redis.io/releases/redi ...
- CTS FAIL(一)
首先简单介绍下CTS:全称Compatibility Test Suite,通过CTS测试,来检测android apk与android系统的兼容性. 最近公司release一版新的Image,但在新 ...
- Java集合排序(看完秒懂)
比如将一个List<Student>排序,则有两种方式: 1:Student实现Comparable接口: 2:给排序方法传递一个Comparator参数: 请看下面的举例: Studen ...
- Java——泛型(最易懂的方式讲解泛型)
来自: 代码大湿 代码大湿 写在前面: 只要认真看过,基本能很熟悉泛型的特性.泛型是JDK1.5之后出现的,比如JDK1.5之前的ArrayList,会出现2个问题 1:向ArrayList当中添加对 ...
- jar,war,ear区别及java基础杂七八
jar,war,earqu区别 这三种文件都可以看作是java的压缩格式,其实质是实现了不同的封装: jar--封装类war--封装web站点ear--封装ejb.它们的关系具体为:jar: ...
- Create a commit using pygit2
Create a commit using pygit2 Create a commit using pygit2 2015-04-06 10:41 user1479699 imported from ...