,小弟我这个排序,程序如何写啊多谢
求助,我这个排序,程序怎么写啊,谢谢!
请教:
struct Ranking
{
string name; //名字
int loops; //圈数
int point; //已经到达的 ID
float distance; //里前面点的距离
}
Ranking a[4];
sting b[4];
现在要对
a[0],a[1],a[2],a[3]
进行排序
排序的规则是
谁的 loops 数值最大
谁排前面
如果 loops 相等
就看 point 值
谁大排 前面
如果 point 相等
就看 distance 值
谁小排 前面
比如
a[0].name = "AA"
a[0].loops = 4;
a[0].point = 20;
a[0].distance = 2.5f;
a[1].name = "BB"
a[1].loops = 3;
a[1].point = 25;
a[1].distance = 4.5f;
a[2].name = "CC"
a[2].loops = 3;
a[2].point = 26;
a[2].distance = 5.5f;
a[3].name = "DD"
a[3].loops = 2;
a[3].point = 30;
a[3].distance = 1.5f;
那么排序应该是
AA CC BB DD
那么 b 的值应该为
b[0] = "AA"
b[2] = "CC"
b[2] = "BB"
b[3] = "DD"
谢谢!
------解决思路----------------------
仅供参考
请教:
struct Ranking
{
string name; //名字
int loops; //圈数
int point; //已经到达的 ID
float distance; //里前面点的距离
}
Ranking a[4];
sting b[4];
现在要对
a[0],a[1],a[2],a[3]
进行排序
排序的规则是
谁的 loops 数值最大
谁排前面
如果 loops 相等
就看 point 值
谁大排 前面
如果 point 相等
就看 distance 值
谁小排 前面
比如
a[0].name = "AA"
a[0].loops = 4;
a[0].point = 20;
a[0].distance = 2.5f;
a[1].name = "BB"
a[1].loops = 3;
a[1].point = 25;
a[1].distance = 4.5f;
a[2].name = "CC"
a[2].loops = 3;
a[2].point = 26;
a[2].distance = 5.5f;
a[3].name = "DD"
a[3].loops = 2;
a[3].point = 30;
a[3].distance = 1.5f;
那么排序应该是
AA CC BB DD
那么 b 的值应该为
b[0] = "AA"
b[2] = "CC"
b[2] = "BB"
b[3] = "DD"
谢谢!
------解决思路----------------------
仅供参考
//字母优先级是 d, f, e, c, a . 然后字符数组是str={"deec","dac","ceef","fee"};输出的是"deec","dac","fee","ceef"
#include <stdio.h>
#include <string.h>
int od(char c) {
char *p;
char order[6]="dfeca";
if (0==c) return -1;
p=strchr(order,c);
if (NULL==p) return -1;
return p-order;
}
int cmp(char *s1,char *s2) {
int od1,od2;
while (1) {
od1=od(*s1);
od2=od(*s2);
if (od1<od2) return -1;
if (od1>od2) return 1;
if (-1==od1 && -1==od2) return 0;
s1++;
s2++;
}
}
int main() {
char *str[4]={"deec","dac","ceef","fee"};
char *t;
int i,j;
for (i=0;i<4-1;i++) {
for (j=i+1;j<4;j++) {
if (1==cmp(str[i],str[j])) {
t=str[i];str[i]=str[j];str[j]=t;
}
}
}
for (i=0;i<4-1;i++) printf("\"%s\",",str[i]);
printf("\"%s\"",str[i]);
return 0;
}
//"deec","dac","fee","ceef"