zoukankan      html  css  js  c++  java
  • hdu 1687

    敲打代码的时候竟然忘记qsort()怎么用的了……

    题目大致意思就是求线段投影把x轴分成几段了,之前看过一道例题(刘汝佳编写的《算法入门经典》提到过类似的题目,有类似的思路吧),很类似……

    #include "stdio.h"
    #include "stdlib.h"
    #include "math.h"

    typedef struct point{
        double start,end;
    }points;

    points p[120];
    int s_x,s_y;

    double get_x(int x,int y)
    {
        if(x==s_x)
            return (double)x;
        else
            return (double)(y*s_x-s_y*x)/(double)(y-s_y);
    }

    int cmp(const void *p1,const void *p2)
    {
     return ((*(points *)p1).end)>((*(points *)p2).end)?1:-1;
    }

    int main()
    {
        int T;
        int n;
        int count,k,i,j;
        int x_s,x_e,y_s,y_e;
        double x1,x2,last;
        scanf("%d",&T);
       
        while(T--)
        {
            scanf("%d",&n);
            scanf("%d%d",&s_x,&s_y);
            for(i=0;i<n;i++)
            {
                scanf("%d%d%d%d",&x_s,&y_s,&x_e,&y_e);
                x1=get_x(x_s,y_s);
                x2=get_x(x_e,y_e);
                if(x1<x2)
                {
                    p[i].start=x1;
                    p[i].end=x2;
                }
                else
                {
                    p[i].start=x2;
                    p[i].end=x1;
                }
            }
            qsort(p,n,sizeof(points),cmp);
            /*for(i=0;i<n;i++)
            {
                for(j=0;j<n-i-1;j++)
                {
                    if(p[j].end>p[j+1].end)
                    {
                        temp=p[j].end;
                        p[j].end=p[j+1].end;
                        p[j+1].end=temp;
                        temp=p[j].start;
                        p[j].start=p[j+1].start;
                        p[j+1].start=temp;
                    }
                }
            }*/
          
            k=0;
            count=0;
            for(i=0;i<n&&k<n;i++)
            {
                last=p[k].end;
                for(j=k+1;j<n;j++)
                {
                    if(p[j].start<last)
                    {
                        last=p[j].end;
                        k=j;
                    }
                }
                    k++;
                    count++;
            }

           
            printf("%d\n",count+1);
        }
       
        return 0;
    }

    这题做的有点辛苦,就贴出来吧……

  • 相关阅读:
    c#自动更新+安装程序的制作
    VS2013项目受源代码管理向源代码管理注册此项目时出错
    WinDbg配置和使用基础
    InstallShield Limited Edition for Visual Studio 2013 图文教程(教你如何打包.NET程序)
    PowerDesigner 如何生成数据库更新脚本
    用户故事(User Story)
    Troubleshooting Record and Playback issues in Coded UI Test
    Coded UI
    compare two oracle database schemas
    How to: Use Schema Compare to Compare Different Database Definitions
  • 原文地址:https://www.cnblogs.com/Shirlies/p/2347040.html
Copyright © 2011-2022 走看看