首页 > 其他分享 >北大ACM poj3979 分数加减法

北大ACM poj3979 分数加减法

时间:2023-08-21 17:07:30浏览次数:38  
标签:gcd int fz poj3979 ACM 加减法 fm op


分数加减法


Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 10041

 

Accepted: 3241


Description


编写一个C程序,实现两个分数的加减法


Input


输入包含多行数据
每行数据是一个字符串,格式是"a/boc/d"。

其中a, b, c, d是一个0-9的整数。o是运算符"+"或者"-"。

数据以EOF结束
输入数据保证合法


Output


对于输入数据的每一行输出两个分数的运算结果。
注意结果应符合书写习惯,没有多余的符号、分子、分母,并且化简至最简分数


Sample Input


1/8+3/8 1/4-1/2 1/3-1/3


Sample Output


1/2 -1/4 0


 

 

#include<stdio.h>
#include<math.h>
int gcd(int a,int b)
{
	if(b==0)return a;
	else return gcd(b,a%b);
}
main()
{
	int a,b,c,d,g,fz,fm;
	char op;
	while(~scanf("%d/%d%c%d/%d",&a,&b,&op,&c,&d))
	{
		fz=op=='+' ? a*d+b*c : a*d-b*c;
		fm=b*d;
		if(fz%fm==0||fz==0) printf("%d\n",fz/fm);
		else
		{
			g=gcd(fabs(fz),fm);
			printf("%d/%d\n",fz/g,fm/g);	
		}	
	}
}

 

标签:gcd,int,fz,poj3979,ACM,加减法,fm,op
From: https://blog.51cto.com/u_10101161/7177235

相关文章

  • 北大ACM poj3589 Number-guessing Game
    Number-guessingGameTimeLimit:1000MS MemoryLimit:65536KTotalSubmissions:5805 Accepted:4204DescriptionLarrylikesplayingthenumber-guessinggame.Twoplayersareneededinagame.SupposetheyareXandY,andXpresentsanumberforYtogu......
  • 北大ACM poj3913 Gnome Sequencing
    GnomeSequencingTimeLimit:1000MS MemoryLimit:65536KTotalSubmissions:1267 Accepted:865DescriptionInthebookAllCreaturesofMythology,gnomesarekind,beardedcreatures,whilegoblinstendtobebossyandsimple-minded.Thegoblinslike......
  • 北大ACM poj3994 Probability One
    ProbabilityOneTimeLimit:1000MS MemoryLimit:65536KTotalSubmissions:938 Accepted:660DescriptionNumberguessingisapopulargamebetweenelementary-schoolkids.Teachersencouragepupilstoplaythegameasitenhancestheirarithmeticski......
  • 北大ACM poj3750 小孩报数问题
    小孩报数问题TimeLimit:1000MS MemoryLimit:65536KTotalSubmissions:7233 Accepted:3454Description有N个小孩围成一圈,给他们从1开始依次编号,现指定从第W个开始报数,报到第S个时,该小孩出列,然后从下一个小孩开始报数,仍是报到S个出列,如此重复下去,直到所有的小孩都......
  • 北大ACM poj1002 487-3279
    487-3279TimeLimit:2000MS MemoryLimit:65536KTotalSubmissions:191845 Accepted:33280DescriptionBusinessesliketohavememorabletelephonenumbers.Onewaytomakeatelephonenumbermemorableistohaveitspellamemorablewordorphrase.......
  • 北大ACM poj1661 Help Jimmy
    HelpJimmyTimeLimit:1000MS MemoryLimit:10000KTotalSubmissions:7380 Accepted:2333Description"HelpJimmy"是在下图所示的场景上完成的游戏。场景中包括多个长度和高度各不相同的平台。地面是最低的平台,高度为零,长度无限。Jimmy老鼠在时刻0从高......
  • 北大ACM poj1050 To the Max(C++)
    TotheMaxTimeLimit:1000MS MemoryLimit:10000KTotalSubmissions:32446 Accepted:16930DescriptionGivenatwo-dimensionalarrayofpositiveandnegativeintegers,asub-rectangleisanycontiguoussub-arrayofsize1*1orgreaterlocatedwithi......
  • 北大ACM poj2141 Message Decowding
    MessageDecowdingTimeLimit:1000MS MemoryLimit:65536KTotalSubmissions:10326 Accepted:5672DescriptionThecowsarethrilledbecausethey'vejustlearnedaboutencryptingmessages.Theythinktheywillbeabletousesecretmessagestoplot......
  • 北大ACM poj2590
    /*找规律后发现增量是1,1,2,2,3,3,4,4,5,5......,可以发现它是个隔位曾1的自然序列步数:12345678910+1+1+2+2+3+3+4+4+5+501246912......
  • 北大ACM poj1636
    /*problem:poj1636如果s中的每个字母在t中都有则输出Yes(注意是Yes,不是YES)代码很简单,就不解释了*/#include<stdio.h>chars[100010],t[100010];intmain(){while(~scanf("%s%s",s,t)){inti=0,j;for(j=0......