首页 > 其他分享 >PAT Basic 1034. 有理数四则运算

PAT Basic 1034. 有理数四则运算

时间:2023-03-20 11:33:07浏览次数:45  
标签:PAT num1 num2 int res long num Basic 1034

PAT Basic 1034. 有理数四则运算

1. 题目描述:

本题要求编写程序,计算 2 个有理数的和、差、积、商。

2. 输入格式:

输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分母不为 0。

3. 输出格式:

分别在 4 行中按照 有理数1 运算符 有理数2 = 结果 的格式顺序输出 2 个有理数的和、差、积、商。注意输出的每个有理数必须是该有理数的最简形式 k a/b,其中 k 是整数部分,a/b 是最简分数部分;若为负数,则须加括号;若除法分母为 0,则输出 Inf。题目保证正确的输出中没有超过整型范围的整数。

4. 输入样例:

2/3 -4/2
5/3 0/6

5. 输出样例:

2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)
1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf

6. 性能要求:

Code Size Limit
16 KB
Time Limit
200 ms
Memory Limit
64 MB

思路:

加减乘除运算本身通过整型运算得出结果即可,本题主要涉及到输出的处理。题目要求输出的每个分数必须是最简形式,这里定义了子函数void toSimplest(long num, long denomi)用于输出最简形式的分数,另外定义了四个子函数分别处理加减乘除。

其中分数的整数部分直接通过整型除法得出,主要问题在于真分数的处理,这里要把分子分母的最大公约数消去才能得到最简形式,所以定义了long getGcd(long num1, long num2)用于计算最大公约数,涉及到更相减损法和辗转相除法。

最后就是虽然题目保证分子分母和正确的输出没有超过整型范围的整数,但是运算过程中涉及到两数相乘,所以这里还是需要定义为long类型。

My Code:

#include <stdio.h>

// void toSimplest(int num, int denomi);
// void sum2rational(int num1, int den1, int num2, int den2);
// void minus2rational(int num1, int den1, int num2, int den2);
// void multi2rational(int num1, int den1, int num2, int den2);
// void divid2rational(int num1, int den1, int num2, int den2);
void toSimplest(long num, long denomi);
void sum2rational(long num1, long den1, long num2, long den2);
void minus2rational(long num1, long den1, long num2, long den2);
void multi2rational(long num1, long den1, long num2, long den2);
void divid2rational(long num1, long den1, long num2, long den2);
long getGcd(long num1, long num2);

int main(void)
{
    //toSimplest(-1, 3);
//     int num1, den1, num2, den2;
    long num1, den1, num2, den2;
    
    scanf("%ld/%ld%ld/%ld", &num1, &den1, &num2, &den2);
//     scanf("%d/%d%d/%d", &num1, &den1, &num2, &den2);
    //printf("%d/%d %d/%d\n", num1, den1, num2, den2); // test input correct
    
    sum2rational(num1, den1, num2, den2);
    minus2rational(num1, den1, num2, den2);
    multi2rational(num1, den1, num2, den2);
    divid2rational(num1, den1, num2, den2);
    
    return 0;
}

void toSimplest(long num, long denomi) //(int num, int denomi)
{
    long k = num/denomi; // the integer's part
    long mod = num % denomi; // the numerator of simplest form
    long i, gcd;
//     int k = num/denomi; // the integer's part
//     int mod = num % denomi; // the numerator of simplest form
//     int i, gcd;
    
    if(num < 0) // the fraction is negative
    {
        if(k) // k exist
        {
            //printf("(-%d", k);
            printf("(%ld", k);
            if(mod) // it's a real fraction, mod is negative for num is negative
            {
//                 for(i = -mod; i>=2; i--)
//                 {
//                     if((-mod % i == 0) && (denomi % i == 0))
//                     {
//                         gcd = i;
//                         break;
//                     }
//                 }         
//                 if(i>=2) {mod/=gcd; denomi/=gcd;} //this assure simplest form of fraction.
                if(mod != -1)
                {
                    gcd = getGcd(-mod, denomi);
                    mod /= gcd;
                    denomi /= gcd; 
                }
                
                printf(" %ld/%ld)", -mod, denomi); //this can't assure simplest form
            }
            else // just have integer part
                printf(")");
        }
        else // k not exist, it must a real fraction
        {
//             for(i = -num; i>=2; i--)
//             {
//                 if((-num % i == 0) && (denomi % i == 0))
//                 {
//                     gcd = i;
//                     break;
//                 }
//             }         
//             if(i>=2) {num/=gcd; denomi/=gcd;} //this assure simplest form of fraction
            if(num != -1)
            {
                gcd = getGcd(-num, denomi);
                num /= gcd;
                denomi /= gcd;                
            }
            
            printf("(%ld/%ld)", num, denomi);
        }
    }
    else if(num > 0)// the fraction is positive
    {
        if(k) // k exist
        {
            printf("%ld", k);
            if(mod) // it's a real fraction
            {
//                 for(i = mod; i>=2; i--)
//                 {
//                     if((mod % i == 0) && (denomi % i == 0))
//                     {
//                         gcd = i;
//                         break;
//                     }
//                 }         
//                 if(i>=2) {mod/=gcd; denomi/=gcd;} //this assure simplest form of fraction
                if(mod != 1)
                {
                    gcd = getGcd(mod, denomi);
                    mod /= gcd;
                    denomi /= gcd;                    
                }
                
                printf(" %ld/%ld", mod, denomi);
            }
        }
        else // k not exist, it must a real fraction
        {
//             for(i = num; i>=2; i--)
//             {
//                 if((num % i == 0) && (denomi % i == 0))
//                 {
//                     gcd = i;
//                     break;
//                 }
//             }         
//             if(i>=2) {num/=gcd; denomi/=gcd;} //this assure simplest form of fraction
            if(num != 1)
            {
                gcd = getGcd(num, denomi);
                num /= gcd;
                denomi /= gcd;                
            }
            
            printf("%ld/%ld", num, denomi);
        }
    }
    else // num == 0, first submit neglect this point, casue output format incorrect
    {
        printf("0");
    }
}

