zoukankan      html  css  js  c++  java
  • poj3318

    题意:给定三个矩阵a,b,c,问a×b是否等于c?

    分析:只需要设置一个列向量x,求a*(b*x),求c*x,看是否相等即可。

    当然这不能保证正确,x是生成的随机向量,多次生成多次测试即可使错误概率大大减小。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <ctime>
    #include <algorithm>
    using namespace std;
    
    const int maxn = 510;
    
    long long  a[maxn][maxn], b[maxn][maxn], c[maxn][maxn], x[maxn];
    int n;
    
    void init(long long m[maxn][maxn])
    {
    	for (int i = 0; i < n; i++)
    		for (int j = 0; j < n; j++)
    			scanf("%I64d", &m[i][j]);
    }
    
    void mul(long long m[maxn][maxn], long long x[maxn], long long ans[maxn])
    {
    	for (int i = 0; i < n; i++)
    	{
    		ans[i] = 0;
    		for (int j = 0; j < n; j++)
    			ans[i] += x[j] * m[i][j];
    	}
    }
    
    bool check(long long ans1[maxn], long long ans2[maxn])
    {
    	for (int i = 0; i < n; i++)
    		if (ans1[i] != ans2[i])
    			return false;
    	return true;
    }
    int main()
    {
    	//freopen("D:\\t.txt", "r", stdin);
    	scanf("%d", &n);
    	init(a);
    	init(b);
    	init(c);
    	int t = 10;
    	while (t--)
    	{
    		srand(time(NULL));
    		for (int i = 0; i < n; i++)
    			x[i] = rand()%1000 + 1;
    		long long ans[maxn], ans1[maxn], ans2[maxn];
    		mul(b, x, ans);
    		mul(a, ans, ans1);
    		mul(c, x, ans2);
    		if (!check(ans1, ans2))
    		{
    			printf("NO\n");
    			return 0;
    		}
    	}
    	printf("YES\n");
    	return 0;
    }
    
    
  • 相关阅读:
    010 排序: 冒泡 选择
    洛谷 P1540 机器翻译
    洛谷 P1011 车站
    周期串
    2019.03.29 大数据图解
    2019.03.29 算法解读
    2019.03.28 博客反省
    2019.03.27 常用的模块
    2019.03.25 git
    2019.03.25 Ajax三级联动
  • 原文地址:https://www.cnblogs.com/rainydays/p/1948629.html
Copyright © 2011-2022 走看看