【例题 7-3 UVA - 10976】Fractions 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;
}
时间: 2024-08-28 20:11:11

【例题 7-3 UVA - 10976】Fractions Again?!的相关文章

暴力枚举 UVA 10976 Fractions Again?!

题目传送门 1 /* 2 x>=y, 1/x <= 1/y, 因此1/k - 1/y <= 1/y, 即y <= 2*k 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cmath> 8 #include <cstring> 9 #include <string> 10 #include <

uva 10976 Fractions Again(简单枚举)

10976 Fractions Again It is easy to see that for every fraction in the form 1 k (k > 0), we can always find two positive integers x and y, 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

uva 10976 fractions again(水题)——yhx

1 #include<cstdio> 2 int a[30010],b[30010]; 3 int main() 4 { 5 int i,j,k,l,m,n,x,y,z; 6 while (scanf("%d",&k)==1) 7 { 8 n=0; 9 for (i=k+1;i<=2*k;i++) 10 if ((k*i)%(i-k)==0) 11 { 12 a[++n]=k*i/(i-k); 13 b[n]=i; 14 } 15 printf("%

Uva 10976 Fractions Again?!

直接暴力 没技巧 y应该从k+1开始循环,因为不然y-k<0的时候 你相当于(x*y) % (负数) 了. 1 #include <iostream> 2 using namespace std; 3 int X[10005]; 4 int Y[10005]; 5 int main() 6 { 7 int k,cnt; 8 while(cin>>k) 9 { 10 cnt=0; 11 for(int y=k+1;y<=2*k;y++) 12 { 13 if( ( (k*

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

题目大意: 就是说输入一个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

UVA 10976 Fractions Again?! 简单枚举题

#include<stdio.h> struct pairs{ int x,y; }; struct pairs pairs[10000]; int judge(int k,int i){ if((k*i)%(i-k)==0) return 1; else return 0; } int main(){ int k,count; while(scanf("%d",&k)!=EOF){ count=0; for(int i=k+1;i<=2*k;i++){ if

UVA 725 UVA 10976 简单枚举

UVA 725 题意:0~9十个数组成两个5位数(或0开头的四位数),要求两数之商等于输入的数据n.abcde/fghij=n. 思路:暴力枚举,枚举fghij的情况算出abcde判断是否符合题目条件.(注意前导零的判断) 枚举的方法为 for(int i=1234;i<=100000/n;i++){} #include<cstdio> #include<cstring> int num[10]; bool check(int a,int b) { memset(num,0,

Fractions Again?! UVA - 10976

It is easy to see that for every fraction in the form 1k(k > 0), we can always find two positive integersx and y, x ≥ y, such that:1k=1x+1yNow our question is: can you write a program that counts how many such pairs of x and y thereare for any given

uva 10976

枚举法 水题 #include <iostream> #include <cstdio> #include <cstdlib> using namespace std; const int maxn = 1000 + 5; int main(){ int k,x,y,cnt; int xx[maxn],yy[maxn]; bool flag; while(cin >> k && k!=0){ cnt = 0; for(int i=k+1;i&