1. 程式人生 > >北京大學MOOC C++程式設計 程式設計與演算法(三)第七週測驗

北京大學MOOC C++程式設計 程式設計與演算法(三)第七週測驗

1:簡單的SumArray

描述

填寫模板 PrintArray,使得程式輸出結果是: TomJackMaryJohn 10 不得編寫SumArray函式

#include <iostream>
#include <string>
using namespace std;
template <class T>
T SumArray(
// 在此處補充你的程式碼
}
int main() {
	string array[4] = { "Tom","Jack","Mary","John"};
	cout << SumArray(array,array+4) << endl;
	int a[4] = { 1, 2, 3, 4};  //提示:1+2+3+4 = 10
	cout << SumArray(a,a+4) << endl;
	return 0;
}

輸入

輸出

TomJackMaryJohn 10

答案:

#include <iostream>
#include <string>
using namespace std;
template <class T>
T SumArray(
T *s, T *e)
{
    T temp = *s;
    s++;
    while(s != e)
    {
        temp += *s;
        s++;
    }
    return temp;
}
int main() {
	string array[4] = { "Tom","Jack","Mary","John"};
	cout << SumArray(array,array+4) << endl;
	int a[4] = { 1, 2, 3, 4};  //提示:1+2+3+4 = 10
	cout << SumArray(a,a+4) << endl;
	return 0;
}

2:簡單的foreach

編寫MyForeach模板,使程式按要求輸出 不得編寫 MyForeach函式

#include <iostream>
#include <string>
using namespace std;
// 在此處補充你的程式碼
void Print(string s)
{
	cout << s;
}
void Inc(int & n)
{
	++ n;
}
string array[100];
int a[100];
int main() {
	int m,n;
	while(cin >> m >> n) {
		for(int i = 0;i < m; ++i)
			cin >> array[i];
		for(int j = 0; j < n; ++j)
			cin >> a[j];
		MyForeach(array,array+m,Print);		 
		cout << endl;
		MyForeach(a,a+n,Inc);		 
		for(int i = 0;i < n; ++i)
			cout << a[i] << ",";
		cout << endl;
	}
	return 0;
}

輸入

多組資料 每組資料第一行是兩個整數 m 和 n ,都不超過 50 第二行是m個不帶空格的字串 第三行是 n個整數

輸出

對每組資料 第一行輸出所有輸入字串連在一起的結果 第二行輸出輸入中的每個整數加1的結果

樣例輸入

3 4
Tom Mike Jack
1 2 3 4
1 2
Peking
100 200

樣例輸出

TomMikeJack
2,3,4,5,
Peking
101,201,

答案:

#include <iostream>
#include <string>
using namespace std;
template<class T, class Pred>
void MyForeach(T s, T e ,Pred op)
{
    while(s != e)
    {
        op(*s);
        s++;
    }
}
void Print(string s)
{
	cout << s;
}
void Inc(int & n)
{
	++ n;
}
string array[100];
int a[100];
int main() {
	int m,n;
	while(cin >> m >> n) {
		for(int i = 0;i < m; ++i)
			cin >> array[i];
		for(int j = 0; j < n; ++j)
			cin >> a[j];
		MyForeach(array,array+m,Print);		 
		cout << endl;
		MyForeach(a,a+n,Inc);		 
		for(int i = 0;i < n; ++i)
			cout << a[i] << ",";
		cout << endl;
	}
	return 0;
}

3:簡單的Filter

編寫Filter模板,使得程式產生指定輸出 不得編寫 Filter函式

#include <iostream>
#include <string>
using namespace std;
// 在此處補充你的程式碼
bool LargerThan2(int n)
{
	return n > 2;
}
bool LongerThan3(string s) 
{
	return s.length() > 3;
}

string as1[5] = {"Tom","Mike","Jack","Ted","Lucy"};
string as2[5];
int  a1[5] = { 1,2,3,4,5};
int a2[5];
int main() {
	string * p = Filter(as1,as1+5,as2,LongerThan3);
	for(int i = 0;i < p - as2; ++i)
		cout << as2[i];
	cout << endl; 
	int * p2 = Filter(a1,a1+5,a2,LargerThan2);
	for(int i = 0;i < p2-a2; ++i)
		cout << a2[i] << ",";
	return 0;
}

輸入

輸出

MikeJackLucy 3,4,5,

答案:

#include <iostream>
#include <string>
using namespace std;
template <class T, class Pred>
T Filter(T s, T e,T r, Pred op)
{
    while(s != e)
    {
        if(op(*s))
        {
            *r = *s;
            r++;
        }
        s++;
    }
    return r;
}
bool LargerThan2(int n)
{
	return n > 2;
}
bool LongerThan3(string s) 
{
	return s.length() > 3;
}

