首页 > 代码库 > UVA - 10895Matrix Transpose(vector)
UVA - 10895Matrix Transpose(vector)
题目: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)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。