java 权重排序算法_权重结构的加权排序算法
1 #include 2 #include 3 #include 4 #include 5 #define Random() (rand()%100)6 using namespacestd;7 #define MAXNUM 1089 //定义加权排序的结构10 template11 struct_sortStru12 {13 T _data1;14 T _data2;15 T _data3;16
1 #include
2 #include
3 #include
4 #include
5 #define Random() (rand()%100)
6 using namespacestd;7 #define MAXNUM 10
8
9 //定义加权排序的结构
10 template
11 struct_sortStru12 {13 T _data1;14 T _data2;15 T _data3;16 T _data4;17 int nWeight[4];18 _sortStru()19 {20 memset(this, 0, sizeof(_sortStru));21 }22 };23
24
25 bool _sort_sample(const _sortStru &l, const _sortStru &r)26 {27 return l._data1
30
31 class_sort32 {33 public:34 _sort() : pos(0){}35 _sort(intnPos) : pos(nPos){}36 bool operator()(const _sortStru &l, const _sortStru &r)37 {38 switch(pos)39 {40 case 0:41 return l._data1
53 private:54 intpos;55 };56
57 template
58 classAdd_Weight59 {60 public:61 Add_Weight(int type, intstart)62 : ntype(type), nstart(start), nLastValue(0), nLastWeight(0)63 {64 }65
66 void operator()(_sortStru &_F)67 {68 switch(ntype)69 {70 case 0: default:71 if (_F._data1 ==nLastValue)72 {73 _F.nWeight[ntype] =nLastWeight;74 }75 else
76 {77 _F.nWeight[ntype] =nstart;78 nLastValue =_F._data1;79 nLastWeight =nstart;80 }81 break;82 case 1:83 if (_F._data2 ==nLastValue)84 {85 _F.nWeight[ntype] =nLastWeight;86 }87 else
88 {89 _F.nWeight[ntype] =nstart;90 nLastValue =_F._data2;91 nLastWeight =nstart;92 }93 break;94 case 2:95 if (_F._data3 ==nLastValue)96 {97 _F.nWeight[ntype] =nLastWeight;98 }99 else
100 {101 _F.nWeight[ntype] =nstart;102 nLastValue =_F._data3;103 nLastWeight =nstart;104 }105 break;106 case 3:107 if (_F._data4 ==nLastValue)108 {109 _F.nWeight[ntype] =nLastWeight;110 }111 else
112 {113 _F.nWeight[ntype] =nstart;114 nLastValue =_F._data4;115 nLastWeight =nstart;116 }117 break;118 }119 nstart++;120 }121 private:122 intntype;123 intnstart;124 T nLastValue;125 intnLastWeight;126 };127
128
129
130 //四个参数的权重类
131 classCWeight132 {133 public:134 CWeight()135 {136 weight_1 = 0;137 weight_1 = 0;138 weight_1 = 0;139 weight_1 = 0;140 };141
142 CWeight(int Fir, int Sec, int thi, intFou)143 : weight_1(Fir), weight_2(Sec), weight_3(thi), weight_4(Fou)144 {145 };146
147 voidCheck()148 {149 assert(weight_1 + weight_2 + weight_3 + weight_4 == 100);150 }151
152 public:153 intweight_1;154 intweight_2;155 intweight_3;156 intweight_4;157 };158
159 template
160 classCompare_Weight161 {162 public:163 Compare_Weight(CWeight *pF)164 : pweight(pF)165 {166 }167
168 bool operator()(const _sortStru &_F, const _sortStru &_L)169 {170 T t1 = _F.nWeight[0] * pweight->weight_1171 + _F.nWeight[1] * pweight->weight_2172 + _F.nWeight[2] * pweight->weight_3173 + _F.nWeight[3] * pweight->weight_4;174
175 T t2 = _L.nWeight[0] * pweight->weight_1176 + _L.nWeight[1] * pweight->weight_2177 + _L.nWeight[2] * pweight->weight_3178 + _L.nWeight[3] * pweight->weight_4;179
180 return t1 >t2;181 }182
183 private:184 CWeight *pweight;185 };186
187 intmain()188 {189 //初始化一个vector,成员类型为_sortStru
190 vector< _sortStru >vec;191 int i = 0;192 for (i = 0; i < MAXNUM; i++)193 {194 _sortStrusort;195 sort._data1 =Random();196 sort._data2 =Random();197 sort._data3 =Random();198 sort._data4 =Random();199 vec.push_back(sort);200 }201
202 //输出
203 for (i = 0; i < MAXNUM; i++)204 {205 _sortStru out;206 out =vec.at(i);207 cout<< out._data1<
212 //简单排序,最结构体中的第一个成员进行排序
213 std::sort(vec.begin(), vec.end(), _sort_sample);214 for (i = 0; i < MAXNUM; i++)215 {216 _sortStru out;217 out =vec.at(i);218 cout<< out._data1<
223 //简单排序,最结构体中的第N个成员进行排序
224 std::sort(vec.begin(), vec.end(), _sort(2));225 for (i = 0; i < MAXNUM; i++)226 {227 _sortStru out;228 out =vec.at(i);229 cout<< out._data1<
234 //加权排序
235 for (i = 0; i < 4; i++)236 {237 std::sort(vec.begin(), vec.end(), _sort(i));238 std::for_each(vec.begin(), vec.end(), Add_Weight(i, 0));239 }240 CWeight *weight = new CWeight(50, 50, 0, 0);241 weight->Check();242 std::sort(vec.begin(), vec.end(), Compare_Weight(weight));243 for (i = 0; i < MAXNUM; i++)244 {245 _sortStru out;246 out =vec.at(i);247 cout<< out._data1<
252 return 0;253 }
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)