1088. Rational Arithmetic (20)

news/2025/11/23 17:33:27/文章来源:https://www.cnblogs.com/WJQ2017/p/19261043

1088. Rational Arithmetic (20)

#include <iostream>using namespace std;long long getsame(long long a, long long b)
{if(b != 0){return getsame(b, a % b);}else{return a;}
}void simplify(long long &a, long long &b)
{if(a == 0){b = 1;return;}if(b == 0) {a = 1;return;}int aflag = 1, bflag = 1;if(a < 0){a = -a;aflag = -1;}if(b < 0){b = -b;bflag = -1;}long long same = getsame(a, b);a /= same;b /= same;a *= aflag * bflag;
}void print(long long a, long long b)
{if(b == 0){printf("Inf");return;}if(a == 0){printf("0");return;}int flag = 0;if(a < 0){printf("(");flag = 1;}long long x = a / b;if(x != 0){printf("%lld", x);if(a % b == 0){if(flag == 1){printf(")");}return;}if(flag == 1){a = -a;}if(x < 0){x = -x;}printf(" ");}a -= x * b;printf("%lld/%lld", a, b);if(flag == 1){printf(")");}
}int main()
{long long a, b, c, d;scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);simplify(a, b);simplify(c, d);print(a, b);printf(" + ");print(c, d);printf(" = ");long long e = a * d + b * c, f = b * d;simplify(e, f);print(e, f);printf("\n");print(a, b);printf(" - ");print(c, d);printf(" = ");e = a * d - b * c;f = b * d;simplify(e, f);print(e, f);printf("\n");print(a, b);printf(" * ");print(c, d);printf(" = ");e = a * c;f = b * d;simplify(e, f);print(e, f);printf("\n");print(a, b);printf(" / ");print(c, d);printf(" = ");e = a * d;f = b * c;simplify(e, f);print(e, f);printf("\n");system("pause");return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/974076.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

1087. All Roads Lead to Rome (30)

1087. All Roads Lead to Rome (30)#include <iostream> #include <vector> #include <string.h>using namespace std;struct node {int next, cost; };vector<node> v[27000]; vector<int…

解码UDP

UDP 协议基础认知 UDP(User Datagram Protocol,用户数据报协议)是传输层核心协议之一,基于 IP 协议实现跨网络主机进程间的无连接数据传输。它面向事务提供简单通信服务,不保证数据交付、有序性和重复防护,也不提…

人工智能之数据分析 numpy:第六章 数组基本操作

人工智能之数据分析 numpy:第六章 数组基本操作人工智能之数据分析 numpy 第六章 数组基本操作@目录人工智能之数据分析 numpy前言一、修改数组形状(Reshaping)1. reshape()2. resize()3. ravel() 与 flatten()二、…

2025中山办公场地租赁优选:中山西区金嘉创新港,一站式创业空间,赋能企业成长新机遇

随着中山市产业升级与创新创业浪潮的蓬勃发展,优质办公空间已成为企业发展的重要基石。在2025年中山商业地产市场中,中山西区金嘉创新港凭借多元化的空间解决方案、完善的配套服务体系及卓越的区位优势,成为各类企业…

国产数据库替代MongoDB:政务电子证照新选择 - 教程

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

读书笔记《投资的未来》,估算收益率

比较IBM和新泽西标准石油两家公司,一个新兴的,受追捧的,一个传统的,但结果,是石油公司胜出。尽管两只股票的业绩都不错,但是1950~2003年,新泽西标准石油的投资者每年可以取得14.42%的年收益率,这比IBM提供的1…

使用代码查询快递信息的方法(与查询天气的方式雷同)

第一步:在标签中添加JS文件具体内容如下:第二步:写出大概的框架第三步:写JS部分(1)定义appkey和API地址(2)校验输入和显示加载状态(3)调用API和解析返回数据(4)展示拼接内容和判断内容是否正确第四步:保存并运行查…

1101. Quick Sort (25)

1101. Quick Sort (25)#include <iostream> #include <vector> #include <algorithm>using namespace std;int num[100010], low[100010], high[100010];int main() {int n;scanf("%d", &…

1100. Mars Numbers (20)

1100. Mars Numbers (20)#include <iostream> #include <string.h>using namespace std;char ch[2][13][5] = {"tret", "jan", "feb", "mar", "apr",…

解码网络编程基础

进程间通信方式 基础概念 程序是数据和指令的集合,运行时成为进程,操作系统会为其分配资源并记录参数。同一主机内进程通信可通过管道、信号、消息队列、信号量集、共享内存实现,这些方式依赖主机本地系统资源,无法…

C++的3种继承方式

C++的3种继承方式 在 C++ 中,继承方式(public、protected、private)决定了基类成员在派生类中的访问权限,以及派生类对象对基类成员的访问权限。正确选择继承方式是实现封装、复用和多态的关键。以下是三种继承方式…

1082. Read Number in Chinese (25)

1082. Read Number in Chinese (25)#include <iostream> #include <string.h>using namespace std;int first = 1;void setfirst() {if(first == 1){first = 0;}else{printf(" ");} }int main()…

1081. Rational Sum (20)

1081. Rational Sum (20)#include <iostream>using namespace std;long long getsame(long long a, long long b) {if(b != 0){return getsame(b, a % b);}else{return a;} }void simplify(long long &a, lo…

1067. Sort with Swap(0) (25)

1067. Sort with Swap(0) (25)#include <iostream>using namespace std;int index[100010], num[100010];int main() {int n;scanf("%d", &n);int i, count = 0;for(i = 0; i < n; i++){scanf(…

1066. Root of AVL Tree (25)

1066. Root of AVL Tree (25)#include <iostream> #include <stdlib.h>using namespace std;typedef struct node {int key, bf;struct node *lchild, *rchild; }*bnode;void rrotate(bnode *root) {bnode…

1070. Mooncake (25)

1070. Mooncake (25)#include <iostream> #include <algorithm>using namespace std;struct node {double amounts, prices, perprice; }mooncakes[1010];int cmp(node n1, node n2) {return n1.perprice …

1069. The Black Hole of Numbers (20)

1069. The Black Hole of Numbers (20)#include <iostream> #include <string.h> #include <algorithm>using namespace std;int cmp(char a, char b) {return a > b; }int main() {int num[2];s…

1050. String Subtraction (20)

1050. String Subtraction (20)#include <iostream> #include <string.h>using namespace std;int flag[130]; char s[2][10010];int main() {int i, len[2];for(i = 0; i <= 1; i++){gets(s[i]);len[i…

1049. Counting Ones (30)

1049. Counting Ones (30)#include <iostream>using namespace std;int main() {int n;scanf("%d", &n);int low, mid, high, d = 1, res = 0;while(n >= d){high = n / (d * 10);mid = (n - h…

1045. Favorite Color Stripe (30)

1045. Favorite Color Stripe (30)#include <iostream>using namespace std;int fav[210], ori[10010], res[210][10010];int getmax(int a, int b) {if(a > b){return a;}else{return b;} }int main() {int …