UVA10976Fractions Again?!(暴力解法)

继续暴力
先分析,xy关系可以推算出y的最大范围2k
解:x>=y 即1/x<=1/y 由于1/x=1/k - 1/y 代入得1/k<=2/y 即y<=2k
然后枚举y即可求出x,再判断。
It is easy to see that for every fraction in the form 1/k(k>0), we can always nd two positive integersxandy, x >=y, such that:
1/k = 1/x + 1/y
Now our question is: can you write a program that counts how many such pairs of x and y there are for any given k?
Input
Input contains no more than 100 lines, each giving a value of
k(0

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
    int x,y,k,s;
    while(scanf("%d", &k)!=EOF)
    {
        s=0;
        for(y=k+1; y<=2*k; y++){
            if((k*y)%(y-k)==0){
                x=(k*y)/(y-k);
                if(x>=y) s++;
            }
        }
        printf("%d\n", s);
        for(y=k+1; y<=2*k; y++){
            if((k*y)%(y-k)==0){
                x=(k*y)/(y-k);
                if(x>=y) printf("1/%d = 1/%d + 1/%d\n", k, x, y);
            }
        }
    }
    return 0;
}

要改注意全改啊。。。别只改一处啊。。。笨蛋。。。

你可能感兴趣的:(UVA个人记录)