Skip to content

Instantly share code, notes, and snippets.

@rkotov93
Created August 30, 2011 22:31
Show Gist options
  • Save rkotov93/1182287 to your computer and use it in GitHub Desktop.
Save rkotov93/1182287 to your computer and use it in GitHub Desktop.
olymp2007_ariph
#include <stdio.h>
#include <malloc.h>
void sum(int, int, int, int, long long*);
void multiplication(int, int, int, int, long long*);
int lcm(int, int); // lowest common multiple
int main() {
int p1, q1, p2, q2;
long long* ans = (long long*)malloc(2*sizeof(long long)); // answer
char sign;
int i = 0;
int commondivisor;
freopen("input.txt", "r", stdin);
scanf("%d/%d%c%d/%d", &p1, &q1, &sign, &p2, &q2);
switch (sign) {
case '+':
sum(p1, q1, p2, q2, ans);
break;
case '-':
sum(p1, q1, -p2, q2, ans);
break;
case '*':
multiplication(p1, q1, p2, q2, ans);
break;
case ':':
multiplication(p1, q1, q2, p2, ans);
break;
}
//printf("%lld/%lld\n", ans[0], ans[1]);
commondivisor = abs(ans[0]*ans[1]) / lcm(ans[0], ans[1]);
printf("%d\n", commondivisor);
//if (ans[1] < 0) {ans[0] = -ans[0]; ans[1] = -ans[1];}
ans[0] /= commondivisor;
ans[1] /= commondivisor;
printf("%lld/%lld\n", ans[0], ans[1]);
freopen("output.txt", "w", stdout);
printf("%lld/%lld\n", ans[0], ans[1]);
}
int lcm(int x, int y) {
int i = ((x >= y) ? x : y);
for (i; i < abs(x*y); i++)
if ((i % x == 0) && (i % y == 0)) {
printf("lcm - %d\n", i);
return i;
}
printf("lcm - %d\n", x*y);
return x*y;
}
void sum(int p1, int q1, int p2, int q2, long long* ans) {
int commonmultiple = lcm(q1, q2);
ans[1] = commonmultiple;
ans[0] = (long long)p1*commonmultiple/q1 + p2*commonmultiple/q2;
}
void multiplication(int p1, int q1, int p2, int q2, long long* ans) {
ans[0] = p1*p2;
ans[1] = q1*q2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment