| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1892 人关注过本帖
标题:请问这个sort函数出了什么问题
只看楼主 加入收藏
komorebi0110
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:145
专家分:17
注 册:2019-11-23
结帖率:96.88%
收藏
已结贴  问题点数:20 回复次数:5 
请问这个sort函数出了什么问题
(Top-k points) We have a list of points on the plane. Please find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance. For point [x,y], the distance is Sqrt(x^2+y^2).
(若存在多个点与原点距离相等,则按照第1象限Quadrant,第2象限,第3象限,第4象限来取。)
( 同一象限不存在距离相等的点)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
【输入】第一行K值,K为正整数,K小于1000;
第二行,n个点的坐标值(x,y)。
【输出】K个离原点最近的坐标点信息。(若存在多个点与原点距离相等,则按照第1象限Quadrant,第2象限,第3象限,第4象限来取。)
例如:
【输入】
2
3 3 5 -1 -2 4//坐标点(3,3),(5,-1),(-2,4),
【输出】
3 3 -2 4//坐标点(3,3),(-2,4)按照由近及远来排序

#include<string.h>
#include <algorithm>
#include<cmath>
using namespace std;
struct zuobiao{
int heng;
int shu;};
int cmp( zuobiao a, zuobiao b )
{return (a.heng*a.heng+a.shu*a.shu)<(b.heng*b.heng+b.shu*b.shu);
}
int main()
{
    struct zuobiao a[1000];
    int n;
    cin>>n;
    int x,y,i=0;
    while(cin>>x>>y)
    {
        a[i].heng=x;
        a[i++].shu=y;
    }
    sort(a,a+i-1,cmp);
    for(int j=0;j<n;j++)
        cout<<a[j].heng<<' '<<a[j].shu<<' ';
}
//我还没想到怎么按一二三四区间排序,就先写了一个计算长度的cmp,但是永远得不出结果QAQ
搜索更多相关主题的帖子: the Sqrt sort 坐标 int 
2020-05-13 17:08
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:3 
cmp函数返回值类型有问题,还是百度具体用法案例吧

剑栈风樯各苦辛,别时冰雪到时春
2020-05-13 17:35
纯蓝之刃
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:76
帖 子:566
专家分:3690
注 册:2019-7-29
收藏
得分:17 
程序代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include <algorithm>
#include<cmath>
using namespace std;
struct zuobiao
{
    int heng;
    int shu;
};

int cmp( zuobiao a, zuobiao b )
{
    return (a.heng*a.heng+a.shu*a.shu)<(b.heng*b.heng+b.shu*b.shu);
}

int main()
{
    struct zuobiao a[1000];
    int n,i=0;
    scanf("%d",&n);
    while(i<=n)
    {
        scanf("%d",&a[i].heng);
        scanf("%d",&a[i].shu);
        i++;
    }
    sort(a,a+n+1,cmp);
    for(int j=0; j<n; j++)
        cout<<a[j].heng<<' '<<a[j].shu<<' ';

    return 0;
}

一沙一世界,一花一天堂。无限掌中置,刹那成永恒。
2020-05-13 19:08
komorebi0110
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:145
专家分:17
注 册:2019-11-23
收藏
得分:0 
非常感谢!我连sort函数怎么用都不知道就开始瞎写

[此贴子已经被作者于2020-5-13 19:43编辑过]


我想要两颗西柚。
2020-05-13 19:36
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
程序代码:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <functional>
#include <vector>
using namespace std;

struct point
{
    double x, y; // 题目中竟然没告知坐标的类型,也没说坐标的取值范围。

    size_t quadrant() const noexcept // 题目只提到象限,那在xy轴上的怎么办
    {
        if( x>0 && y>=0 ) return 1;
        if( x<=0 && y>0 ) return 2;
        if( x<0 && y<=0 ) return 3;
        if( x>=0 && y<0 ) return 4;
        return 0;
    }
    friend bool operator<( const point& a, const point& b )
    {
        double d1 = a.x*a.x + a.y*a.y;
        double d2 = b.x*b.x + b.y*b.y;
        if( d1 == d2 )
            return a.quadrant() < b.quadrant();
        return d1 < d2;
    }
    friend istream& operator>>( istream& is, point& pt )
    {
        return is >> pt.x >> pt.y;
    }
    friend ostream& operator<<( ostream& os, const point& pt )
    {
        return os << pt.x << ' ' << pt.y;
    }
};

int main( void )
{
    size_t k;
    cin >> k;

    vector<point> pts;
    pts.reserve( 1000 );
    copy( istream_iterator<point>(cin), istream_iterator<point>(), back_inserter(pts) );

    k = min( k, pts.size() );

    std::partial_sort( begin(pts), next(begin(pts),k), end(pts) );

    copy( begin(pts), next(begin(pts),k), ostream_iterator<point>(cout," ") );
}
2020-05-13 20:55
qing_yx
Rank: 2
等 级:论坛游民
威 望:1
帖 子:25
专家分:70
注 册:2020-4-26
收藏
得分:0 
#include<stdio.h>
struct zuobiao{
    int heng;
    int shu;
    int b;   //用于存放a[i].heng*a[i].heng + a[i].shu*a[i].shu的结果
};

void swap(struct zuobiao *a, struct zuobiao *a1)
{
    struct zuobiao temp;

    temp.heng = (*a).heng;
    temp.shu = (*a).shu;
    temp.b = (*a).b;

    (*a).heng = (*a1).heng;
    (*a).shu = (*a1).shu;
    (*a).b = (*a1).b;

    (*a1).heng = temp.heng;
    (*a1).shu = temp.shu;
    (*a1).b = temp.b;
}

void sort(struct zuobiao a[], int n)
{
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = 0; j < n - 1 - i; j++)
        {
            if (a[j].b > a[j + 1].b)
            {
                swap(&a[j],&a[j+1]);
            }
        }
    }
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = 0; j < n - 1 - i; j++)
        {
            if (a[j].b == a[j + 1].b)
            {
                if ((a[j].heng<0 && a[j].shu>0)) //4象限
                    swap(&a[j], &a[j + 1]);
                else if ((a[j].heng<0 && a[j].shu<0) && ((a[j + 1].heng>0 && a[j + 1].shu>0) || (a[j + 1].heng>0 && a[j + 1].shu<0))) //3象限
                    swap(&a[j], &a[j + 1]);
                else if ((a[j].heng>0 && a[j].shu<0) && (a[j + 1].heng>0 && a[j + 1].shu>0)) //2象限
                    swap(&a[j], &a[j + 1]);   
            }
        }
    }
}

int main(void)
{
    struct zuobiao a[100] = { 0 };
    int k = 0;
    int n = 0;

    printf("请输入点的个数:");
    scanf("%d", &n);
    getchar();

    printf("输出距离原点最近的点的个数:");
    scanf("%d", &k);
    getchar();

    for (int i = 0; i < n; i++)
    {
        printf("请输入点的横坐标和纵坐标:");
        scanf("%d%d", &a[i].heng, &a[i].shu);
        getchar();
        a[i].b = a[i].heng*a[i].heng + a[i].shu*a[i].shu;
    }

    sort(a,n);

    for (int j = 0; j < k; j++)
        printf("%d %d\n", a[j].heng, a[j].shu);

    return 0;
}
我是用C语言写的,冒泡排序,写的不好,勉强能用
2020-05-14 12:09
快速回复:请问这个sort函数出了什么问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016890 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved