程序设计与算法(三)第07周测验(2020春季)

001:简单的SumArray

输入

输出
TomJackMaryJohn
10

#include <iostream>
#include <string>
using namespace std;
template <class T>
T SumArray(
	// 在此处补充你的代码
//——————————————————
	T* s, T* e)
{
	T sum = *s;
	for (s++; s != e; s++)
		sum += *s;
	return sum;
//——————————————————
}
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;
}`

002:简单的foreach

输入
多组数据
每组数据第一行是两个整数 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;
}

003:简单的Filter

输入

输出
MikeJackLucy
3,4,5,

#include <iostream>
#include <string>
using namespace std;
// 在此处补充你的代码
//——————————————————————————————
template<class T>
T* Filter(T* s1, T* e1, T* s2, bool f(T)) {
	while (s1 != e1) {
		if (f(*s1)) {
			*s2 = *s1; s2++;
		}
		s1++;
	}
	return s2;
}
//——————————————————————————————
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;
}

004:你真的搞清楚为啥 while(cin >> n) 能成立了吗?

输入
多组数据,每组一行,是两个整数
输出
对每组数据,原样输出
当碰到输入中出现-1 时,程序结束
输入中保证会有 -1
样例输入
12 44
344 555
-1
2 3
样例输出
12 44
344 555

#include <iostream>
using namespace std;
class MyCin
{
    // 在此处补充你的代码
//————————————————————————
    bool state;
public:
    MyCin() :state(true) {}
    MyCin& operator>>(int& n) {
        //if (!state)return*this;
        cin >> n;
        if (n == -1)state = false;
        return *this;
    }
    operator bool() { return state; }
//————————————————————————
};
int main()
{
    MyCin m;
    int n1, n2;
    while (m >> n1 >> n2)
        cout << n1 << " " << n2 << endl;
    return 0;
}

005:山寨版istream_iterator

输入
第一行是整数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

#include <iostream>
#include <string>

using namespace std;
template <class T>
class CMyistream_iterator
{
	// 在此处补充你的代码
	T val;
public:
	CMyistream_iterator(istream& istr) { istr >> val; }
	CMyistream_iterator(T x) { val = x; }
	ostream& operator<<(ostream& ostr) {
		ostr << val; return ostr;
	}
	friend T operator*(CMyistream_iterator<T> ss) {
		return ss.val;
	}
	friend CMyistream_iterator operator++(CMyistream_iterator<T>& ss,int) {
		T tmp = ss.val;
		cin >> ss.val; 
		return CMyistream_iterator<T>(tmp);
	}
//————————————————————————————————————————————————————
};



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;
}

006:这个模板并不难

输入
多组数据。每组第一行是一个不含空格的字符串
第二行是整数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 {
	// 在此处补充你的代码
	T* p;
	int size;
public:
	myclass(T* s, int len) {
		p = new T[len]; size = len;
		for (int i = 0; i < len; 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;
}

007:排序,又见排序!

输入

输出
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* st, T1* ed, T2 judge) {
    int len = ed - st, flag = 1;
    while (flag) {
        flag = 0;
        for (int i = 0; i < len - 1; i++)
            if (judge(st[i + 1], st[i])) {
                flag = 1;
                T1 tmp = st[i];
                st[i] = st[i + 1];
                st[i + 1] = tmp;
            }
    }
}
//————————————————————————————————
#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;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JILIN.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值