Code Monkey home page Code Monkey logo

acmer's People

Watchers

 avatar  avatar

acmer's Issues

PAT 1034. 有理数四则运算(20)

本题要求编写程序,计算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

解题:简单的分数运算,可能会出错的地方是如果没有用long long可能在第三四的数据点WA,此外此题要用到辗转相除法,否则会在第四个检测点超时。

代码:

include

include

include<string.h>

using namespace std;

long long gcd(long a, long b)
{
if (b == 0) return a;
return gcd(b, a%b);
}

void deal(long long a, long long b)
{
if (b == 0) cout << "Inf";
else if (a%b == 0)
{
if (a / b >= 0)
{
cout << a / b;
}
else cout << "(" << a / b << ")";
}
else
{
long long fu = a / b;
a -= fu*b;
long long m = a, n = b;
if (m < 0) m = 0 - m;
if (n < 0) n = 0 - n;
long long s = gcd(m, n);
a /= s;
b /= s;
if (fu < 0)
{
if (a < 0) a = 0 - a;
cout << "(" << fu << " " << a << "/" << b << ")";
}
else if (fu != 0)
{
if (a < 0) a = 0 - a;
cout << fu << " " << a << "/" << b;
}
else
{
if (b < 0)
{
b = 0 - b;
a = 0 - a;
}
if (a < 0)
{
cout << "(" << a << "/" << b << ")";
}
else
cout << a << "/" << b;
}
}
}

int main()
{
char q = 0;
long long a, b, c, d;
cin >> a >> q >> b >> c >> q >> d;
long long jin1 = 0, jin2 = 0;
char s[4] = { '+','-','_','/' };
for (long long i = 0; i< 4; i++)
{
deal(a, b);
cout << " " << s[i] << " ";
deal(c, d);
cout << " = ";
switch (i)
{
case 0:
deal(a_d + c_b, b_d);
cout << endl;
break;
case 1:
deal(a_d - c_b, b_d);
cout << endl;
break;
case 2:
deal(a_c, b_d);
cout << endl;
break;
case 3:
if (c < 0)
{
a = 0 - a;
c = 0 - c;
}
deal(a_d, b*c);
break;
}
}

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.