1034. 有理數四則運算(20)
時間限制
200 ms
內存限制
65536 kB
代碼長度限制
8000 B
判題程序
Standard
作者
CHEN, Yue
本題要求編寫程序,計算2個有理數的和、差、積、商。
輸入格式:
輸入在一行中按照“a1/b1 a2/b2”的格式給出兩個分數形式的有理數,其中分子和分母全是整型范圍內的整數,負號只可能出現在分子前,分母不為0。
輸出格式:
分別在4行中按照“有理數1 運算符 有理數2 = 結果”的格式順序輸出2個有理數的和、差、積、商。注意輸出的每個有理數必須是該有理數的最簡形式“k a/b”,其中k是整數部分,a/b是最簡分數部分;若為負數,則須加括號;若除法分母為0,則輸出“Inf”。題目保證正確的輸出中沒有超過整型范圍的整數。
輸入樣例1:
2/3 -4/2
輸出樣例1:
2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)
輸入樣例2:
5/3 0/6
輸出樣例2:
1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf
### 分析(偽代碼和知識點)
### 代碼
```
#include<stdio.h>
int gong(long a,long b)
{
long r,min,max;
min=(a>b)?b:a;
max=(a>b)?a:b;
r=max%min;
while(r)
{
max=min;
min=r;
r=max%min;
}
return min;
}
void print(long a,long b)
{
long flag=0,c,d,e;
if(a<0){
a=-a;
flag=1;
}
if(a==0) printf("0");
else{
e=gong(a,b);
a/=e;
b/=e;
c=a/b;
d=a%b;
if(flag==0){
if(d==0) printf("%lld",c);
else{
if(c==0) printf("%lld/%lld",a,b);
else printf("%lld %lld/%lld",c,d,b);
}
}
else{
if(d==0) printf("(-%lld)",c);
else{
if(c==0) printf("(-%lld/%lld)",a,b);
else printf("(-%lld %lld/%lld)",c,d,b);
}
}
}
}
void add(long a1,long b1,long a2,long b2)
{
print(a1,b1);
printf(" + ");
print(a2,b2);
printf(" = ");
print(a1*b2+a2*b1,b1*b2);
printf("\n");
}
void minus(long a1,long b1,long a2,long b2)
{
print(a1,b1);
printf(" - ");
print(a2,b2);
printf(" = ");
print(a1*b2-a2*b1,b1*b2);
printf("\n");
}
void muilt(long a1,long b1,long a2,long b2)
{
print(a1,b1);
printf(" * ");
print(a2,b2);
printf(" = ");
print(a1*a2,b1*b2);
printf("\n");
}
void divide(long a1,long b1,long a2,long b2)
{
print(a1,b1);
printf(" / ");
print(a2,b2);
printf(" = ");
if(a2==0) printf("Inf\n");
else{
if(a2<0){
a2=-a2;
a1=-a1;
}
print(a1*b2,b1*a2);
printf("\n");
}
}
int main()
{
long a1,b1,a2,b2;
while(~scanf("%lld/%lld %lld/%lld",&a1,&b1,&a2,&b2))
{
add(a1,b1,a2,b2);
minus(a1,b1,a2,b2);
muilt(a1,b1,a2,b2);
divide(a1,b1,a2,b2);
}
return 0;
}
```
- 大賽說明
- PAT乙集題庫
- 模板
- 1001.害死人的3n+1 c
- 1002.寫出這個數 c 02
- 1003.我要通過(20)c03
- 1004.成績排名 C 04
- 1005.繼續(3n+1)猜想 (25) c05
- 1006. 換個格式輸出整數 (15) c06
- 1007.素數對猜想 (20) C 07
- 1008. 數組元素循環右移問題 (20) c08
- 1009.說反話(20)c09
- 1010.一元多項式求導 (25) c10
- 1011. A+B和C(15)c11
- 1012.數字分類 C12
- 1013.數素數(20) C 13
- 1014.福爾摩斯的約會(20)c 14
- 1015.德才論c15
- 1016.部分A+B C 16
- 1018.錘子剪刀布c18
- 1023 組個最小數(20) C23
- 1024 科學計數法(20) C24
- 1025.反轉鏈表(25)C25
- 1026.程序運行時間(15)c 26
- 1027. 打印沙漏(20) C 27
- 1028 人口普查(20) C 01
- 1029. 舊鍵盤(20) C 01
- 1030.完美數列 C 04
- 1031. 查驗身份證(15) c05
- 1032. 挖掘機技術哪家強(20) c06
- 1033. 舊鍵盤打字(20) C 07
- 1034. 有理數四則運算 08
- 1035.插入和歸并 C 09
- 1036.跟奧巴馬一起編程(15)C18
- 1037.在霍格沃茨找零錢C 11
- 1039.到底買不買 C 13
- 1041.考試座位號(15)C03
- 1042。字符統計 C16
- 1044.火星數字 C 15
- 1046. 劃拳(15) C 01
- 1047.編程團體賽C18
- 1051.復數乘法(15) c24
- 1054. 求平均值 (20) C
- 1036. 跟奧巴馬一起編程(15) c10
- 1055.集體照 C 09
- 1050. 螺旋矩陣
- C語言
- assert.h
- stdio.h
- Operations on files
- File access
- Formatted input/output
- Character input/output
- Direct input/output
- File positioning
- Error-handling
- Macros
- 數據結構
- 算法
- 5月8日測試題目
- 喬棟
- 01申彥棟
- 02方常吉
- 03王永明
- 04劉果靜
- 05原曉曉
- 09牛錦江
- 10李澤路
- 11葛建輝
- 12程才耀
- 13王旭昕
- 16田鵬
- 15李新奇
- 24王翡
- 18高捷
- 07耿生年
- 05王進
- 25閆鑫炎
- 5月9日測試題目
- 熱身題庫
- L1-1
- L1-2
- L1-3
- L1-4
- L1-5
- L1-6
- L1-8
- L1-7
- L2-1
- L2-2
- L2-3
- L2-4
- L3-1
- L3-2
- L3-3