void sum2rational(long num1, long den1, long num2, long den2)//(int num1, int den1, int num2, int den2)
{
//     int res_num, res_den;
    long res_num, res_den;
    
    res_num = num1*den2 + den1*num2;
    res_den = den1*den2;
    
    toSimplest(num1, den1);
    printf(" + ");
    toSimplest(num2, den2);
    printf(" = ");
    toSimplest(res_num, res_den);
    printf("\n");
}

void minus2rational(long num1, long den1, long num2, long den2)//(int num1, int den1, int num2, int den2)
{
//     int res_num, res_den;
    long res_num, res_den;
    
    res_num = num1*den2 - den1*num2;
    res_den = den1*den2;
    
    toSimplest(num1, den1);
    printf(" - ");
    toSimplest(num2, den2);
    printf(" = ");
    toSimplest(res_num, res_den);
    printf("\n");
}

void multi2rational(long num1, long den1, long num2, long den2)//(int num1, int den1, int num2, int den2)
{
//     int res_num, res_den;
    long res_num, res_den;
    
    res_num = num1*num2;
    res_den = den1*den2;
    
    toSimplest(num1, den1);
    printf(" * ");
    toSimplest(num2, den2);
    printf(" = ");
    toSimplest(res_num, res_den);
    printf("\n");
}

void divid2rational(long num1, long den1, long num2, long den2)//(int num1, int den1, int num2, int den2)
{
//     int res_num, res_den;
    long res_num, res_den;
    
    res_num = num1*den2;
    res_den = den1*num2;
    
    if(res_den < 0) // to assure the result's denominator sign is postive
    {
        res_den = -res_den;
        res_num = -res_num;
    }
    
    toSimplest(num1, den1);
    printf(" / ");
    toSimplest(num2, den2);
    printf(" = ");
    if(res_den)
        toSimplest(res_num, res_den);
    else
        printf("Inf");
    printf("\n");
}

long getGcd(long num1, long num2) // get the greatest common divisor
{
//     num1 = (num1>0) ? num1 : -num1;
//     num2 = (num2>0) ? num2 : -num2;
    
//     while(num1 != num2)
//     {
//         if(num1 > num2)
//             num1 -= num2;
//         else
//             num2 -= num1;
//     }
//     return num1;
    long temp;
    
    while(temp = num1 % num2) // testpoint3 throw timeout, here use zhanzhuanxiangchu to pass testpoint3
    {
        num1 = num2;
        num2 = temp;
    }
    
    return num2;
}

标签:PAT,num1,num2,int,res,long,num,Basic,1034
From: https://www.cnblogs.com/tacticKing/p/17235737.html

相关文章

  • matlab genpath命令 查看搜索路径
    在命令窗口中输入genpath命令,可以得到MATLAB所有的搜索路径首尾连接而成的一个长字符串。示例:>>genpathans=D:\R2009a\toolbox;D:\R2009a\toolbox\aero;D:\R2009a\tool......
  • pat 乙级1031 查验身份证
    1#include<stdio.h>2#include<stdlib.h>3#include<string.h>4#include<math.h>56intmain()7{8intn;9scanf("%d",&n);10c......
  • 2023 ICPC香港区域赛(UCup) D Shortest Path Query
    啊对对对,下次题解写详细一点好不好。首先考虑naive的\(O(n^2)\),记\(dp[i][j]\)表示从\(1\)走到\(i\),恰好走了\(j\)条黑边的时候走过白边的最少数量。\(O(nm)\)......
  • pat 乙级 1027 打印沙漏
    ac但写得就像坨答辩过两天我自己都忘了这些变量用来干嘛的了1#include<stdio.h>2#include<stdlib.h>3#include<string.h>4#include<math.h>56int......
  • c-basic
    title:C语言教程tags:Ccategories:C语言abbrlink:a964date:2023-03-1523:45:43C语言教程前言C语言特性C语言的设计C语言具有高效性C语言具有可移......
  • PATH
    Path环境变量的作用它提供了windows命令行中指令的可执行文件(比如:.exe文件)路径,让我们在命令行中输入命令时,能够找到对应的可执行文件执行简单说:让命令在命令行中......
  • xpath定位方法
    一.常用定位方法1.根据文本值定位元素查找文本值为DNS的div元素text1=html.xpath("//div[text()='DNS']")text2=html.xpath("//div[text()='DNS']/text()")#获......
  • PAT Basic 1033. 旧键盘打字
    PATBasic1033.旧键盘打字1.题目描述:旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及坏掉的那些键,打出的结果文字......
  • PAT Basic 1031. 查验身份证
    PATBasic1031.查验身份证1.题目描述:一个合法的身份证号码由17位地区、日期编号和顺序编号加1位校验码组成。校验码的计算规则如下:首先对前17位数字加权求和,权重分配......
  • PAT 甲级 1012 The Best Rank(25)
    ToevaluatetheperformanceofourfirstyearCSmajoredstudents,weconsidertheirgradesofthreecoursesonly:C-CProgrammingLanguage,M-Mathematics......