string as1[5] = {"Tom","Mike","Jack","Ted","Lucy"};
string as2[5];
int  a1[5] = { 1,2,3,4,5};
int a2[5];
int main() {
	string * p = Filter(as1,as1+5,as2,LongerThan3);
	for(int i = 0;i < p - as2; ++i)
		cout << as2[i];
	cout << endl; 
	int * p2 = Filter(a1,a1+5,a2,LargerThan2);
	for(int i = 0;i < p2-a2; ++i)
		cout << a2[i] << ",";
	return 0;
}

4:你真的搞清楚為啥 while(cin >> n) 能成立了嗎?

讀入兩個整數,輸出兩個整數 ,直到碰到-1

#include <iostream>
using namespace std;
class MyCin
{
// 在此處補充你的程式碼
};
int main()
{
    MyCin m;
    int n1,n2;
    while( m >> n1 >> n2) 
        cout  << n1 << " " << n2 << endl;
    return 0;
}

輸入

多組資料,每組一行,是兩個整數

輸出

對每組資料,原樣輸出  當碰到輸入中出現-1 時,程式結束  輸入中保證會有 -1

樣例輸入

12 44
344 555
-1
2 3

樣例輸出

12 44
344 555

答案:

#include <iostream>
using namespace std;
class MyCin
{
public:
    int flag;
    MyCin():flag(0){}
    istream & operator >>(int & x)
    {
        cin >> x;
        if(x == -1)
            exit(0);
        return cin;
    }
};
int main()
{
    MyCin m;
    int n1,n2;
    while( m >> n1 >> n2) 
        cout  << n1 << " " << n2 << endl;
    return 0;
}

5:山寨版istream_iterator

模仿C++標準模板庫istream_iterator用法,實現CMyistream_iterator使得程式按要求輸出

#include <iostream>
#include <string>

using namespace std;
template <class T>
class CMyistream_iterator
{
// 在此處補充你的程式碼
};



int main()  
{ 
	int t;
	cin >> t;
	while( t -- ) {
		 CMyistream_iterator<int> inputInt(cin);
		 int n1,n2,n3;
		 n1 = * inputInt; //讀入 n1
		 int tmp = * inputInt;
		 cout << tmp << endl;
		 inputInt ++;   
		 n2 = * inputInt; //讀入 n2
		 inputInt ++;
		 n3 = * inputInt; //讀入 n3
		 cout << n1 << " " << n2<< " " << n3 << " ";
		 CMyistream_iterator<string> inputStr(cin);
		 string s1,s2;
		 s1 = * inputStr;
		 inputStr ++;
		 s2 = * inputStr;
		 cout << s1 << " " << s2 << endl;
	}
	 return 0;  
}

輸入

第一行是整數t,表示有t組資料 每組資料一行,三個整數加兩個字串。字串是不含空格的

輸出

對每組資料,輸出二行  在第一行輸出第一個數 第二行原樣輸出輸入的內容

樣例輸入

2
79 90 20 hello me
12 34 19 take up

樣例輸出

79
79 90 20 hello me
12
12 34 19 take up

提示

C++標準模板庫 istream_iterator模版使用說明: 其建構函式執行過程中就會要求輸入,然後每次執行++,則讀取輸入流中的下一個專案,執行 * 則返回上次從輸入流中讀取的專案。例如,下面程式執行時,就會等待使用者輸入資料,輸入資料後程序才會結束: #include  #include  using namespace std; int main() {  istream_iterator inputInt(cin); return 0;  } 下面程式執行時,如果輸入 12 34 程式輸出結果是: 12,12 #include  #include  using namespace std; int main()  {  istream_iterator inputInt(cin); cout << * inputInt << "," << * inputInt << endl; return 0;  } 下面程式執行時,如果輸入 12 34 56程式輸出結果是: 12,56 #include  #include  using namespace std; int main()  {  istream_iterator inputInt(cin); cout << * inputInt << "," ; inputInt ++; inputInt ++; cout << * inputInt; return 0;  }

答案:

#include <iostream>
#include <string>

using namespace std;
template <class T>
class CMyistream_iterator
{
public:
    T value;
    CMyistream_iterator(istream & i)
    {
        i >> value;
    }
    void operator ++(int)
    {
        cin>>value;
    }
    T operator *()
    {
        return value;
    }
};



int main()  
{ 
	int t;
	cin >> t;
	while( t -- ) {
		 CMyistream_iterator<int> inputInt(cin);
		 int n1,n2,n3;
		 n1 = * inputInt; //讀入 n1
		 int tmp = * inputInt;
		 cout << tmp << endl;
		 inputInt ++;   
		 n2 = * inputInt; //讀入 n2
		 inputInt ++;
		 n3 = * inputInt; //讀入 n3
		 cout << n1 << " " << n2<< " " << n3 << " ";
		 CMyistream_iterator<string> inputStr(cin);
		 string s1,s2;
		 s1 = * inputStr;
		 inputStr ++;
		 s2 = * inputStr;
		 cout << s1 << " " << s2 << endl;
	}
	 return 0;  
}

