题解

29 条题解

  • 0
    @ 2014-12-15 22:04:48

    #include<iostream>
    using namespace std;
    double a,b,aa,bb,l;
    int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
    int main()
    {
    cin>>a>>b>>l;
    double ans=a/b;
    double ans2=0.0000;
    double ans3=1000000.00;
    for(double i=1.0;i<=l;i++)
    {
    for(double j=1.0;j<=l;j++)
    {

    ans2=(double)i/j;
    double ans4=ans2-ans;
    if(ans4>=0&&gcd((int)i,(int)j)==1&&ans4<=ans3){ans3=ans4;aa=i;bb=j;}
    }
    }
    cout<<aa<<" "<<bb<<endl;
    return 0;
    }

  • 0
    @ 2014-12-14 18:46:16

    var a,b,l,i,j,min_a,min_b:longint; min:real;
    function g(a,b:longint):longint;
    begin
    if a<b then begin a:=a+b; b:=a-b; a:=a-b; end;
    if a mod b=0 then exit(b);
    exit(g(b,a mod b));
    end;

    begin
    read(a,b,l); min:=maxlongint; min_a:=0; min_b:=0;
    if a=b then writeln('1 1')
    else
    begin
    for i:=1 to l do
    for j:=1 to l do
    if (i/j>=a/b) and (g(i,j)=1) and (i/j-a/b<min) then
    begin min:=i/j-a/b; min_a:=i; min_b:=j; end;
    writeln(min_a,' ',min_b);
    end;
    end.

  • 0
    @ 2014-11-29 21:16:25

    双指针扫描

    #include <stdio.h>
    int main(){
    double A,B,max;
    double pointerA=1,pointerB=1,ansA=10000,ansB=1;
    scanf("%lf%lf%lf",&A,&B,&max);
    while(1){
    if(pointerA/pointerB>=A/B && ansA/ansB>pointerA/pointerB)
    ansA=pointerA,ansB=pointerB;
    if(pointerA/pointerB>A/B){
    if(pointerB<max)
    pointerB++;
    else
    break;
    }else if(pointerA/pointerB<A/B){
    if(pointerA<max)
    pointerA++;
    else
    break;
    }else{
    break;
    }
    }
    printf("%.0lf %.0lf\n",ansA,ansB);
    return 0;
    }

  • 0
    @ 2014-11-24 16:08:39

    #include<cstdio>
    int a,b,l,ansx,ansy;
    double min=123456789.0,s,tmp;
    int gcd(int a,int b) {return b?gcd(b,a%b):a;}
    int main()
    {
    scanf("%d%d%d",&a,&b,&l),s=(double)a/(double)b;
    for(int i=1;i<=l;i++)
    for(int j=1;j<=l;j++)
    gcd(i,j)==1&&(tmp=(double)i/double(j))>=s&&tmp<=min?(ansx=i,ansy=j,min=tmp):1;
    printf("%d %d\n",ansx,ansy);
    return 0;
    }

  • -1
    @ 2017-08-13 17:39:46

    var
    n,m,i,j,x,y,l:longint;
    function gcd(m,n:longint):longint;
    begin
    if m mod n=0 then exit(n)
    else gcd:=gcd(n,m mod n);
    end;
    begin
    readln(m,n,l);
    x:=maxlongint;y:=1;
    for i:=1 to l do
    for j:=1 to l do
    if (gcd(i,j)=1)and(i/j-m/n<x/y-m/n)and(i/j>=m/n) then
    begin
    x:=i;
    y:=j;
    end;
    writeln(x,' ',y);
    end.

  • -1
    @ 2017-01-04 11:57:11

    坑爹啊 居然不是算误差 而是【正】误差 60分都是错这了

  • -1
    @ 2016-12-10 14:17:57

    #include <iostream>
    #include <cstdio>
    #include <cstring>

    using namespace std;
    int gcd(int x,int y){
    if(y != 0){
    return gcd(y, x%y);
    }else{
    return x;
    }

    }

    float a,b;
    int l;
    float mi = 0x3f3f3f3f;
    int a1,b1;
    int main(){
    cin>>a>>b>>l;
    float tempi, tempj;
    for(int i = 1; i <= l; i++){
    for(int j = 1; j <= l; j++){
    tempi = i;
    tempj = j;
    if(gcd(i,j) == 1){
    if(tempi/tempj >= a/b){
    if(tempi/tempj - a/b < mi){
    mi = tempi/tempj - a/b;
    a1 = i;
    b1 = j;
    }
    }
    }
    }
    }
    printf("%d %d", a1, b1);

    return 0;
    }

  • -1
    @ 2016-08-27 17:41:38
    #include <iostream>
    #include <cstdio>
    using namespace std;
    int gcd(int a, int b)
    {
        if(a == 0)
        {
            return b;
        }
    
        if(b == 0)
        {
            return a;
        }
    
        if(a & 1 == 0 && b & 1 == 0)
        {
            return gcd(a >> 1, b >> 1) << 1;
        }
        else
            if(a & 1 == 0)
            {
                return gcd(a >> 1, b);
            }
            else
                if(b & 1 == 0)
                {
                    return gcd(a, b >> 1);
                }
                else
                {
                    return gcd(a - b >= 0 ? a - b : b - a, min(a, b));
                }
    }
    int a, b, t, x, y, l;
    double _min = 3838438;
    int main()
    {
        ios::sync_with_stdio(false);
        cin >> a >> b >> l;
        t = gcd(a, b);
    
        while(t > 1)
        {
            a /= t;
            b /= t;
            t = gcd(a, b);
        }
    
        if(a > l || b > l)
            for(int i = 1; i <= l; i++)
                for(int j = 1; j <= l; j++)
                {
                    if(gcd(i, j) == 1 && i * 1.0 / j * 1.0 > a * 1.0 / b * 1.0 && i * 1.0 / j * 1.0 < _min)
                    {
                        x = i;
                        y = j;
                        _min = i * 1.0 / j * 1.0;
                    }
                }
        else
        {
            x = a;
            y = b;
        }
    
        cout << x << ' ' << y;
    }
    
  • -1
    @ 2016-07-17 21:17:40

    少了一句判断i/j>=a/b就WA了4个点。。

信息

ID
1912
难度
4
分类
(无)
标签
递交数
2120
已通过
870
通过率
41%
被复制
12
上传者