2016-07-02 6 views
1

私は5つのスコア(後で増やすことができます)ごとに、各ユーザー(クリーンさときれいさ、サービス、場所、スタッフなど)ごとに製品をポーリングすることができます。各スコアには形容詞(1:最悪、2:悪い、3:良い、4:非常に良い、5:異常)があります。 清潔さと清楚:4(非常に良い) サービス:3(良い) 場所:1(最悪) スタッフ:5(異常な)ユーザがこのような製品の一つにポーリングすることができ、例えば小規模なポーリングメカニズムに最適なアルゴリズムは何ですか?

このスコアの平均は製品のスコアであり、小数点になります。この例では3.25です。

今、私はこの結果(3.25)によって製品に形容詞をつけたいと思っています。もしポイントが3.25のような半分の下にあるならば、これは下に丸めます(この3の場合)。 3.7のように半分、それは上に回る(4)

私はこれのための最良のアルゴリズムは何ですか?

私classs'デザインは以下のようなものです:

public class Product 
{} 

public Class Poll 
{ 
    public int Id {get; set;} 
    public int ProductId {get; set;} 
    public Product Product {get; set;} 
    public decimal Score {get; set} 
    public string Adjective {get; set;} 
    public ICollection<PollAttributes> Attributes {get; set;} 

} 

public class Attribute // for the attribute like Services 
{ 
    public int Id {get; set;} 
    public string Title {get; set;} 
    public ICollection<PollAttributes> Attributes {get; set;} 
} 

public Class PollAttributes 
{ 
    public decimal score {get; set;} 

    public int AttributeId {get; set;} 
    public Attribute{get; set;} 

    public int PollId {get; set;} 
    public Poll Poll {get; set;} 
} 

答えて

1

あなたは整数値に丸めた値を取得し、辞書を(持っているConvert.ToInt32(恐らくMath.round(スコア))を使用することができ)保持

poll.attribute = lookup[Convert.toInt32(Math.Round(score))];

+1

.NETの「奇妙な」デフォルトの丸め動作:銀行員の丸めにも留意してください。期待した答えが必ずしもあなたに与えられるとは限りません。だから私は非常に使用することをお勧めします: 'Math.round(score、MidpointRounding.AwayFromZero)' あなたが期待する丸めを与えるように。 – daf

0

平均化が容易である:あなたのような何かができるように、属性値をあなただけの各パラメータのために投票した人の数、およびスコアの合計を保つ(清浄度、サービス、... )。 投票が完了すると、そのパラメータの合計をカウントで割ることで、各パラメータの平均を取得します。次に、5つの平均スコアを合計し、合計を5で割り、製品の全体平均を求めます。今

、次のような文字列配列ます3.99999999999999999と4.0は同じと考えられているので、我々は精密なパラメータを必要とするのでイプシロンの存在が非常に必要であること

String[] adj = {"Worst", "Acceptable", "Good", "Very Good", "Excellent"}; 
//let "score" be the product average score 
double epsilon = 1e-8; 
double score = 3.51; 

int adj_index = (int)(score + epsilon); 
if(score - adj_index >= 0.5){//the floating part was bigger than half 
    adj_index++; 
} 
printf("Product is %s", adj[adj_index]); 

は注意を。実際、double 4.0は常に4と正確に表現することはできません。

関連する問題