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)的更多相关文章

  1. 1081. Rational Sum (20) -最大公约数

    题目如下: Given N rational numbers in the form "numerator/denominator", you are supposed to ca ...

  2. PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]

    题目 Given N rational numbers in the form "numerator/denominator", you are supposed to calcu ...

  3. PAT甲题题解-1081. Rational Sum (20)-模拟分数计算

    模拟计算一些分数的和,结果以带分数的形式输出注意一些细节即可 #include <iostream> #include <cstdio> #include <algori ...

  4. 【PAT甲级】1081 Rational Sum (20 分)

    题意: 输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数.输出N个分数的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPE ...

  5. PAT (Advanced Level) 1081. Rational Sum (20)

    简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  6. PAT 1081 Rational Sum

    1081 Rational Sum (20 分)   Given N rational numbers in the form numerator/denominator, you are suppo ...

  7. pat1081. Rational Sum (20)

    1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N ...

  8. PAT 1081 Rational Sum[分子求和][比较]

    1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppose ...

  9. 1081 Rational Sum(20 分)

    Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...

随机推荐

  1. DOM笔记(二):Node接口

    所有的节点都使用Node接口来表示,可以使用很多方法去获取节点,如document.getElementsByTagName().document.getElementsByName()等均返回一个N ...

  2. Canvas入门(2):图形渐变和图像形变换

    来源:http://www.ido321.com/986.html 一.图形渐变(均在最新版Google中测试) 1.绘制线性渐变 1: // 获取canvas 的ID 2: var canvas = ...

  3. matlab的&和&&操作

    A&B(1)首先判断A的逻辑值,然后判断B的值,然后进行逻辑与的计算.(2)A和B可以为矩阵(e.g. A=[1 0],B=[0 0]).A&&B(1)首先判断A的逻辑值,如果 ...

  4. Lucene学习笔记: 五,Lucene搜索过程解析

    一.Lucene搜索过程总论 搜索的过程总的来说就是将词典及倒排表信息从索引中读出来,根据用户输入的查询语句合并倒排表,得到结果文档集并对文档进行打分的过程. 其可用如下图示: 总共包括以下几个过程: ...

  5. <问题>Eclipse中Deploy应用到GAE的错误

    1.在Eclipse中部署App到Google App Engine(GAE),有时候会遇到这样的错误: java.lang.RuntimeException: Cannot get the Syst ...

  6. Directory.GetCurrentDirectory

    1.一个应用程序中,Directory.GetCurrentDirectory获得的当前工作目录是C:\Windows\System32,这是为什么呢?是如何设置的? 2.在WinXP下:System ...

  7. 20150913K-means聚类

    1.聚类的思想: 将一个有N个对象的数据集,构造成k(k<=n)个划分,每个划分代表一个簇.使得每个簇包含一个对象,每个对象有且仅属于一个簇.对于给定的k,算法首先给出一个初始的划分方法,以后通 ...

  8. 第二百四十三天 how can I 坚持

    制定的计划完成不了了,好多问题啊.又想当然了,晚上加了会班. 今天雾霾好严重,一出地铁大裤衩怎么没了.雾霾爆表啊. 还好现在刮大风了. 准备看<芈mi月传>了. 睡觉.

  9. Codeforces 599D Spongebob and Squares(数学)

    D. Spongebob and Squares Spongebob is already tired trying to reason his weird actions and calculati ...

  10. UIImage 相关操作

    修改UIImage大小 修改UISlider的最大值和最小值图片的时候,发现需要修改图片的大小,否则会导致UISlider变形.目前苹果还不支持直接修改UIImage类的大小,只能修改UIImageV ...