码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 10895Matrix Transpose(vector)

时间:2014-09-11 11:14:11      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:http   io   os   ar   for   sp   cti   代码   on   

题目:UVA - 10895Matrix Transpose(vector)


题目大意:给出一个矩阵求它的转置矩阵。


解题思路:因为数组可以达到10000 * 10000 然后里面非0的数最多1000,所以用vector数组来存储。


代码:

#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

const int N = 10005;
struct Mat {

	int th, val;
};

vector<Mat> v[N];
vector<Mat> ans;
vector<int> th, val;
int cnt[N];
int m, n;

void solve () {

	printf ("%d %d\n", n, m);

	memset (cnt, 0, sizeof (cnt));	
	Mat tmp;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (v[j].size() && cnt[j] < v[j].size() && v[j][cnt[j]].th == i) {
				tmp.th = j;
				tmp.val = v[j][cnt[j]].val;
				ans.push_back(tmp);
				cnt[j]++;
			}
		}

		printf ("%d", ans.size());
		for (int j = 0; j < ans.size(); j++)
			printf (" %d", ans[j].th + 1);
		printf ("\n");

		if (ans.size()) {
			for (int j = 0; j < ans.size(); j++) {
				if (j != ans.size() - 1)
					printf ("%d ", ans[j].val);
				else
					printf ("%d\n", ans[j].val);
			}
		} else
			printf ("\n");
		ans.clear();
	}

	for (int i = 0; i < m; i++)
		v[i].clear();
}

int main () {

	int num, a;
	Mat tmp;
	while (scanf ("%d%d", &m, &n) != EOF) {

		for (int i = 0; i < m; i++) {

			scanf ("%d", &num);
			th.clear();
			val.clear();

			for (int j = 0; j < num; j++) {
				scanf ("%d", &a);
				th.push_back(a);
			}

			for (int j = 0; j < num; j++) {
				scanf ("%d", &a);
				val.push_back(a);
			}

			for (int j = 0; j < num; j++) {
				tmp.th = th[j] - 1;
				tmp.val = val[j];
				v[i].push_back(tmp);
			}	
		}
		solve();
	}
	return 0;
}


UVA - 10895Matrix Transpose(vector)

标签:http   io   os   ar   for   sp   cti   代码   on   

原文地址:http://blog.csdn.net/u012997373/article/details/39203183

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!