zoukankan      html  css  js  c++  java
  • POJ C++程序设计 编程题#1 编程作业—STL2

    编程题#1

    来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

    注意: 总时间限制: 1000ms 内存限制: 65536kB

    描述

    下面的程序用枚举法解决如下问题,请填空。

    平面上的一个矩形,如果其边平行于坐标轴,我们就称其为“标准矩形”。给定不重复的 n 个整点(横、纵坐标都是整数的点),求从这n个点中任取4点作为顶点所构成的四边形中,有多少个是标准矩形。

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    struct Point {
        int x;
        int y;
        Point(int x_,int y_):x(x_),y(y_) { }
    };
    bool operator < ( const Point & p1, const Point & p2)
    {
        if( p1.y < p2.y )
            return true;
        else if( p1.y == p2.y )
            return p1.x < p2.x;
        else
            return false;
    }
    int main()
    {
        int t;
        int x,y;
        cin >> t;
        vector<Point> v;
        while( t -- ) {
            cin >> x >> y;
            v.push_back(Point(x,y));
        }
        vector<Point>::iterator i,j;
        int nTotalNum = 0;
    // 在此处补充你的代码
    return 0;
    }

     

    输入

    第一行是点的数目

    其后每一行都代表一个点,由两个整数表示,第一个是x坐标,第二个是y坐标

     

    输出

    输出标准矩形的数目

    样例输入

    6
    2 3
    2 5
    4 5
    4 4
    2 4
    4 3

     

    样例输出

    3

     

    提示

    所缺代码具有如下形式:

        _____________________;
        for( i = v.begin(); i < v.end() - 1;i ++ )
            for(_____________; ______________; _____________) {
                if(binary_search(v.begin(),v.end(),Point( j->x, i->y)) &&
                    ____________________________________________ &&
                    ____________________________________________ &&
                    ____________________________________________ )
                    nTotalNum ++;
            }
        cout << _________________;

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    struct Point {
        int x;
        int y;
        Point(int x_,int y_):x(x_),y(y_) { }
    };
    bool operator < ( const Point & p1, const Point & p2)
    {
        if( p1.y < p2.y )
            return true;
        else if( p1.y == p2.y )
            return p1.x < p2.x;
        else
            return false;
    }
    int main()
    {
        int t;
        int x,y;
        cin >> t;
        vector<Point> v;
        while( t -- ) {
            cin >> x >> y;
            v.push_back(Point(x,y));
        }
        vector<Point>::iterator i,j;
        int nTotalNum = 0;
    // 在此处补充你的代码
        sort(v.begin(),v.end());
        for( i = v.begin(); i < v.end() - 1;i ++ )
            for(j = i+1; j < v.end(); j++) {
                if(binary_search(v.begin(),v.end(),Point( j->x, i->y)) &&
                   binary_search(v.begin(),v.end(),Point(i->x, j->y)) &&
                   i ->x != j->x &&
                   i->y != j->y )
                    nTotalNum ++;
            }
        cout << nTotalNum / 2;
        return 0;
    }
  • 相关阅读:
    Objective-C Runtime Method Swizzling 实践
    code signing blocked mmap() of '/private/var/contai 报错
    supervisor 监控nginx 一直在重启的问题
    Supervisor 两种不同的启动方式,带来两种不同的结果
    addChildViewController后 Childvc viewWillAppear 不调用的问题
    cocoapods 升级到最新beta 版
    addChildViewController后开启热点/wifi/打电话引起的子vc的布局问题
    "undefined method `root' for nil:NilClass" error when using "pod install" 解决办法
    macOS sierra 10.12 Cocoapods 私有库
    Demo
  • 原文地址:https://www.cnblogs.com/dagon/p/4790853.html
Copyright © 2011-2022 走看看