题目来源:程序设计与算法(三)测验和作业题汇总
PS:可能我只是入门小白吧,C++对我来说就是一坨shit,我已经绕晕了!!!两年前做Qt项目的时候就觉得这玩意是个无底洞,现在在准备某校的机试,就更觉得了!!!
#include
#include
using namespace std;template
T SumArray (T *begin, T *end){T result;for (T *i = begin; i < end; i++)result += *i;return result;
}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
#include
using namespace std;// 在此处补充你的代码
template
void MyForeach (T *begin, T *end, F *func){ // func为函数指针 for (T *i = begin; i < end; i++)func(*i);
}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个整数
3 4
Tom Mike Jack
1 2 3 4
1 2
Peking
100 200
输出:对每组数据,第一行输出所有输入字符串连在一起的结果,第二行输出输入中的每个整数加1的结果
TomMikeJack
2,3,4,5,
Peking
101,201,
#include
#include
using namespace std;// 在此处补充你的代码
template
T *Filter (T *begin, T *end, T *result, F *func){ // func为函数指针int j = 0;for (T *i = begin; i < end; i++){if (func(*i)){result[j] = *i;j++;}}return &result[j];
}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); // 留下长度大于3的字符串for(int i = 0; i < p - as2; ++i) // p-as2是数组as2的长度 cout << as2[i];cout << endl; int * p2 = Filter(a1,a1+5,a2,LargerThan2); // 留下大于2的元素for(int i = 0; i < p2 - a2; ++i) // p2-a2是数组a2的长度 cout << a2[i] << ",";return 0;
}
输出:
MikeJackLucy
3,4,5,
#include
using namespace std;class MyCin
{
// 在此处补充你的代码
public:bool isStop;MyCin(): isStop(false) {}MyCin &operator >> (int &x){cin >> x;if (x == -1)isStop = true;return *this;} operator bool (){ // 对强制类型转换运算符bool进行重载 return !isStop;}
};int main()
{MyCin m;int n1, n2;while(m >> n1 >> n2) cout << n1 << " " << n2 << endl;return 0;
}
输入:多组数据,每组一行,是两个整数
12 44
344 555
-1
2 3
输出:对每组数据,原样输出;当碰到输入中出现-1时,程序结束。输入中保证会有-1
12 44
344 555
(略)
#include
#include
#include
using namespace std;template
class myclass {
// 在此处补充你的代码
public:T *p;int size;myclass(T *a, int len) {size = len;p = new T[len+1]; // 考虑到字符串的\0 for (int i = 0; i < len; i++)p[i] = a[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 obj(line, strlen(line));obj.Show();int n;cin >> n;for(int i = 0; i < n; ++i)cin >> a[i];myclass obj2(a, n);obj2.Show();}return 0;
}
输入:多组数据。每组第一行是一个不含空格的字符串,第二行是整数n,第三行是n个整数
Tom
3
3 4 5
Jack
4
1 2 3 4
输出:对每组数据,先依次输出输入字符串的每个字母,并且在每个字母后面加逗号,然后依次再输出输入的n个整数 ,在每个整数后面加逗号
T,o,m,
3,4,5,
J,a,c,k,
1,2,3,4,
#include
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
void mysort(T1 *begin, T1 *end, T2 (*func)(T1, T1)){ // 回调函数:函数指针作为函数的参数int len = end - begin - 1;for (int i = 0; i < len; i++)for (int j = 0; j < len - i; j++)if (func(begin[j+1], begin[j])){T1 tmp = begin[j];begin[j] = begin[j+1];begin[j+1] = tmp;}
}#define NUM 5int 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
using namespace std;template
struct GoodCopy { // 函数对象,GoodCopy是一个类名,但可以当作函数名
// 在此处补充你的代码void operator () (T *begin, T *end, T *init){int len = end - begin;T *tmp = new T[len];for (int i = 0; i < len; ++i) // 先复制到一个中间缓存区 tmp[i] = begin[i];for (int i = 0; i < len; ++i) // 再将 中间缓存区 的数据复制到 目标地址 init[i] = tmp[i];}
};int a[200];
int b[200];
string c[200];
string d[200];template
void Print(T s,T e) {for(; s != e; ++s)cout << * s << ",";cout << endl;
}int main()
{int t;cin >> t;while( t-- ) {int m ;cin >> m;for(int i = 0; i < m; ++i)cin >> a[i];GoodCopy()(a, a+m, b);Print(b, b+m);GoodCopy()(a, a+m, a+m/2);Print(a+m/2, a+m/2+m);for(int i = 0; i < m; ++i)cin >> c[i];GoodCopy()(c, c+m, d);Print(c, c+m);GoodCopy()(c, c+m, c+m/2);Print(c+m/2, c+m/2+m);}return 0;
}
输入:第一行是整数 t,表示数据组数
每组数据:
第一行是整数 n , n < 50
第二行是 n 个整数
第三行是 n 个字符串
2
4
1 2 3 4
Tom Jack Marry Peking
1
0
Ted
输出:将输入的整数原序输出两次,用","分隔
然后将输入的字符串原序输出两次,也用 ","分隔
1,2,3,4,
1,2,3,4,
Tom,Jack,Marry,Peking,
Tom,Jack,Marry,Peking,
0,
0,
Ted,
Ted,
输入:多组数据,每组一行,是一个整数n和一个字符串s
2 a123456
4 a12345
输出:定义两个整数的距离为两个整数差的绝对值
定义两个字符串的距离为两个字符串长度差的绝对值
对每组数据:
对数组a按和n的距离从小到大排序后输出。距离相同的,值小的排在前面。
然后对数组b,按照和s的距离从小到大输出。距离相同的,字典序小的排在前面
1,3,0,4,7,8,9,10,15,20,
American,Peking,123456789,Jack,To,abcdefghijklmnop,
4,3,1,7,0,8,9,10,15,20,
Peking,American,Jack,123456789,To,abcdefghijklmnop,
题解:
(没看懂题意,不做了)
(啊,还能这样操作,写的什么呀,完全看不懂,网上答案照着写的,像我这种小白就蒙混不了了)
#include
#include
#include
using namespace std;template
class CArray3D
{
// 在此处补充你的代码private:int layer;int row;int col;T *data;public:CArray3D (int l, int r, int c): layer(l), row(r), col(c) {data = new T[layer*row*col];}~CArray3D (){delete [] data;}class CArray2D{private:int row;int col;T *data;public:CArray2D (T *d, int r, int c): data(d), row(r), col(c) {}T* operator [] (const int r){return data + r * col;}operator T* (){return data;}};CArray2D operator [] (const int l){return CArray2D(data + l*row*col, row, col);}
};CArray3D a(3,4,5);
CArray3D b(3,2,2);void PrintA()
{for(int i = 0; i < 3; ++i) {cout << "layer " << i << ":" << endl;for(int j = 0; j < 4; ++j) {for(int k = 0; k < 5; ++k) cout << a[i][j][k] << "," ;cout << endl;}}
}void PrintB()
{for(int i = 0; i < 3; ++i) {cout << "layer " << i << ":" << endl;for(int j = 0; j < 2; ++j) {for(int k = 0; k < 2; ++k) cout << b[i][j][k] << "," ;cout << endl;}}
}int main()
{int No = 0;for( int i = 0; i < 3; ++i ) {a[i];for( int j = 0; j < 4; ++j ) {a[j][i];for( int k = 0; k < 5; ++k )a[i][j][k] = No++;a[j][i][i]; }}PrintA();memset(a[1], -1, 20*sizeof(int)); memset(a[1], -1, 20*sizeof(int));PrintA(); memset(a[1][1], 0, 5*sizeof(int)); PrintA();for( int i = 0; i < 3; ++i )for( int j = 0; j < 2; ++j )for( int k = 0; k < 2; ++k )b[i][j][k] = 10.0 / (i+j+k+1);PrintB();int n = a[0][1][2];double f = b[0][1][1];cout << "****" << endl;cout << n << "," << f << endl;return 0;
}
输出:
layer 0:
0,1,2,3,4,
5,6,7,8,9,
10,11,12,13,14,
15,16,17,18,19,
layer 1:
20,21,22,23,24,
25,26,27,28,29,
30,31,32,33,34,
35,36,37,38,39,
layer 2:
40,41,42,43,44,
45,46,47,48,49,
50,51,52,53,54,
55,56,57,58,59,
layer 0:
0,1,2,3,4,
5,6,7,8,9,
10,11,12,13,14,
15,16,17,18,19,
layer 1:
-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,
layer 2:
40,41,42,43,44,
45,46,47,48,49,
50,51,52,53,54,
55,56,57,58,59,
layer 0:
0,1,2,3,4,
5,6,7,8,9,
10,11,12,13,14,
15,16,17,18,19,
layer 1:
-1,-1,-1,-1,-1,
0,0,0,0,0,
-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,
layer 2:
40,41,42,43,44,
45,46,47,48,49,
50,51,52,53,54,
55,56,57,58,59,
layer 0:
10,5,
5,3.33333,
layer 1:
5,3.33333,
3.33333,2.5,
layer 2:
3.33333,2.5,
2.5,2,
****
7,3.33333
#include
#include
using namespace std;struct A {int v;A() { }A(int n):v(n) { };bool operator<(const A & a) const {return v < a.v;}
};// 在此处补充你的代码
template
class FilterClass{private:T low;T high;public:FilterClass (T l, T h): low(l), high(h) {}bool operator () (T o){return (low < o) && (o < high);}
};template
void Print(T s,T e)
{for(;s!=e; ++s)cout << *s << ",";cout << endl;
}template
T2 Filter(T1 s, T1 e, T2 s2, T3 op)
{for(;s != e; ++s) {if (op(*s)) { // 函数对象的调用(一个参数)* s2 = * s;++s2;}}return s2;
}ostream & operator <<(ostream & o,A & a)
{o << a.v;return o;
}vector ia;
vector aa; int main()
{int m,n;while(cin >> m >> n) {ia.clear();aa.clear(); int k,tmp;cin >> k;for(int i = 0; i < k; ++i) {cin >> tmp; ia.push_back(tmp);aa.push_back(tmp); }vector ib(k);vector ab(k);vector::iterator p = Filter(ia.begin(),ia.end(),ib.begin(),FilterClass(m,n)); // 实例化FilterClass(两个参数) Print(ib.begin(),p);vector::iterator pp = Filter(aa.begin(),aa.end(),ab.begin(),FilterClass(m,n)); // 实例化FilterClass(两个参数)Print(ab.begin(),pp);}return 0;
}
输入:
多组数据
每组数据两行
第一行是两个整数 m 和 n
第二行先是一个整数k,然后后面跟着k个整数
1 3
1 2
2 8
5 1 2 3 4 9
输出:
对每组数据,按原顺序输出第二行的后k个整数中,大于m且小于n的数
输出两遍
数据保证一定能找到符合要求的整数
2,
2,
3,4,
3,4,
#include
#include
#include
#include
using namespace std;int main()
{ double a[] = {1.2,3.4,9.8,7.3,2.6};list lst(a,a+5);lst.sort( [=](double x, double y)->bool{return x > y;} ); // Lambda表达式 for(list::iterator i = lst.begin(); i != lst.end(); ++i) cout << * i << "," ;return 0;
}
输出:
9.8,7.3,3.4,2.6,1.2,
(略)
描述:写一个程序完成以下命令:
new id ——新建一个指定编号为id的序列(id < 10000)
add id num——向编号为id的序列加入整数
merge id1 id2——如果id1等于id2,不做任何事,否则归并序列id1和id2中的数,并将id2清空
unique id——去掉序列id中重复的元素
out id ——从小到大输出编号为id的序列中的元素,以空格隔开
输入:第一行一个数n,表示有多少个命令( n<=200000)。以后n行每行一个命令。
16
new 1
new 2
add 1 1
add 1 2
add 1 3
add 2 1
add 2 2
add 2 3
add 2 4
out 1
out 2
merge 1 2
out 1
out 2
unique 1
out 1
输出:按题目要求输出。
1 2 3
1 2 3 4
1 1 2 2 3 3 41 2 3 4
题解:
#include
#include
描述:现有一整数集(允许有重复元素),初始为空。我们定义如下操作:
add x 把x加入集合
del x 把集合中所有与x相等的元素删除
ask x 对集合中元素x的情况询问
对每种操作,我们要求进行如下输出。
add 输出操作后集合中x的个数
del 输出操作前集合中x的个数
ask 先输出0或1表示x是否曾被加入集合(0表示不曾加入),再输出当前集合中x的个数,中间用空格格开。
输入:第一行是一个整数n,表示命令数。0<=n<=100000。后面n行命令,如Description中所述。
7
add 1
add 1
ask 1
ask 2
del 2
del 1
ask 1
输出:共n行,每行按要求输出。
1
2
1 2
0 0
0
2
1 0
题解:
#include
#include
#include
using namespace std;typedef multiset MSET;int main(){int n, x;MSET s, record; // record用来记录是否加入过集合s string cmd;cin >> n;s.clear();record.clear();while (n--){cin >> cmd;if (cmd == "add"){cin >> x;s.insert(x);record.insert(x);cout << s.count(x) << endl;}else if (cmd == "del"){ cin >> x;cout << s.count(x) << endl;s.erase(x);}else if (cmd == "ask"){ cin >> x;bool flag = (record.find(x) != record.end());cout << flag << " " << s.count(x) << endl;}}return 0;
}