Problem 33 Digit cancelling fractions (暴力set+pair)

Digit cancelling fractions

Problem 33

The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that49/98 = 4/8, which is correct, is obtained by cancelling the 9s.

We shall consider fractions like, 30/50 = 3/5, to be trivial examples.

There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.

If the product of these four fractions is given in its lowest common terms, find the value of the denominator.


Answer:
100
Completed on Sat, 29 Oct 2016, 11:17

题解:暴力set + pair....

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    vector< pair<int,int> > s;
    for (int a=10;a<=98;a++)
    {
        for(int b=a+1;b<=99;b++)
        {
            if (a%10 == b/10)//分子个位==分母十位
            {
                if (b%10 != 0)
                {
                    if (a/(float)b == (a/10)/(float)(b%10) )//删除相同的数字后相等
                    {
                        pair<int,int> p(a,b);
                        s.push_back(p);
                    }
                }
            }
        }
    }
 
    int ans1=1,ans2=1; //分子和分母分别的乘积
    vector<pair<int,int> >::iterator it;
    for (it=s.begin(); it!=s.end();++it)
    {
        ans1 *= it->first;
        ans2 *= it->second;
    }

    int ans=__gcd(ans1,ans2);
    ans=ans2/ans;
    cout<<ans<<endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值