序列数索引(排序索引)
来源:互联网 发布:淘宝卖家中心后台 编辑:程序博客网 时间:2024/06/06 08:48
对于任意给定的一组数,从大到小依次取最大值的索引位置,构成的索引映射序列。
// -------------------------------------------// 示例:static void test(){ long[] weight = new long[] { 3, 2, 5, 1, 1 }; int[] maxMap = Tools.getMaxMap(weight); MessageBox.Show(ToString(weight) + " 的序列数索引为:\r\n" +ToString(maxMap));}/// <summary>/// 按数值从大到小的顺序依次获取各个值所在的索引位置/// 如:3、2、5、1、1 从大到小的索引为:2、0、1、3、4/// </summary>public static int[] getMaxMap(long[] iteams){ int[] map = new int[iteams == null ? 0 : iteams.Length]; List<int> have = new List<int>(); // 记录各索引位置是否已记录 for (int i = 0; i < iteams.Length; i++) // 每轮循环获取一个最大值索引 { long max = 0; int index = -1; for (int j = 0; j < iteams.Length; j++) { if (have.Contains(j)) continue; // 跳过已记录项 if (iteams[j] > max || index == -1) // 记录更大值索引 { max = iteams[j]; index = j; } } map[i] = index; // 记录此次循环获取到的最大值索引 have.Add(index); } return map;}/** 转化long字符数组为字符串 */public static string ToString(Array A){ string tmp = ""; foreach (object a in A) { tmp += ";" + a; } return tmp.Length > 0 ? tmp.Substring(1) : tmp;}
2017-08-06:修改排序数生成,死循环bug
# region 条件优选处理逻辑 // ------------------------------------------- // 示例: public static void test() { float[] weight = new float[] { 3, 2, 5, 1, 1 }; int[] maxMap = getMaxMap(weight); float[] Rate = getRate(weight); MessageBox.Show(ToString(weight) + " 的,\r\n序列数索引为:\t" + ToString(maxMap) + "\r\n排名分值为:\t" + ToString(Rate)); } /// <summary> /// 对iteams中的数据进行排名(采用浮点数排名),第一名取100分,第二名往后依次递减。 /// choiceMax指示按最大或最小值进行排名 /// </summary> public static float[] getRate(float[] iteams, bool choiceMax = true) { int[] maxMap = getMaxMap(iteams); // 获取最大排名索引信息 float[] rates = new float[maxMap.Length]; // 排名评分信息 for (int i = 0; i < maxMap.Length; i++) { int index = maxMap[i]; float value = choiceMax ? (maxMap.Length - i) : (i + 1); // 获取对应的排名对应的值 float rate = value / maxMap.Length * 100;// 获取对应的百分排名值 rates[index] = rate; // 记录排名分值 } return rates; } /// <summary> /// 按数值从大到小的顺序依次获取各个值所在的索引位置 /// 如:3、2、5、1、1 从大到小的索引为:2、0、1、3、4 /// </summary> public static int[] getMaxMap(float[] iteams) { List<int> map = new List<int>(); // 获取所有索引值 List<int> indexs = new List<int>(); for (int i = 0; i < iteams.Length; i++) indexs.Add(i); while (indexs.Count > 0) { // 获取最大值的索引信息 int index = indexs[0]; float max = iteams[index]; foreach (int i in indexs) { if (iteams[i] > max) // 记录更大值索引 { max = iteams[i]; index = i; } } map.Add(index); indexs.Remove(index); } return map.ToArray(); } /** 转化long字符数组为字符串 */ public static string ToString(Array A) { StringBuilder tmp = new StringBuilder(); foreach (object a in A) { tmp.Append(";" + a); } return tmp.Length > 0 ? tmp.ToString().Substring(1) : tmp.ToString(); } // ------------------------------------------- /// <summary> /// 对datas中的数据,按preferMax进行综合评分。 /// 按列数据分别评分,评分后取乘积,再次转化为百分制 /// </summary> public static List<float> getRate(List<List<float>> datas, bool[] preferMax, bool addMode = false) { // 获取所有列的评分信息 List<float[]> allRates = new List<float[]>(); for (int i = 0; i < datas.Count; i++) { float[] data = datas[i].ToArray(); float[] rate = getRate(data, preferMax[i]); // 获取当前列的评分 allRates.Add(rate); } // 进行综合评分 List<float> Rate=null; foreach (float[] rate in allRates) { if (Rate == null) Rate = Init(rate.Length, addMode ? 0 : 100); Rate = CombineRate(Rate, rate, addMode); } // 若为叠加模式则计算平均得分 if (addMode) { for (int i = 0; i < Rate.Count; i++) { Rate[i] = Rate[i] / datas.Count; } } return Rate; } /// <summary> /// 使用默认值初始化指定大小的List /// </summary> private static List<float> Init(int size, float defultValue) { List<float> list = new List<float>(); for (int i = 0; i < size; i++) { list.Add(defultValue); } return list; } /// <summary> /// 合并评分列表1、评分列表2 /// addMode简单叠加模式 /// </summary> private static List<float> CombineRate(List<float> rate1, float[] rate2, bool addMode = true) { List<float> list = new List<float>(); for (int i = 0; i < rate1.Count || i< rate2.Length; i++) { float rate = addMode ? (rate1[i] + rate2[i]) : CombineRate(rate1[i], rate2[i]); list.Add(rate); } return list; } /// <summary> /// 合并评分1、评分2 /// </summary> private static float CombineRate(float rate1, float rate2) { float rate = rate1 * rate2 / 100; return rate; } #endregion
阅读全文
0 0
- 序列数索引(排序索引)
- 序列、索引
- 【序列】索引
- 索引排序
- 索引排序
- 索引排序
- 索引排序
- 索引排序
- oracle索引与序列
- 关于索引和序列
- 索引、序列、视图
- 索引序列的应用
- 序列:索引,分片,运算
- 索引视图序列
- oracle索引与序列
- 视图、序列、索引、同义词
- 序列和索引
- oracle_序列、索引、同义词
- Uuntu16.04重装后u盘不识别问题解决
- const函数,const变量
- [PAT-乙级]1068.万绿丛中一点红
- coredump 调试
- LintCode 快乐数
- 序列数索引(排序索引)
- 线程安全之如何实现多线程下的自增安全
- 文章标题
- QT 常见错误总结
- 购物车实现——Session
- Mysql 卸载 坑
- quartz.properties配置文件
- Shell运算符
- 二进制和十进制之间的转换