zoukankan      html  css  js  c++  java
  • usaco-2.1-frac1-pass

    这个最简递增分数题关键在于最简,如果知道这个方法,此题易解。呵呵,一次性通过。

    /*
    ID: qq104801
    LANG: C++
    TASK: frac1
    */
    
    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <vector>
    #include <map>
    #include <set>
    #include <algorithm>
    #include <cstdlib>
    #include <cmath>
    
    using namespace std;
    
    int n;
    struct frac
    {
        int up;
        int down;       
        bool operator < (const frac &b)const{
           return up*b.down < down*b.up; }    
    };
    
    bool cmp(const frac &a,const frac &b)
    {   
        return a.up*b.down < b.down*b.up;    
    }
    
    int isstupid(int up,int down)
    {
        int t;
        if (up==1)return 1;
        if(down%up==0)return 0;
        while(down%up!=0)
        {
            t=up;up=down%up;down=t;
        }
        if(up>1)return 0;
        else return 1;
    }
    
    void test()
    {    
        freopen("frac1.in","r",stdin);
        freopen("frac1.out","w",stdout);
        cin >> n;
        //cout<<n<<endl;
        cout<<"0/1"<<endl;
        vector<frac> v;
        int i,j,k;
        frac f;
        for(i=2;i<=n;i++)
            for(j=1;j<=i;j++)
            {
                if(isstupid(j,i))
                {
                    f.up=j;f.down=i;
                    v.push_back(f);
                    //cout<<f.up<<"/"<<f.down<<endl;
                }
            }
        sort(v.begin(),v.end());
        //cout<<endl;
        vector<frac>::iterator it;
        for(it=v.begin();it!=v.end();it++)
            cout<<it->up<<"/"<<it->down<<endl;
    
        cout<<"1/1"<<endl;
    }
    
    int main () 
    {        
        test();        
        return 0;
    }

    测试结果:

    USER: cn tom [qq104801]
    TASK: frac1
    LANG: C++
    
    Compiling...
    Compile: OK
    
    Executing...
       Test 1: TEST OK [0.008 secs, 3376 KB]
       Test 2: TEST OK [0.011 secs, 3508 KB]
       Test 3: TEST OK [0.008 secs, 3508 KB]
       Test 4: TEST OK [0.008 secs, 3508 KB]
       Test 5: TEST OK [0.008 secs, 3508 KB]
       Test 6: TEST OK [0.011 secs, 3508 KB]
       Test 7: TEST OK [0.014 secs, 3508 KB]
       Test 8: TEST OK [0.038 secs, 3508 KB]
       Test 9: TEST OK [0.070 secs, 3508 KB]
       Test 10: TEST OK [0.097 secs, 3508 KB]
       Test 11: TEST OK [0.329 secs, 3508 KB]
    
    All tests OK.
    
    YOUR PROGRAM ('frac1') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations.
    
    Here are the test data inputs:
    
    ------- test 1 ----
    1
    ------- test 2 ----
    2
    ------- test 3 ----
    4
    ------- test 4 ----
    7
    ------- test 5 ----
    10
    ------- test 6 ----
    15
    ------- test 7 ----
    24
    ------- test 8 ----
    50
    ------- test 9 ----
    75
    ------- test 10 ----
    100
    ------- test 11 ----
    160
    
    Keep up the good work!
    Thanks for your submission!
    /***********************************************

    看书看原版,原汁原味。

    不会英文?没关系,硬着头皮看下去慢慢熟练,才会有真正收获。

    没有原书,也要网上找PDF来看。

    网上的原版资料多了去了,下载东西也到原始下载点去看看。

    你会知其所以然,呵呵。

    ***********************************************/

  • 相关阅读:
    海明距离分类 JavaScript
    控制器语法糖
    中文版 ImageNet Classification with Deep Convolutional Neural Networks
    中文版 R-FCN: Object Detection via Region-based Fully Convolutional Networks
    中文版 Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks
    Scrapy中scrapy.Request和response.follow的区别
    神马是回调函数?
    数据库索引原理及优化
    MYSQL数据库引擎区别详解
    数据库索引原理详解
  • 原文地址:https://www.cnblogs.com/dpblue/p/3952331.html
Copyright © 2011-2022 走看看