zoukankan      html  css  js  c++  java
  • 结构-02. 有理数加法(15)

    本题要求编写程序,计算两个有理数的和。

    输入格式:

    输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整形范围内的正整数。

    输出格式:

    在一行中按照“a/b”的格式输出两个有理数的和。注意必须是该有理数的最简分数形式,若分母为1,则只输出分子。

    输入样例1:

    1/3 1/6
    

    输出样例1:

    1/2
    

    输入样例2:

    4/3 2/3
    

    输出样例2:

    2

    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <string.h>
    #include <string>
    #include <math.h>
    
    
    using namespace::std; 
    
    int main(){
        char s[100];
        gets(s);
        //划分
        char *p,*q;
        p=strtok(s," "); 
        q=strtok(NULL," ");
        int a,b,c,d;
        sscanf(p,"%d/%d",&a,&b);
        sscanf(q,"%d/%d",&c,&d);
        int m=a*d+c*b;
        int n=b*d;
        int yueshu;
        int flag=0;
        if(m%n==0)
        {
            printf("%d",m/n);
        } else
        {
            //求最大公约数 
            
            for(int i=1;i<=m&&i<=n;i++)
            {
                if(m%i==0&&n%i==0)
                {
                yueshu=i;
                flag=1;    
                }
            } 
            if(flag==1)
            {
                printf("%d/%d",m/yueshu,n/yueshu);
            }else
            {
                    printf("%d/%d",m,n);
            }
        }
        
        
        return 0;
    }
  • 相关阅读:
    会跳舞的树(只用HTML+CSS)(转)
    国内UED收录
    HDU 1078 dfs+dp
    HDU 1278
    HDU 4499
    HDU 4597
    POJ2777
    POJ1780 Code
    简单的Fleury算法模板
    POJ 2513 无向欧拉通路+字典树+并查集
  • 原文地址:https://www.cnblogs.com/ligen/p/4291143.html
Copyright © 2011-2022 走看看