zoukankan      html  css  js  c++  java
  • POJ1328 qsort

    至于很多朋友说这道题快排为什么不行,请看qcmp函数,返回1与-1

    // Rader.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <algorithm>
    #include <math.h>
    #include <iostream>
    using namespace std;
    typedef struct island{
        double l;
        double r;
    }Node;
    int   qcmp(   const   void   *arg1,   const   void   *arg2   )  
    {  
        return   (((Node*)arg1)->l >  ((Node*)arg2)->l)?1:-1;
    }
    int   cmp(   Node   arg1,   Node   arg2   )  
    {  
        return   arg1.l<  arg2.l;
    }
    int main(int argc, char* argv[])
    {
        freopen("d:/t.txt","r",stdin);
        int count;
        int r;
        int c = 0;
        while(cin>>count>>r&&(count||r)){
            bool err = false;
            Node n[1000];
            c++;
            for(int i=0;i<count;i++){
                double x,y;
                cin>>x>>y;
                y=y<0?-y:y;
                if(y>r){
                    err = true;
                }
                double d = sqrt(r*r-y*y);
                n[i].l = x-d;
                n[i].r = x+d;
            }
            if(err)
            {
                printf("Case %d: -1\n",c);
                continue;
            }
            qsort(n,count,sizeof(n[0]),qcmp);
            //sort(n,n+count,cmp);
            for(int m = 0;m<count;m++)
                printf("%f\n",n[m].l);
            int ans = 1;
            double left = n[0].r;

            for(int j=1;j<count;j++){
                if(n[j].r<=left){
                    left = n[j].r;
                }else{
                    if(n[j].l>left){ans++;left = n[j].r;}
                }
            }
            printf("Case %d: %d\n",c,ans);
        }
        return 0;
    }

  • 相关阅读:
    linux 常用命令
    books list
    开发文化,沟通、会议、总结
    编程资源
    敏捷开发流程
    服务器安全部署指南
    服务器应用部署规范
    单元测试
    弱弱的页码问题
    实验A javaScript XML数据操作按姓名查询
  • 原文地址:https://www.cnblogs.com/yangyh/p/2033104.html
Copyright © 2011-2022 走看看