0
点赞
收藏
分享

微信扫一扫

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;
}



举报

相关推荐

0 条评论