1. 程式人生 > >A+B for Matrices 及 C++ transform的用法

A+B for Matrices 及 C++ transform的用法

題目大意:給定兩個矩陣,矩陣的最大大小是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))。

     函式原型:

  1. template < class InputIterator, class OutputIterator, class UnaryOperator >  
  2.   OutputIterator transform ( InputIterator first1, InputIterator last1,  
  3.                              OutputIterator result, UnaryOperator op );  
  4. template < class InputIterator1, class InputIterator2,  
  5.            class OutputIterator, class BinaryOperator >  
  6.   OutputIterator transform ( InputIterator1 first1, InputIterator1 last1,  
  7.                              InputIterator2 first2, OutputIterator result,  
  8.                              BinaryOperator binary_op );  

     引數說明:

first1, last1 
指出要進行元素變換的第一個迭代器區間 [first1,last1)。 
first2 
指出要進行元素變換的第二個迭代器區間的首個元素的迭代器位置,該區間的元素個數和第一個區間相等。 
  
result 
指出變換後的結果存放的迭代器區間的首個元素的迭代器位置 
op 
用一元函式物件op作為引數,執行其後返回一個結果值。它可以是一個函式或物件內的類過載operator()。 
binary_op 
用二元函式物件binary_op作為引數,執行其後返回一個結果值。它可以是一個函式或物件內的類過載operator()。

      程式示例:

    1. /*******************************************************************   
    2.  * Copyright (C) Jerry Jiang   
    3.  *                  
    4.  * File Name   : transform .cpp   
    5.  * Author      : Jerry Jiang   
    6.  * Create Time : 2012-4-29 22:22:18   
    7.  * Mail        : [email protected]   
    8.  * Blog        : http://blog.csdn.net/jerryjbiao    
    9.  *                  
    10.  * Description : 簡單的程式詮釋C++ STL算法系列之十八                     
    11.  *               變易演算法 : 區間元素交換 transform  
    12.  *                  
    13.  ******************************************************************/
    14. #include <iostream>
    15. #include <algorithm>
    16. #include <vector>
    17. usingnamespace std;  
    18. int op_increase (int i) { return ++i; }  
    19. int op_sum (int i, int j) { return i+j; }  
    20. int main () {  
    21.   vector<int> first;  
    22.   vector<int> second;  
    23.   vector<int>::iterator it;  
    24.   // set some values:
    25.   for (int i=1; i<6; i++) first.push_back (i*10); //  first: 10 20 30 40 50
    26.   second.resize(first.size());     // allocate space
    27.   transform (first.begin(), first.end(), second.begin(), op_increase);  
    28.                                                   // second: 11 21 31 41 51
    29.   transform (first.begin(), first.end(), second.begin(), first.begin(), op_sum);  
    30.                                                   //  first: 21 41 61 81 101
    31.   cout << "first contains:";  
    32.   for (it=first.begin(); it!=first.end(); ++it)  
    33.     cout << " " << *it;  
    34.   cout << endl;  
    35.   return

      相關推薦

      A+B for Matrices C++ transform用法

      題目大意:給定兩個矩陣,矩陣的最大大小是M*N(小於等於10),矩陣元素的值的絕對值小於等於100,求矩陣相加後全0的行以及列數。 1 #include<iostream> 2 using namespace std; 3 #define N 10 4 5 int main()

      A+B for Matrices

      1.題目描述 This time, you are supposed to find A+B where A and B are two matrices, and then count the number of zero rows and columns. 輸入 The input

      【九度】題目1001:A+B for Matrices

      題目描述:     This time, you are supposed to find A+B where A and B are two matrices, and then count the number of zero rows and columns. 輸入

      A+B for Matrices(矩陣加法)

      題目描述:     This time, you are supposed to find A+B where A and B are two matrices, and then count the number of zero rows and columns. 輸入:     The inpu

      九度——題目1001:A+B for Matrices

      題目描述:     This time, you are supposed to find A+B where A and B are two matrices, and then count the number of zero rows and columns. 輸入

      九度考研真題 浙大 2011-1浙大1001:A+B for Matrices

      //題目1001:A+B for Matrices #include<iostream> #include<string.h> using namespace std; int main() {int M,N;int a1[11][11],a2[11

      九度oj 題目1001:A+B for Matrices 【ZJU2011考研機試題1】

      題目1001:A+B for Matrices 時間限制:1 秒 記憶體限制:32 兆 特殊判題:否 提交:11539 解決:4694 題目描述:     This time, you are supposed to find A+B where

      A+B for Matrices(JAVA)

      題目描述:     This time, you are supposed to find A+B where A and B are two matrices, and then count the number of zero rows and columns. 輸入

      PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分)(C++)(甲級)

      1002 A+B for Polynomials (25 分) This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: Each input

      hdu 1867 A + B for you again

      roc stdio.h 相同 tween them substr bsp sdf des A + B for you again Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (

      java hdu A+B for Input-Output Practice (III)

      是否 for left bigint desc accep 以及 next sed A+B for Input-Output Practice (III) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6

      Hdu 1091 A+B for Input-Output Practice (III)

      bsp time blog for in sse log pro sca ces A+B for Input-Output Practice (III) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6553

      Hdu 1094 A+B for Input-Output Practice (VI)

      and highlight tar turn multi for in sca ger contain A+B for Input-Output Practice (VI) Time Limit: 2000/1000 MS (Java/Others) Memory L

      Hdu 1090 A+B for Input-Output Practice (II)

      miss should sam ota main clu sub logs pac A+B for Input-Output Practice (II) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6553

      Hdu 1096 A+B for Input-Output Practice (VIII)

      accep each sca mit amp brush for esc cpp A+B for Input-Output Practice (VIII) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 655

      Hdu 1092 A+B for Input-Output Practice (IV)

      amp miss sse tput pro mis calculate star des A+B for Input-Output Practice (IV) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6

      甲級1002 A+B for Polynomials (25)

      指數 lines term ios esp printf contains n) lin 題目描述: This time, you are supposed to find A+B where A and B are two polynomials. Input Ea

      hdu - 1867 - A + B for you again

      selected blank tar tro .net ini rip res second A + B for you again Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K

      HDU1867 A + B for you again(KMP)

      字典 不一定 string 連接 title pan log cst tail A + B for you again Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O

      PAT 甲級1002 A+B for Polynomials (25)

      文本 please terms struct suppose 作者 notice opera and 1002. A+B for Polynomials (25) 時間限制 400 ms 內存限制 65536 kB 代碼長度限制 16000 B 判題程序 S