例题 7-3 UVA - 10976Fractions Again?!

Posted Visitor

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了例题 7-3 UVA - 10976Fractions Again?!相关的知识,希望对你有一定的参考价值。

【链接】 我是链接,点我呀:)
【题意】


在这里输入题意

【题解】


x>=y
=>
\(\frac{1}{x}<=\frac{1}{y}\)
=>
\(\frac{1}{x}=\frac{1}{k}-\frac{1}{y}\)
结合两个式子可以得到
y<=2*k
则枚举y,然后根据式子得到x,判断合法性就ok

【代码】

/*
    1.Shoud it use long long ?
    2.Have you ever test several sample(at least therr) yourself?
    3.Can you promise that the solution is right? At least,the main ideal
    4.use the puts("") or putchar() or printf and such things?
    5.init the used array or any value?
    6.use error MAX_VALUE?
*/

#include <bits/stdc++.h>
using namespace std;

int k;
vector <pair<int,int> > v;

int main(){
    #ifdef LOCAL_DEFINE
        freopen("F:\\c++source\\rush_in.txt", "r", stdin);
    #endif
    ios::sync_with_stdio(0),cin.tie(0);
    while (cin >> k){
        v.clear();
        for (int y = k+1;y <= 2*k;y++){
            int fenzi = k*y;
            int fenmu = y-k;
            if (fenzi%fenmu==0){
                int x = fenzi/fenmu;
                if (x>=y) v.push_back({x,y});
            }
        }
        cout << v.size() << endl;
        for (int i = 0;i < (int)v.size();i++){
                int x = v[i].first,y = v[i].second;
            cout <<"1/"<<k<<" = 1/"<<x<<" + 1/"<<y<<endl;
        }
    }
    return 0;
}

以上是关于例题 7-3 UVA - 10976Fractions Again?!的主要内容,如果未能解决你的问题,请参考以下文章

Fractions Again?! UVA - 10976

UVA10976

UVa 10976 - Fractions Again?!

Uva 10976 Fractions Again?!

uva 10976 Fractions Again?!

UVa10976 Fractions Again?! (推公式)