about summary refs log tree commit diff
path: root/2ndary/09/TP-HN-2014/cau1.cpp
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2020-06-06 21:33:13 +0700
committerNguyễn Gia Phong <mcsinyx@disroot.org>2020-06-06 21:33:13 +0700
commit2f674dc80f0382f1c3178f435714960734dc9d3c (patch)
tree2abba7e4ec72bd16f58f7375126144d3fd9f4bca /2ndary/09/TP-HN-2014/cau1.cpp
parentb2d80610db6beda38573890ed169815e495bc663 (diff)
downloadcp-2f674dc80f0382f1c3178f435714960734dc9d3c.tar.gz
Reorganize stuff from secondary school
Diffstat (limited to '2ndary/09/TP-HN-2014/cau1.cpp')
-rw-r--r--2ndary/09/TP-HN-2014/cau1.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/2ndary/09/TP-HN-2014/cau1.cpp b/2ndary/09/TP-HN-2014/cau1.cpp
new file mode 100644
index 0000000..274cef6
--- /dev/null
+++ b/2ndary/09/TP-HN-2014/cau1.cpp
@@ -0,0 +1,51 @@
+#include <iostream>
+#include <fstream>
+
+using namespace std;
+
+long
+gcd(long x, long y)
+{
+  long z;
+  while (y)
+    {
+      z = x;
+      x = y;
+      y = z % x;
+    }
+
+  return x;
+}
+
+int
+main()
+{
+  ifstream infile;
+  long a, b, c, d;
+  infile.open("CAU1.INP");
+  infile >> a >> b >> c >> d;
+  infile.close();
+
+  long y = b * d / gcd(b, d);
+  long x = a * y / b - c * y / d;
+  if (!x)
+    y = 1;
+  else
+    {
+      a = gcd(x, y);
+      x /= a;
+      y /= a;
+      if (y < 0)
+        {
+          x *= -1;
+          y *= -1;
+        }
+    }
+
+  ofstream outfile;
+  outfile.open("CAU1.OUT");
+  outfile << x << ' ' << y << endl;
+  outfile.close();
+
+  return 0;
+}