UVA, 332 Rational Numbers from Repeating Fractions

题意 :一致一个循环小数。求他的分数表达形式。读题有比较大的难度。很长时间时间才读懂。记住。题目中往往含有完整的示例信息。


#include <cstdio>

#include <string.h>

#include <cstdlib>

#include <math.h>

#include <iostream>

using namespace std;

int fun(int a, int b)

{

    return a%b?fun(b, a%b):b;

}


int main()

{

    char m[100];

    int a;

    int i = 0;

    while(scanf("%d",&a) && (a!=-1)){

        scanf("%s",m);

        i++;

        int length = strlen(m);

        double x = atof(m);

        int k = length - 2 - a;

        int j = length - 2 - k;

        

        int fenzi,fenmu;

        int temp1,tmep2;

        temp1 = pow(10,j+k) * x;

        tmep2 = pow(10, k) * x;

        fenzi = temp1 - tmep2;

        fenmu = pow(10, j+k) - pow(10, k);

        

        int yueshu = fun(fenzi,fenmu);

        printf("Case %d: %d/%d\n",i,fenzi/yueshu,fenmu/yueshu);

        

    }

    return 0;

}


1 最小公约数的求法,记住


return a%b?fun(b, a%b):b;


输入a,b ----- 如果a%b是true,即不为0,即没有余数关系 ----执行fun(b, a%b)

       ------如果a%b是false,即为0,即有余数关系    -----返回小的那个

比如,输入ab分别为4,20;4%20 = 4; fun(20,4) ;  20%4=0;return 4;



2.pow()需要math。h

atof()是转成double ,需要cstdlib.h




Create a class called Rational for performing arithmetic with fractions. Use integer variables to represent the private data of the class – the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should store the fraction in reduced form. For example, the fraction 2/4 would be stored in the object as 1 in the numerator and 2 in the denominator. In order to compute the reduced form, you need to write a reduction function which uses the Euclidean algorithm to get the greatest common divisor (GCD) of the numerator and denominator first and then divides GCD to get the reduced numerator and denominator. Provide public member functions that perform each of the following tasks: (a) Subtract a Rational number from the other Rational number. The result should be stored in reduced form. (b) Divide a Rational number by the other Rational number. The result should be stored in reduced form. (c) Print Rational numbers in the form a/b, where a is the numerator and b is the denominator. (d)Compare two Rational numbers to make sure which one is smaller or they are equal. (1 for the first number, 2 for the second number and 0 if they are equal) Please also write a main function to prompt the user to input two Rational numbers . Subtract one number from the other from these two numbers using (a) and then print the result using (c). Divide one number from the other from these two numbers using (b) and then print the result using (c). Compare these two Rational numbers using (d) and indicate which one is smaller or they are equal. 用c++5.11寫出,且使用using namespace std;
最新发布
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值