bnuoj 20838 Item-Based Recommendation (模拟)

http://www.bnuoj.com/bnuoj/problem_show.php?pid=20838

【题意】:

  有点长,略。

【code】:

  

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string>
 4 #include <string.h>
 5 #include <math.h>
 6 #include <algorithm>
 7 
 8 using namespace std;
 9 
10 double rat[220][220];
11 double drat[220][220];
12 
13 struct Nod
14 {
15     int id;
16     double rt;
17 }node[220];
18 
19 int m,n;
20 
21 bool cmp(Nod a,Nod b)
22 {
23     return a.rt>b.rt;
24 }
25 
26 double getAns(int a,int b)
27 {
28     int i;
29     double sum=0;
30     for(i=1;i<=n;i++)
31     {
32         if(rat[i][a]>0&&rat[i][b]>0)
33         {
34             sum+=(rat[i][a]-rat[i][b])*(rat[i][a]-rat[i][b]);
35         }
36     }
37     return 1.0/(sum+1);
38 }
39 
40 void getDrat()
41 {
42     int i,j;
43     for(i=1;i<=m;i++)
44     {
45         drat[i][i]=1;
46         for(j=i+1;j<=m;j++)
47         {
48             drat[i][j]=drat[j][i]=getAns(i,j);
49         }
50     }
51 }
52 
53 int main()
54 {
55     int c;
56     scanf("%d%d%d",&n,&m,&c);
57     memset(rat,0,sizeof(rat));
58     while(c--)
59     {
60         int a,b;
61         scanf("%d%d",&a,&b);
62         scanf("%lf",&rat[a][b]);
63     }
64     getDrat();
65     int x;
66     while(~scanf("%d",&x))
67     {
68         printf("Recommendations for user %d:
",x);
69         int i,cnt=0,j;
70         double down=0,up=0;
71         for(i=1;i<=m;i++)
72         {
73             if(rat[x][i]==0)//i=6
74             {
75                 down=0,up=0;
76                 for(j=1;j<=m;j++)
77                 {
78                     if(rat[x][j]>0)
79                     {
80                         down+=drat[i][j];
81                         up+=drat[i][j]*rat[x][j];
82                     }
83                 }
84                 node[cnt].id = i;
85                 node[cnt].rt = up/down;
86                 cnt++;
87             }
88         }
89         sort(node,node+cnt,cmp);
90         for(i=0;i<cnt&&i<10;i++)
91         {
92             printf("%d %.3lf
",node[i].id,node[i].rt);
93         }
94         putchar(10);
95     }
96     return 0;
97 }
原文地址:https://www.cnblogs.com/crazyapple/p/3335295.html