PU-1,3;
PD-3,3;
PU-4,8;
PD-5,8;
PU-4,2;
PD-3,2;
PU-3,5;
PD-5,5;
PU是坐标开始变量,PD是坐标结束变量(PU,PD两点连接起来表示的是一条线段)。对{1,3},{3,3},{4,8},{5,8},{4,2},{3,2},{3,5},{5,5}进行排序程序已经写好了,但输出的不是我需要的结果,要求的是(对PU,PD一对数据进行排序,使的每一条线保持最近的排序)输出为:
PU-4,2;
PD-3,2;
PU-1,3;
PD-3,3;
PU-3,5;
PD-5,5;
PU-4,8;
PD-5,8;
程序如下:
#include "stdafx.h"
#include <iostream>
using namespace std;
void main()
{
int a[8][2]={{1,3},{3,3},{4,8},{5,8},{4,2},{3,2},{3,5},{5,5}i,j,r,t;
for(i=1;i<8;i++)
{
for(j=0;j<8-i;j++)
{
if(a[j][0]>a[j+1][0])
{
for(r=0;r<2;r++)
{
t=a[j][r];
a[j][r]=a[j+1][r];
a[j+1][r]=t;
}
}
else if(a[j][0]==a[j+1][0])
{
if(a[j][1]>a[j+1][1])
{
t=a[j][1];
a[j][1]=a[j+1][1];
a[j+1][1]=t;
}
}
}
}
for(i=0;i<8;i++)
{
cout<<"{";
for(j=0;j<2;j++)
{
cout<<a[i][j];
cout<<",";
}
cout<<"},";
}
cout<<endl;
}
怎么搞啊?请高手指教下!~
5 个解决方案
#1
sf
#2
so easy.
struct point
{
int x;
int y;
};
class line
{
public:
point pt1;
point pt2;
};
struct comp
{
bool operator()(const line& l1, const line& l2) const
{
return l1.pt2.y < l2.pt2.y;
}
vector<line> group;
sort(group.begin(), group.end());
struct point
{
int x;
int y;
};
class line
{
public:
point pt1;
point pt2;
};
struct comp
{
bool operator()(const line& l1, const line& l2) const
{
return l1.pt2.y < l2.pt2.y;
}
vector<line> group;
sort(group.begin(), group.end());
#3
更正:
struct comp
{
bool operator()(const line& l1, const line& l2) const
{
return l1.pt2.y < l2.pt2.y;
};
struct comp
{
bool operator()(const line& l1, const line& l2) const
{
return l1.pt2.y < l2.pt2.y;
};
#4
晕:
#include <algorithm>
using namespace std;
struct point
{
int x;
int y;
};
class line
{
public:
point pt1;
point pt2;
};
struct comp
{
bool operator()(const line& l1, const line& l2) const
{
return l1.pt2.y < l2.pt2.y;
}
};
int main(void)
{
vector<line> group;
// 把你的数据插入group中
// ...
sort(group.begin(), group.end(), comp());
// ...
return 0;
}
#include <algorithm>
using namespace std;
struct point
{
int x;
int y;
};
class line
{
public:
point pt1;
point pt2;
};
struct comp
{
bool operator()(const line& l1, const line& l2) const
{
return l1.pt2.y < l2.pt2.y;
}
};
int main(void)
{
vector<line> group;
// 把你的数据插入group中
// ...
sort(group.begin(), group.end(), comp());
// ...
return 0;
}
#5
可以像 sinall那么做~
自己定义比较方式~
自己定义比较方式~
#1
sf
#2
so easy.
struct point
{
int x;
int y;
};
class line
{
public:
point pt1;
point pt2;
};
struct comp
{
bool operator()(const line& l1, const line& l2) const
{
return l1.pt2.y < l2.pt2.y;
}
vector<line> group;
sort(group.begin(), group.end());
struct point
{
int x;
int y;
};
class line
{
public:
point pt1;
point pt2;
};
struct comp
{
bool operator()(const line& l1, const line& l2) const
{
return l1.pt2.y < l2.pt2.y;
}
vector<line> group;
sort(group.begin(), group.end());
#3
更正:
struct comp
{
bool operator()(const line& l1, const line& l2) const
{
return l1.pt2.y < l2.pt2.y;
};
struct comp
{
bool operator()(const line& l1, const line& l2) const
{
return l1.pt2.y < l2.pt2.y;
};
#4
晕:
#include <algorithm>
using namespace std;
struct point
{
int x;
int y;
};
class line
{
public:
point pt1;
point pt2;
};
struct comp
{
bool operator()(const line& l1, const line& l2) const
{
return l1.pt2.y < l2.pt2.y;
}
};
int main(void)
{
vector<line> group;
// 把你的数据插入group中
// ...
sort(group.begin(), group.end(), comp());
// ...
return 0;
}
#include <algorithm>
using namespace std;
struct point
{
int x;
int y;
};
class line
{
public:
point pt1;
point pt2;
};
struct comp
{
bool operator()(const line& l1, const line& l2) const
{
return l1.pt2.y < l2.pt2.y;
}
};
int main(void)
{
vector<line> group;
// 把你的数据插入group中
// ...
sort(group.begin(), group.end(), comp());
// ...
return 0;
}
#5
可以像 sinall那么做~
自己定义比较方式~
自己定义比较方式~