69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#define ADS(n) ((n)>=0)?(n):(-n)
int count = 0;
int first_num = 0, second_num = 0, first_no = 0, second_no = 0;
int gcd(int m,int n)
{
if (m < n)
{
int tmp = m;
m = n;
n = tmp;
}
if (n == 0)
return m;
else
return gcd(n,m % n);
}
int gbs(int m,int n)
{
if (m < n)
{
int tmp = m;
m = n;
n = tmp;
}
return m*n/gcd(m,n);
}
void go_count(void)
{
int result_num,result_no;
result_no = gbs(first_no, second_no);
//printf("\n");
if(1 == count)
{
result_num = first_num * result_no / first_no + second_num * result_no / second_no;
if(result_num != 0)
{
printf("%d/%d\n", result_num / gcd(result_num, result_no), result_no / gcd(result_num, result_no));
}
else
{
printf("0\n");
}
}
else if(2 == count)
{
result_num = first_num * result_no / first_no - second_num * result_no / second_no;
if(result_num != 0)
{
printf("%d/%d\n", result_num / gcd(ADS(result_num), ADS(result_no)), result_no / gcd(ADS(result_num), ADS(result_no)));
}
else
{
printf("0\n");
}
}
}
int main(void)
{
int ch;
int num = 1;
while((ch = getchar()) != EOF)
{
if(ch == '+')
{
count = 1;
}
else if(ch == '-')
{
count = 2;
}
else if(ch >= '0' && ch <= '9')
{
if(1 == num)
{
first_num = ch - 48;
num++;
}
else if(2 == num)
{
first_no = ch - 48;
num++;
}
else if(3 == num)
{
second_num = ch -48;
num++;
}
else if(4 == num)
{
second_no = ch - 48;
num++;
}
}
else if(ch == '\n')
{
num = 1;
go_count();
}
}
return 0;
}
#include <stdio.h>
#include <string.h>
char s[8];
int gcd(int a, int b) {
return b % a ? gcd(b%a, a) : a;
}
int main(void) {
int a1, a2, b1, b2;
char op;
int t, c1, c2;
while(gets(s) != NULL) {
a1 = s[0] - '0';
b1 = s[2] - '0';
op = s[3];
a2 = s[4] - '0';
b2 = s[6] - '0';
if(op == '+') {
c1 = a1 * b2 + a2 * b1;
c2 = b1 * b2;
if(c1 == 0) {
printf("0\n");
} else {
t = gcd(c1, c2);
// printf("gcd %d %d %d \n", t, c1, c2);
if(c2 == t) {
printf("%d\n", c1 / t);
} else {
printf("%d/%d\n", c1/t, c2/t);
}
}
} else {
c1 = a1 * b2 - a2 * b1;
c2 = b1 * b2;
if(c1 == 0) {
printf("0\n");
} else if(c1 < 0) {
c1 = -1 * c1;
t = gcd(c1, c2);
if(c2 == t) {
printf("-%d\n", c1 / t);
} else {
printf("-%d/%d\n", c1/t, c2/t);
}
} else {
t = gcd(c1, c2);
if(c2 == t) {
printf("%d\n", c1 / t);
} else {
printf("%d/%d\n", c1/t, c2/t);
}
}
}
}
return 0;
}
1/8+3/8
1/4-1/2
1/3-1/3
1/1+1/1
1/1+0/1
1/1-0/1
1/2+1/2
1/2
-1/4
0
2
1
1
1