读书人

计算重复率的公式或算法

发布时间: 2011-12-24 23:03:24 作者: rapoo

求教:计算重复率的公式或算法
有题目ID列表:a b c d

每次随机抽取3道题目,共抽了5次:
第1次:b a c
第2次:c b d
第3次:d b a
第4次:c a d
第5次:b d a

统计:
a出现4次
b出现4次
c出现3次
d出现4次

求:计算重复率的公式或算法

[解决办法]
using System;
using System.Collections.Generic;
using System.Text;

namespace Creator
{
public class TestPaperCreator
{
public static void Main()
{
new TestPaperCreator().Create(5, 3);
}

/// <summary>
/// 出题
/// </summary>
/// <param name= "pageKind "> 出题的套数 </param>
/// <param name= "problemNumber "> 每套题的题目数 </param>
public void Create(int pageKind,int problemNumber)
{
ItemLib lib = new ItemLib();

//每套题目数不能大于题库的总数
if (problemNumber > lib.Items.Count)
return;

Random rd = new Random();

for (int i = 0; i < pageKind; i++)
{
Console.WriteLine( "------------------------------------------- "+i.ToString()+ "--Start-- ");
List <Item> tempLib = new List <Item> ();
tempLib.AddRange(lib.Items);

//出现次数增加那么被选中的概率就会减少
for (int j = 0; j < problemNumber; j++)
{
int sumTimes = 0;
foreach (Item item in tempLib)
{
sumTimes += item.Times;
}

//获得选择依据
int selectKey = rd.Next(sumTimes*(tempLib.Count-1));

int value = 0;
Item selectItem = null;
foreach(Item item in tempLib)
{
value += sumTimes - item.Times;
if (selectKey <= value)
{
selectItem = item;
break;
}
}

//次数加1
selectItem.Times++;
tempLib.Remove(selectItem);
Console.WriteLine( "title: " + selectItem.Name + " Content: " + selectItem.Content);
}

Console.WriteLine();
}


Console.WriteLine( "--------------------------------------------Summary-- ");
foreach (Item item in lib.Items)
{
int occurTimes = item.Times-1;
Console.WriteLine( "title: " + item.Name + " Occur Times: " + occurTimes.ToString());
}
}
}

/// <summary>
/// 题库
/// </summary>
public class ItemLib
{
private List <Item> m_Items = new List <Item> ();

public List <Item> Items
{
get { return m_Items; }
set { m_Items = value; }
}

public ItemLib()
{
//you can get data form library
m_Items.Add(new Item( "A ", "1+1=? "));


m_Items.Add(new Item( "B ", "1+2=? "));
m_Items.Add(new Item( "C ", "3+1=? "));
m_Items.Add(new Item( "D ", "1+4=? "));
}

public int GetSumTimes()
{
int sum = 0;
foreach (Item item in m_Items)
{
sum += item.Times;
}
return sum;
}
}

/// <summary>
/// 题目
/// </summary>
public class Item
{
private string m_Name = string.Empty;
private string m_Content = string.Empty;
private int m_Times = 1;//这里初始化为1

public Item(string inName, string inContent)
{
m_Times = 1;
m_Name = inName;
m_Content = inContent;
}

public string Content
{
get { return m_Content; }
set { m_Content = value; }
}

public int Times
{
get { return m_Times; }
set { m_Times = value; }
}

public string Name
{
get { return m_Name; }
set { m_Name = value; }
}
}
}


------------------------------------- > <(> -----
只是实现了需求,算法不一定太好 :P

读书人网 >C#

热点推荐