码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 10976 Fractions Again?(枚举+数学)

时间:2015-04-04 06:32:14      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

题目大意:

  就是说输入一个k,然后求出满足1/k = 1/x+1/y的所有形式,使得x>=y。

解题思路:

  拿到这个题后,看到题目仅仅给了x>=y.这个条件,感觉无从下手,但是仔细想想就不难发现,只要我们

首先确定了枚举的范围,那么我们就能够在这个区间内,找到符合我们要求的解了。

推导:

  因为x>=y,所以1/x<=1/y,由1/k-1/x=1/y  -> 1/k-1/y <= 1/y -> y<=2k,

  然后,由等式1/k = 1/x+1/y -> x = (y*k)/(y-k)

  这样一来,我们只需要在[k+1,2k]上枚举y的值,然后找到 (y*k)%(y-k)==0的所有x的值就好了。

 

代码:

技术分享
# include<cstdio>
# include<iostream>
# include<algorithm>
# include<functional>
# include<cstring>
# include<string>
# include<cstdlib>
# include<iomanip>
# include<numeric>
# include<cctype>
# include<cmath>
# include<ctime>
# include<queue>
# include<stack>
# include<list>
# include<set>
# include<map>

using namespace std;

const double PI=4.0*atan(1.0);

typedef long long LL;
typedef unsigned long long ULL;

# define inf 999999999
# define MAX 10000

int k;

struct node
{
    int x, y;
}a[MAX];

int main(void)
{
    while ( cin>>k )
    {
        int num = 0;
        for ( int yy = k+1;yy <= 2*k;yy++ )
        {
            int fenzi = yy*k;
            int fenmu = yy-k;
            if ( fenzi%fenmu == 0 )
            {
                int xx = fenzi/fenmu;
                a[num].x = xx;
                a[num++].y = yy;
            }
        }
        cout<<num<<endl;
        for ( int i = 0;i < num;i++ )
        {
            printf("1/%d = 1/%d + 1/%d\n",k,a[i].x,a[i].y);
        }
    }



    return 0;
}
View Code

 

UVA 10976 Fractions Again?(枚举+数学)

标签:

原文地址:http://www.cnblogs.com/wikioibai/p/4391387.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!