POJ 3318 Matrix Multiplication(随机算法)

题目链接

随机算法使劲水...srand((unsigned)time(0))比srand(NULL)靠谱很多,可能是更加随机。

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <map>
 5 #include <ctime>
 6 #include <cmath>
 7 using namespace std;
 8 #define LL __int64
 9 int a[501][501],b[501][501],c[501][501];
10 int main()
11 {
12     int i,j,k,flag,n;
13     srand((unsigned)time(0));
14     while(scanf("%d",&n)!=EOF)
15     {
16         for(i = 1; i <= n; i ++)
17         {
18             for(j = 1; j <= n; j ++)
19                 scanf("%d",&a[i][j]);
20         }
21         for(i = 1; i <= n; i ++)
22         {
23             for(j = 1; j <= n; j ++)
24                 scanf("%d",&b[i][j]);
25         }
26         for(i = 1; i <= n; i ++)
27         {
28             for(j = 1; j <= n; j ++)
29                 scanf("%d",&c[i][j]);
30         }
31         flag = 1;
32         for(i = 1; i <= 40000&&flag; i ++)
33         {
34             LL temp = 0;
35             int x,y;
36             x = rand()%n + 1;
37             y = rand()%n + 1;
38             for(k = 1; k <= n; k ++)
39             {
40                 temp += a[x][k]*b[k][y];
41             }
42             if(temp != c[x][y])
43                 flag = 0;
44         }
45         if(flag)
46             printf("YES
");
47         else
48             printf("NO
");
49     }
50     return 0;
51 }
原文地址:https://www.cnblogs.com/naix-x/p/3212936.html