Hdu 4920矩阵乘法(内存访问的讲究)
2024-08-29 22:00:54
Matrix multiplication
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2143 Accepted Submission(s): 967Problem DescriptionGiven two matrices A and B of size n×n, find the product of them.bobo hates big integers. So you are only asked to find the result modulo 3.
InputThe input consists of several tests. For each tests:The first line contains n (1≤n≤800). Each of the following n lines contain n integers -- the description of the matrix A. The j-th integer in the i-th line equals Aij. The next n lines describe the matrix B in similar format (0≤Aij,Bij≤109).
OutputFor each tests:Print n lines. Each of them contain n integers -- the matrix A×B in similar format.
Sample Input10120 12 34 56 7Sample Output00 12 1
请看完这篇博文,看完就去AC吧。加了输入优化,效果并不明显。
Accepted Code:
/*************************************************************************
> File Name: 1010.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年08月05日 星期二 19时22分23秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; int n;
int a[][], b[][], c[][]; int read() {
int res = ;
char c = ' ';
while (c < '' || c > '') c = getchar();
while (c >= '' && c <= '') res += c - '', c = getchar();
return res%;
} int main(void) {
while (~scanf("%d", &n)) {
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
a[i][j] = read();
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
b[i][j] = read();
memset(c, , sizeof(c));
for (int i = ; i < n; i++) {
for (int k = ; k < n; k++) {
for (int j = ; j < n; j++) {
c[i][j] += a[i][k] * b[k][j]; //注意这里的循环顺序
}
}
}
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
printf("%d%c", c[i][j]%, j == n- ? '\n' : ' ');
}
return ;
}
最新文章
- Mac入门教程之: Command键5个隐藏功能
- Spring解析实践
- Centos ftp服务器安装配置
- UVM中的class
- Python小游戏之猜数字
- 关于doctype
- git http方式时保存密码
- cf C. Fox and Box Accumulation
- Blade和其他构建工具有什么不同
- Scala 函数(五)
- HDU 1976 prime path
- java.lang.UnsupportedClassVersionError: Bad version number in .class file 解决方案
- C++primer读书笔记9-转换和类类型
- 兔子--gradle安装和配置
- ELK 6.2.4搭建
- phtyon
- 关于导入zepto出错的问题
- 对聊天室项目的NABCD的分析
- Codeforces Round #309 (Div. 1) A(组合数学)
- Java循环中标签的作用(转)