(spoj) SIMPLE


SUBMITTED BY: Guest

DATE: Feb. 11, 2014, 11:36 a.m.

FORMAT: C++

SIZE: 1.1 kB

HITS: 1239

  1. //Haha TTpro
  2. //SIMPLE spoj
  3. //http://www.spoj.com/problems/SIMPLE/
  4. #include <iostream>
  5. using namespace std;
  6. void inRation(long &a, long &b);
  7. void outRation(int Ca,long &a, long &b);
  8. int GCD(long a, long b);
  9. void MakeItSimple(long &a, long &b);
  10. int main()
  11. {
  12. int t;
  13. cin >>t;
  14. int i;
  15. long a,b;
  16. for (i=1;i<=t;i++)
  17. {
  18. inRation(a,b);
  19. MakeItSimple(a,b);
  20. outRation(i,a,b);
  21. }
  22. }
  23. void inRation(long &a, long &b)
  24. {
  25. cin >>a;
  26. cin.ignore(1);
  27. cin >>b;
  28. }
  29. void outRation(int Ca,long &a, long &b)
  30. {
  31. cout<<"Case "<<Ca<<": "<<a<<"/"<<b<<endl;
  32. }
  33. int GCD(long a, long b)
  34. {
  35. int c;
  36. if (a<b)
  37. {
  38. c=a;
  39. a=b;
  40. b=c;
  41. }
  42. int r;
  43. while (true)
  44. {
  45. r=a%b;
  46. if (r==0) return b;
  47. a=b;
  48. b=r;
  49. }
  50. }
  51. void MakeItSimple(long &a, long &b)
  52. {
  53. int G;
  54. G=GCD(a,b);
  55. a=a/G;
  56. b=b/G;
  57. }

comments powered by Disqus