10976 - Fractions Again!

来源:互联网 发布:网络牛牛赌博犯法 编辑:程序博客网 时间:2024/05/16 00:50

It is easy to seethat for every fraction in the form  (k >0), we can always find two positive integers x and yx ≥ y,such that: 

.

Now our questionis: can you write a program that counts how many such pairs of x and y thereare for any given k?

Input

Input contains nomore than 100 lines, each giving a value of k (0< k ≤ 10000).

Output

For each k,output the number of corresponding (xy)pairs, followed by a sorted list of the values of x and y,as shown in the sample output.

Sample Input

2

12

Sample Output

2

1/2 = 1/6 + 1/3

1/2 = 1/4 + 1/4

8

1/12 = 1/156 +1/13

1/12 = 1/84 + 1/14

1/12 = 1/60 + 1/15

1/12 = 1/48 + 1/16

1/12 = 1/36 + 1/18

1/12 = 1/30 + 1/20

1/12 = 1/28 + 1/21

1/12 = 1/24 + 1/24

代码:

#include<iostream>

#include<vector>

using namespacestd;

 

int main()

{

    int k;

    while(cin>>k)

    {

        vector<int> ans;

        int num=0;

        for(int y=k+1; y<2*k+10; y++)

        {

            if(((k*y)%(y-k)==0)&&((k*y)/(y-k)>=y))

            {

                ans.push_back((k*y)/(y-k));

                ans.push_back(y);

                num++;

            }

        }

        cout<<num<<endl;

        for(int i=0; i<ans.size()-1; i=i+2)

        {

           cout<<"1/"<<k<<" =1/"<<ans[i]<<" + 1/"<<ans[i+1]<<endl;

        }

    }

    return 0;

}

解析:

判断是否整除的方法:取余的结果是否为0?

�Mca�5U�Rmal>       if(ans[d] >= 0)

       {

           printf("%d %d\n", ans[d], d);

           return;

       }

       d--;

    }

}

int main()

{

   int T, a, b, c, d;

   scanf("%d", &T);

   while(T--)

    {

       scanf("%d%d%d%d", &a, &b, &c, &d);

       solve(a, b, c, d);

    }

   return 0;

}

0 0