码迷,mamicode.com
首页 > 其他好文 > 详细

1081. Rational Sum (20)

时间:2015-12-06 12:50:50      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

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

Input Specification:

Each input file contains one test case. Each case starts with a positive integer N (<=100), followed in the next line N rational numbers "a1/b1 a2/b2 ..." where all the numerators and denominators are in the range of "long int". If there is a negative number, then the sign must appear in front of the numerator.

Output Specification:

For each test case, output the sum in the simplest form "integer numerator/denominator" where "integer" is the integer part of the sum, "numerator" < "denominator", and the numerator and the denominator have no common factor. You must output only the fractional part if the integer part is 0.

Sample Input 1:
5
2/5 4/15 1/30 -2/60 8/3
Sample Output 1:
3 1/3
Sample Input 2:
2
4/3 2/3
Sample Output 2:
2
Sample Input 3:
3
1/3 -1/6 1/8
Sample Output 3:
7/24

 

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <math.h>
  6. #pragma warning(disable:4996)
  7. using namespace std;
  8. long long int GongYue(long long int a, long long int b) {
  9. if (a == 0 || b == 0)
  10. return 1;
  11. if (a == b)
  12. return b;
  13. else if (a > b) {
  14. int m = a%b;
  15. while (m != 0) {
  16. a = b;
  17. b = m;
  18. m = a%b;
  19. }
  20. return b;
  21. }
  22. else {
  23. int m = b%a;
  24. while (m != 0) {
  25. b = a;
  26. a = m;
  27. m = b%a;
  28. }
  29. return a;
  30. }
  31. }
  32. vector<long long int> fenzi, fenmu;
  33. int main(void) {
  34. int n;
  35. cin >> n;
  36. char temp[50];
  37. long long int fenzi4push,fenmu4push;
  38. for (int i = 0; i < n; i++) {
  39. scanf("%s", temp);
  40. sscanf(temp, "%lld/%lld", &fenzi4push, &fenmu4push);
  41. if (fenzi4push == 0)
  42. continue;
  43. long long int yuefen = GongYue(abs(fenzi4push), abs(fenmu4push));
  44. fenzi4push /= yuefen;
  45. fenmu4push /= yuefen;
  46. fenzi.push_back(fenzi4push);
  47. fenmu.push_back(fenmu4push);
  48. }
  49. n = fenzi.size();
  50. if (fenzi.size() == 0) {
  51. cout << "0";
  52. return 0;
  53. }
  54. long long int fenziResult=fenzi[0], fenmuResult=fenmu[0];
  55. for (int i = 1; i < n; i++) {
  56. if (fenmuResult == fenmu[i]) {
  57. fenziResult += fenzi[i];
  58. long long int yueshu = GongYue(abs(fenmuResult), abs(fenziResult));
  59. fenziResult /= yueshu;
  60. fenmuResult /= yueshu;
  61. }
  62. else {
  63. fenziResult = fenziResult*fenmu[i]+fenmuResult*fenzi[i];
  64. fenmuResult = fenmuResult*fenmu[i];
  65. long long int yueshu = GongYue(abs(fenmuResult), abs(fenziResult));
  66. fenziResult /= yueshu;
  67. fenmuResult /= yueshu;
  68. }
  69. }
  70. if (fenziResult*fenmuResult<0) {
  71. fenziResult = abs(fenziResult);
  72. fenmuResult = abs(fenmuResult);
  73. cout << "-";
  74. }
  75. if (fenziResult % fenmuResult == 0)
  76. cout << fenziResult / fenmuResult;
  77. else if (fenziResult > fenmuResult)
  78. cout << fenziResult / fenmuResult << " " << fenziResult % fenmuResult << "/" << fenmuResult;
  79. else
  80. cout << fenziResult << "/" << fenmuResult;
  81. return 0;
  82. }





1081. Rational Sum (20)

标签:

原文地址:http://www.cnblogs.com/zzandliz/p/5023260.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!