6:這個模板並不難

程式填空,輸出指定結果

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
template <class T>  
class myclass {
// 在此處補充你的程式碼
~myclass( ) {
		delete [] p;
	}
	void Show()
	{
		for( int i = 0;i < size;i ++ ) {
			cout << p[i] << ",";
		}
		cout << endl;
	}
};
int a[100];
int main() {
	char line[100];
	while( cin >> line ) {
		myclass<char> obj(line,strlen(line));;
		obj.Show();
		int n;
		cin >> n;
		for(int i = 0;i < n; ++i)
			cin >> a[i];
		myclass<int> obj2(a,n);
		obj2.Show();
	}
	return 0;
}

輸入

多組資料。每組第一行是一個不含空格的字串 第二行是整數n 第三行是n個整數

輸出

對每組資料,先依次輸出輸入字串的每個字母,並且在每個字母后面加逗號 然後依次再輸出輸入的n個整數 ,在每個整數後面加逗號

樣例輸入

Tom 
3
3 4 5
Jack
4
1 2 3 4

樣例輸出

T,o,m,
3,4,5,
J,a,c,k,
1,2,3,4,

答案:

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
template <class T>  
class myclass {
public:
    T *p;
    int size;
    myclass(T s[], int ssize)
    {
        p = new T[ssize];
        size = ssize;
        for(int i = 0;i < ssize;i++)
            p[i] = s[i];
    }
~myclass( ) {
		delete [] p;
	}
	void Show()
	{
		for( int i = 0;i < size;i ++ ) {
			cout << p[i] << ",";
		}
		cout << endl;
	}
};
int a[100];
int main() {
	char line[100];
	while( cin >> line ) {
		myclass<char> obj(line,strlen(line));;
		obj.Show();
		int n;
		cin >> n;
		for(int i = 0;i < n; ++i)
			cin >> a[i];
		myclass<int> obj2(a,n);
		obj2.Show();
	}
	return 0;
}

7:排序,又見排序!

自己編寫一個能對任何型別的陣列進行排序的mysort函式模版。只能寫一個mysort模板,不能寫mysort函式!

#include <iostream>
using namespace std;

bool Greater2(int n1,int n2) 
{
	return n1 > n2;
}
bool Greater1(int n1,int n2) 
{
	return n1 < n2;
}
bool Greater3(double d1,double d2)
{
	return d1 < d2;
}

template <class T1,class T2>
void mysort(
// 在此處補充你的程式碼
#define NUM 5
int main()
{
    int an[NUM] = { 8,123,11,10,4 };
    mysort(an,an+NUM,Greater1); //從小到大排序 
    for( int i = 0;i < NUM; i ++ )
       cout << an[i] << ",";
    mysort(an,an+NUM,Greater2); //從大到小排序 
    cout << endl;
    for( int i = 0;i < NUM; i ++ )
        cout << an[i] << ","; 
    cout << endl;
    double d[6] = { 1.4,1.8,3.2,1.2,3.1,2.1};
    mysort(d+1,d+5,Greater3); //將陣列從下標1到下標4從小到大排序 
    for( int i = 0;i < 6; i ++ )
         cout << d[i] << ","; 
	return 0;
}

輸入

輸出

4,8,10,11,123, 123,11,10,8,4, 1.4,1.2,1.8,3.1,3.2,2.1,

答案:

#include <iostream>
using namespace std;

bool Greater2(int n1,int n2) 
{
	return n1 > n2;
}
bool Greater1(int n1,int n2) 
{
	return n1 < n2;
}
bool Greater3(double d1,double d2)
{
	return d1 < d2;
}

template <class T1,class T2>
void mysort(
T1* s, T1* e, T2 op)
{
    int n = e - s;
    int i, j;
    for(i = 0; i < n; i++)
    {
        for(j = i+1; j < n; j++)
        {
            if(!op(*(s+i),*(s+j)))
            {
                T1 temp = *(s+i);
                *(s+i) =*(s+j);
                *(s+j) = temp;
            }
        }
    }
}
#define NUM 5
int main()
{
    int an[NUM] = { 8,123,11,10,4 };
    mysort(an,an+NUM,Greater1); //從小到大排序 
    for( int i = 0;i < NUM; i ++ )
       cout << an[i] << ",";
    mysort(an,an+NUM,Greater2); //從大到小排序 
    cout << endl;
    for( int i = 0;i < NUM; i ++ )
        cout << an[i] << ","; 
    cout << endl;
    double d[6] = { 1.4,1.8,3.2,1.2,3.1,2.1};
    mysort(d+1,d+5,Greater3); //將陣列從下標1到下標4從小到大排序 
    for( int i = 0;i < 6; i ++ )
         cout << d[i] << ","; 
	return 0;
}