比如有20个句子,该句子有三个属性
一个值记录句子出现的顺序,一个值为句子内容,另一个为句子权值。
按照权值大小选取 比如10个权值最大的句子,然后按照句子原来的循序输出这10个句子的内容。
该如何操作呢?

解决方案 »

  1.   

    Dictionary 等都是二维表示,可以自己定义一个结构,然后实现Dictionary 中类似的算法,比如排序之类的,这个还很容易做到的
      

  2.   

    用linq
               var maxWeightList = (from sentence in sentences orderby sentence.Weight descending select sentence).Take(5);            var finalList = (from sentence in maxWeightList orderby sentence.Index select sentence.Content);            foreach (var sentenceContent in finalList)
                {
                    Console.WriteLine(sentenceContent);
                } 
      

  3.   

        public class Sentence
        {
            public static int CurrentIndex = 0;
            public int Weight;
            public readonly int Index;
            public string Content;
            public Sentence()
            {
                Index = CurrentIndex++;
            }
        }
      

  4.   

    leafold 你好,按照你的方法确实能很好的完成这个功能,sentences 应该是一个句子对象数组,声明时应该必须要确定数组的大小,但我程序的要求是,句子的数量不知道,能够动态的增长。
     我有一个想法 //先定义一个句子的哈希表
    hashtable sentences =new hashtable();
    //句子的添加函数
    public void tianjia(string content,int weight)
     {  string con =content;int w=weight;
      sentence s=new sentence(string con,int w);
      int index=s.getIndex();//得到句子的序列号
      sentences.add(index,s);
      }
     然后在根据,哈希表中的value值进行 排序啊 提取句子啊
    这只是我的一个想法,也不知道行不行,反正是很麻烦很麻烦。
    有没有简单一点的方法啊
      

  5.   

    leafold 你好,按照你的方法确实能很好的完成这个功能,sentences 应该是一个句子对象数组,声明时应该必须要确定数组的大小,但我程序的要求是,句子的数量不知道,能够动态的增长。
     我有一个想法 //先定义一个句子的哈希表
    hashtable sentences =new hashtable();
    //句子的添加函数
    public void tianjia(string content,int weight)
     {  string con =content;int w=weight;
      sentence s=new sentence(string con,int w);
      int index=s.getIndex();//得到句子的序列号
      sentences.add(index,s);
      }
     然后在根据,哈希表中的value(句子对象)值进行 排序啊 提取句子啊
    这只是我的一个想法,也不知道行不行,反正是很麻烦很麻烦。
    有没有简单一点的方法啊
      

  6.   

    用list就行。index是自动生成的。下面是实例
                List<Sentence> sentences = new List<Sentence>(){new Sentence(){Content = "1,3",Weight=3},
                new Sentence(){Content = "2,7",Weight=7},
                new Sentence(){Content = "3,8",Weight=8},
                new Sentence(){Content = "4,4",Weight=4},
                new Sentence(){Content = "5,2",Weight=2},
                new Sentence(){Content = "6,9",Weight=9},
                new Sentence(){Content = "7,1",Weight=1},
                new Sentence(){Content = "8,10",Weight=10},
                new Sentence(){Content = "9,5",Weight=5},
                new Sentence(){Content = "10,6",Weight=6},
                };