题目大意:给定两个矩阵,矩阵的最大大小是M*N(小于等于10),矩阵元素的值的绝对值小于等于100,求矩阵相加后全0的行以及列数。
1 #include<iostream>
2 using namespace std;
3 #define N 10
4
5 int main()
6 {
7 int n,m,i,j,a[N][N],b[N][N],s;
8 while(cin>>m)
9 { if(m==0) break;
10 cin>>n;
11 for(i=0;i<m;i++)
12 for(j=0;j<n;j++)
13 cin>>a[i][j];
14 for(i=0;i<m;i++)
15 for(j=0;j<n;j++)
16 { cin>>b[i][j];
17 a[i][j]+=b[i][j];
18 }
19 s=0;
20 for(i=0;i<m;i++)
21 { for(j=0;j<n;j++)
22 if(a[i][j]!=0) break;
23 if(j==n) s++;
24 }
25 for(j=0;j<n;j++)
26 { for(i=0;i<m;i++)
27 if(a[i][j]!=0) break;
28 if(i==m) s++;
29 }
30 cout<<s<<endl;
31 }
32 return 0;
33 }
在网上看到一个大神用纯C++的思想写了如下代码:
1 #include <iostream>
2 #include <vector>
3 #include <algorithm>
4 using namespace std;
5
6 int add(int a, int b){
7 return a+b;
8 }
9
10 int main(){
11 vector<int> ivec1;
12 vector<int> ivec2;
13
14 int m, n, sum, data;
15 while(cin >> m){
16 if(0 == m){
17 break;
18 }
19 cin >> n;
20 sum = m * n;
21 for(int i=0; i!=sum; ++i){
22 cin >> data;
23 ivec1.push_back(data);
24 }
25 for(int i=0; i!=sum; ++i){
26 cin >> data;
27 ivec2.push_back(data);
28 }
29 transform(ivec1.begin(), ivec1.end(), ivec2.begin(), ivec1.begin(), add);
30 int count = 0;
31 int temp;
32 for(int i=0; i!=m; ++i){
33 temp = 0;
34 for (int j=0; j!=n; ++j){
35 temp += ivec1[i*n + j];
36 }
37 if (0 == temp){
38 count++;
39 }
40 }
41 for(int i=0; i!=n; ++i){
42 temp = 0;
43 for (int j=0; j!=m; ++j){
44 temp += ivec1[j*n + i];
45 }
46 if (0 == temp){
47 count++;
48 }
49 }
50 cout << count << endl;
51 ivec1.resize(0);
52 ivec2.resize(0);
53 }
54 return 0;
55 }
上面出现了transform的用法,在这里介绍下其用法:
1 /*////////////////////////////////
2 template < class InputIterator, class OutputIterator, class UnaryOperator >
3 OutputIterator transform ( InputIterator first1, // 源容器的起始地址
4 InputIterator last1, // 源容器的终止地址
5 OutputIterator result, // 目标容器的起始地址
6 UnaryOperator op ); // 函数指针
7 // typedef 目标容器元素类型 (*UnaryOperator)(源容器元素类型);
8
9 template < class InputIterator1, class InputIterator2,
10 class OutputIterator, class BinaryOperator >
11 OutputIterator transform ( InputIterator1 first1, // 源容器1的起始地址
12 InputIterator1 last1, // 源容器1的终止地址
13 InputIterator2 first2, // 源容器2的起始地址,元素个数与1相同
14 OutputIterator result, // 目标容器的起始地址,元素个数与1相同
15 BinaryOperator binary_op ); // 函数指针
16 // typedef 目标容器元素类型 (*BinaryOperator)(源容器1元素类型,源容器2元素类型);
17 //*////////////////////////////////
18
19 #include <iostream>
20 #include <algorithm>
21 #include <vector>
22 #include <string>
23 using namespace std;
24
25 int op_increase (int i)
26 {
27 return i+1;
28 }
29
30 int op_sum (int i, int j)
31 {
32 return i+j;
33 }
34
35 int to_upper(int c)
36 {
37 if (islower(c))
38 {
39 return (c-32);
40 }
41
42 return c;
43 }
44
45 int to_lower(int c)
46 {
47 if (isupper(c))
48 {
49 return c+32;
50 }
51
52 return c;
53 }
54
55 int main () {
56 vector<int> first;
57 vector<int> second;
58 vector<int>::iterator it;
59
60 // set some values:
61 for (int i=1; i<6; i++) first.push_back (i*10); // first: 10 20 30 40 50
62
63 ///将first容器的元素加1赋值给second容器
64 second.resize(first.size()); // allocate space !!!必须预先设置一个大小与first相同
65 transform (first.begin(), first.end(), second.begin(), op_increase); // second: 11 21 31 41 51
66 cout << "second contains:";
67 for (it=second.begin(); it!=second.end(); ++it)
68 {
69 cout << " " << *it;
70 }
71 cout << endl;
72 //*////////////////////////////////////////////
73
74 ///将first容器的元素与second容器的元素相加,并将得到的结果重新赋值给first
75 transform (first.begin(), first.end(), second.begin(), first.begin(), op_sum); // first: 21 41 61 81 101
76 cout << "first contains:";
77 for (it=first.begin(); it!=first.end(); ++it)
78 cout << " " << *it;
79 cout << endl;
80 //*//////////////////////////////////////////////////////////////////////////
81
82 ///大小写转换/////////////////////////////////////
83 string strsrc("Hello, World!");
84 string strdest;
85 strdest.resize(strsrc.size()); // !!!必须预先设置一个大小与strsrc相同
86 transform(strsrc.begin(), strsrc.end(), strdest.begin(), to_upper); // 转换为大写
87 cout << strdest << endl;
88
89 transform(strsrc.begin(), strsrc.end(), strdest.begin(), to_lower); // 转换为小写
90 cout << strdest << endl;
91 //*/////////////////////////////////////////
92
93 return 0;
94 }
我们已经了解了一种区间元素交换swap_ranges函数,现在我们再来学习另外一种区间元素交换transform。该算法用于实现容器元素的变 换操作。有如下两个使用原型,一个将迭代器区间[first,last)中元素,执行一元函数对象op操作,交换后的结果放在 [result,result+(last-first))区间中。另一个将迭代器区间[first1,last1)的元素*i,依次与 [first2,first2+(last-first))的元素*j,执行二元函数操作binary_op(*i,*j),交换结果放在 [result,result+(last1-first1))。
函数原型:
[cpp] view plaincopy
template < class InputIterator, class OutputIterator, class UnaryOperator >
OutputIterator transform ( InputIterator first1, InputIterator last1,
OutputIterator result, UnaryOperator op );
template < class InputIterator1, class InputIterator2,
class OutputIterator, class BinaryOperator >
OutputIterator transform ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, OutputIterator result,
BinaryOperator binary_op );
参数说明:
first1, last1 指出要进行元素变换的第一个迭代器区间 [first1,last1)。 first2 指出要进行元素变换的第二个迭代器区间的首个元素的迭代器位置,该区间的元素个数和第一个区间相等。 result 指出变换后的结果存放的迭代器区间的首个元素的迭代器位置 op 用一元函数对象op作为参数,执行其后返回一个结果值。它可以是一个函数或对象内的类重载operator()。 binary_op 用二元函数对象binary_op作为参数,执行其后返回一个结果值。它可以是一个函数或对象内的类重载operator()。
程序示例:
[cpp] view plaincopy
/*******************************************************************
* Copyright (C) Jerry Jiang
*
* File Name : transform .cpp
* Author : Jerry Jiang
* Create Time : 2012-4-29 22:22:18
* Mail : jbiaojerry@gmail.com
* Blog : http://blog.csdn.net/jerryjbiao
*
* Description : 简单的程序诠释C++ STL算法系列之十八
* 变易算法 : 区间元素交换 transform
*
******************************************************************/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int op_increase (int i) { return ++i; }
int op_sum (int i, int j) { return i+j; }
int main () {
vector<int> first;
vector<int> second;
vector<int>::iterator it;
// set some values:
for (int i=1; i<6; i++) first.push_back (i*10); // first: 10 20 30 40 50
second.resize(first.size()); // allocate space
transform (first.begin(), first.end(), second.begin(), op_increase);
// second: 11 21 31 41 51
transform (first.begin(), first.end(), second.begin(), first.begin(), op_sum);
// first: 21 41 61 81 101
cout << "first contains:";
for (it=first.begin(); it!=first.end(); ++it)
cout << " " << *it;
cout << endl;
return 0;
}