• POJ3318 Matrix Multiplication


      1 /*
      2  POJ3318 Matrix Multiplication
      3  http://poj.org/problem?id=3318
      4  随机化算法 矩阵
      5  随机一个1×n的矩阵h
      6  在ab=c左右两边左乘h
      7  验证是否相等
      8  然而不知道为什么g++交就会RE,
      9  c++交AC
     10  *
     11  */
     12 #include <cstdio>
     13 #include <algorithm>
     14 #include <cstring>
     15 #include <cmath>
     16 #include <vector>
     17 #include <queue>
     18 #include <iostream>
     19 #include <map>
     20 #include <set>
     21 #include <ctime>
     22 #include <cstdlib>
     23 //#define test
     24 using namespace std;
     25 const int Nmax=505;
     26 const int mod=500;
     27 int n;
     28 struct Matrix
     29 {
     30     int n,m;
     31     int num[Nmax][Nmax];
     32     Matrix()
     33     {
     34         n=0;
     35         m=0;
     36     }
     37     Matrix &operator = (const Matrix b)
     38     {
     39         n=b.n;
     40         m=b.m;
     41         for(int i=1;i<=n;i++)
     42             for(int j=1;j<=m;j++)
     43                 num[i][j]=b.num[i][j];
     44         //show();
     45         return *this;
     46     }
     47     //Matrix(Matrix &a)
     48     //{
     49         //*this=a;
     50     //}
     51     Matrix(int _n,int _m,int **_num)
     52     {
     53         n=_n;
     54         m=_m;
     55         for(int i=1;i<=n;i++)
     56             for(int j=1;j<=m;j++)
     57                 num[i][j]=_num[i][j];
     58     }
     59     friend Matrix operator + (Matrix a,Matrix b)
     60     {
     61         Matrix ans;
     62         ans.n=a.n;
     63         ans.m=a.m;
     64         for(int i=1;i<=ans.n;i++)
     65             for(int j=1;j<=ans.m;j++)
     66                 ans.num[i][j]=a.num[i][j]+b.num[i][j];
     67         return ans;
     68     }
     69     friend Matrix operator * (Matrix a,Matrix b)
     70     {
     71         Matrix ans;
     72         if(a.m!=b.n)
     73         {
     74             printf("error!
    ");
     75             return ans;
     76         }
     77         ans.n=a.n,ans.m=b.m;
     78         int tmp=a.m;
     79         for(int i=1;i<=ans.n;i++)
     80             for(int j=1;j<=ans.m;j++)
     81                 ans.num[i][j]=0;
     82         for(int i=1;i<=ans.n;i++)
     83             for(int j=1;j<=ans.m;j++ )
     84                 for(int k=1;k<=tmp;k++)
     85                     ans.num[i][j]+=a.num[i][k]*b.num[k][j];
     86         //ans.show();
     87         return ans;
     88     }
     89     friend bool operator != (Matrix a,Matrix b)
     90     {
     91         if(a.n!=b.n || a.m!=b.m)
     92             return 1;
     93         for(int i=1;i<=a.n;i++)
     94             for(int j=1;j<=a.m;j++)
     95                 if(a.num[i][j]!=b.num[i][j])
     96                     return 1;
     97         return 0;
     98     }
     99     void show()
    100     {
    101         printf("n:%d m:%d
    ",n,m);
    102         for(int i=1;i<=n;i++)
    103             for(int j=1;j<=m;j++)
    104                 printf("%d%c",num[i][j],j==m? '
    ':' ');
    105     }
    106 };
    107 Matrix a,b,c,h;
    108 //Matrix tmpa,tmpb,tmpc;
    109 int work()
    110 {
    111     srand(unsigned(time(0)));
    112     h.n=1,h.m=n;
    113     for(int t=1;t<=5;t++)
    114     {
    115         for(int i=1;i<=n;i++)
    116         {
    117             h.num[1][i]=rand()%mod;
    118 
    119         }
    120         //h.show();
    121         //tmpa=h*a;
    122         //tmpb=tmpa*b;
    123         //tmpc=h*c;
    124         //tmpa.show();
    125         //tmpb.show();
    126         //tmpc.show();
    127         if( ( (h*a)*b )!=(h*c) )
    128             return 0;
    129     }
    130 
    131     return 1;
    132 }
    133 int main()
    134 {
    135     #ifdef test
    136     #endif
    137     //freopen("poj3318.in","r",stdin);
    138     scanf("%d",&n);
    139     a.n=a.m=b.n=b.m=c.n=c.m=n;
    140     for(int i=1;i<=n;i++)
    141         for(int j=1;j<=n;j++)
    142             scanf("%d",&a.num[i][j]);
    143     for(int i=1;i<=n;i++)
    144         for(int j=1;j<=n;j++)
    145             scanf("%d",&b.num[i][j]);
    146     for(int i=1;i<=n;i++)
    147         for(int j=1;j<=n;j++)
    148             scanf("%d",&c.num[i][j]);
    149     if(work())
    150         printf("YES
    ");
    151     else
    152         printf("NO
    ");
    153     return 0;
    154 }
  • 相关阅读:
    【转载】C#中List集合使用Contains方法判断是否包含某个对象
    【转载】C#中List集合使用Clear方法清空集合
    【转载】C#中List集合使用AddRange方法将一个集合加入到指定集合末尾
    【转载】C#中List集合使用Min()方法查找到最小值
    【转载】 C#中List集合使用OrderByDescending方法对集合进行倒序排序
    Mysql,ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'
    Android -- MeasureSpec
    Android -- PullToRefresh应用
    Android -- Gradle
    Android -- ShortCut
  • 原文地址:https://www.cnblogs.com/BBBob/p/6673095.html
Copyright © 2020-2023  润新知