题目链接

Matrix multiplication

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2143    Accepted Submission(s): 967

Problem Description
Given 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.

Input
The 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).

Output
For each tests:

Print n lines. Each of them contain n integers -- the matrix A×B in similar format.

Sample Input
1
0
1
2
0 1
2 3
4 5
6 7
 
Sample Output
0
0 1
2 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 ;
}

最新文章

  1. Mac入门教程之: Command键5个隐藏功能
  2. Spring解析实践
  3. Centos ftp服务器安装配置
  4. UVM中的class
  5. Python小游戏之猜数字
  6. 关于doctype
  7. git http方式时保存密码
  8. cf C. Fox and Box Accumulation
  9. Blade和其他构建工具有什么不同
  10. Scala 函数(五)
  11. HDU 1976 prime path
  12. java.lang.UnsupportedClassVersionError: Bad version number in .class file 解决方案
  13. C++primer读书笔记9-转换和类类型
  14. 兔子--gradle安装和配置
  15. ELK 6.2.4搭建
  16. phtyon
  17. 关于导入zepto出错的问题
  18. 对聊天室项目的NABCD的分析
  19. Codeforces Round #309 (Div. 1) A(组合数学)
  20. Java循环中标签的作用(转)

热门文章

  1. Everything-启用http服务器(公网IP)会导致共享文件被搜索引擎搜索
  2. Java+微信支付(下预购单+回调+退款+查询账单)
  3. 【核心核心】4.Spring【IOC】注解方式
  4. 2.Spring【DI】XML方式
  5. cf519E
  6. 【agc019f】AtCoder Grand Contest 019 F - Yes or No
  7. Maven实战01_Maven简介
  8. UOJ#80. 二分图最大权匹配 模板
  9. Liferay 7:Liferay DXP解决方案
  10. JAVA面试常见问题之进程和线程篇