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. STL六大组件之——仿函数偷窥

    仿函数(functor),就是使一个类或类模板的使用看上去象一个函数.其实现就是类或类模板中对operator()进行重载,这个类或类模板就有了类似函数的行为.仿函数是智能型函数就好比智能指针的行为像 ...

  2. CCCallFuncN误用导致引用计数循环引用

    昨天测试“角色被遮挡部分透明显示”功能时,发现角色死亡后,其轮廓精灵不会消失.调试发现,角色在死亡时,其引用计数retain_count居然是9.这是由引用计数混乱引起的内存泄露. 加了很多日志跟踪r ...

  3. 关于CodeBlocks中stdc++-6.dll缺失的小问题

    前一阵子在用CodeBlocks刷OJ(也并不是什么很难的算法,背包问题而已)时,遇到了个小麻烦,按下F9编译运行(有的人习惯用鼠标去点那个小button,多麻烦啊)后,Windows给我跳出个小窗说 ...

  4. rman的conver方法拷贝ASM文件

    rman中的conver命令主要用户跨平台传输表空间,也可以完成从ASM何本地文件系统中拷贝文件,比用dbms_file_transfer方法要简单 从ASM拷贝到文件系统: 拷贝表空间 在拷贝表空间 ...

  5. C++继承与派生的概念、什么是继承和派生

    在C++中可重用性是通过继承(inheritance)这一机制来实现的.因此,继承是C++的一个重要组成部分. 前面介绍了类,一个类中包含了若干数据成员和成员函数.在不同的类中,数据成员和成员函数是不 ...

  6. HDU5765 Bonds 最小割极

    http://acm.hdu.edu.cn/showproblem.php?pid=5765 题意:无向连通图,问每条边在几个最小割极上 思路:用位压形式,表示边的关系.g[1<<i]=1 ...

  7. CSS布局基础

    (初级)css布局 一.单列布局1.基础知识块级元素 div p ul li dl dt 行级元素 img span input strong同一行显示.无换行2.盒子模型盒子模型 (边框border ...

  8. java.net.BindException: Address already in use: bind

    环境:jxse-2.7, netty-3.6.6.Final 现象:每次执行都抛出以下异常 八月 08, 2013 8:45:19 下午 net.jxta.logging.Logging logChe ...

  9. HDU 5724 Chess (sg函数)

    Chess 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5724 Description Alice and Bob are playing a s ...

  10. POJ 1258 Agri-Net (最小生成树)

    Agri-Net 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/H Description Farmer John has be ...