程序设计与算法八周测验

2:按距离排序

#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
template <class T1,class T2>
struct Closer {
//补充代码
};

int Distance1(int n1,int n2) {
	return abs(n1-n2);
}
int Distance2(const string & s1, const string & s2)
{
	return abs((int)s1.length()- (int) s2.length());
}
int a[10] = { 0,3,1,4,7,9,20,8,10,15};
string b[6] = {"American","Jack","To","Peking","abcdefghijklmnop","123456789"};
int main()
{
	int n;string s;
	while( cin >> n >> s ) {
		sort(a,a+10,Closer<int ,int (*)(int ,int)> (n,Distance1));
		for(int i = 0;i < 10; ++i)
			cout << a[i] << "," ;
		cout << endl;
		sort(b,b+6,Closer<string,int (*)(const string &,const string &  )> (s,Distance2)); 
		for(int i = 0;i < 6; ++i)
			cout << b[i] << "," ;
		cout << endl;
	}
	return 0;
}

输入

多组数据,每组一行,是一个整数ñ和一个字符串小号

输出

定义两个整数的距离为两个整数差的绝对值
定义两个字符串的距离为两个字符串长度差的绝对值 

对每组数据:
对数组一个按和Ñ的距离从小到大排序后输出。距离相同的,值小的排在前面。
然后对数组b中,按照和小号的距离从小到大输出。距离相同的,字典序小的排在前面

样例输入

2 a123456
4 a12345

样例输出

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 <iostream>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
template <class T1, class T2>
struct Closer {
	//your code starts here
	T1 n;
	T2 op;
	Closer(T1 n1, T2 n2) :n(n1), op(n2) {	}
	bool operator()(const T1& a1, const T1& a2) {//这里的a1,a2为int类型,为传值,来自sort中的a
		int x1 = op(a1, n);//注意这里是int不是T1
		int x2 = op(a2, n);
		if (x1 > x2) {
			return false;
		}
		else if (x1 < x2) {//和规则一样,小的留下
			return true;
		}
		else {
			return a1 < a2;
		}
	}
	//your code ends here	
};

int Distance1(int n1, int n2) {
	return abs(n1 - n2);
}
int Distance2(const string & s1, const string & s2)
{
	return abs((int)s1.length() - (int)s2.length());
}
int a[10] = { 0,3,1,4,7,9,20,8,10,15 };
string b[6] = { "American","Jack","To","Peking","abcdefghijklmnop","123456789" };
int main()
{
	int n; string s;
	while (cin >> n >> s) {
		sort(a, a + 10,
			Closer<int, int(*)(int, int)>(n, Distance1));
		for (int i = 0; i < 10; ++i)
			cout << a[i] << ",";
		cout << endl;
		sort(b, b + 6,
			Closer<string, int(*)(const string &, const string &)>(s, Distance2));
		for (int i = 0; i < 6; ++i)
			cout << b[i] << ",";
		cout << endl;
	}
	return 0;
}

 

3:很难蒙混过关的CArray3d三维数组模板类

答案看不懂233333

 

5:白给的名单排序

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
int main()
{	
	double a[] = {1.2,3.4,9.8,7.3,2.6};
	list<double> lst(a,a+5);
	lst.sort(//补充代码
);
	
	for(list<double>::iterator i  = lst.begin(); i != lst.end(); ++i) 
		cout << * i << "," ;
    return 0;
}

输入

输出

9.8,7.3,3.4,2.6,1.2,

 

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
int main()
{
	double a[] = { 1.2,3.4,9.8,7.3,2.6 };
	list<double> lst(a, a + 5);
	lst.sort(greater<double>()//list有个greater的排序方式//不过VS2015貌似不认
		//don't forget double
	);

	for (list<double>::iterator i = lst.begin(); i != lst.end(); ++i)
		cout << *i << ",";
	return 0;
}

 

6:我自己的ostream_iterator

#include <iostream>
#include <list>
#include <string>
using namespace std;

template <class T1,class T2>
void Copy(T1 s,T1 e, T2 x)
{
	for(; s != e; ++s,++x)
		*x = *s;
}

 
template<class T>
class myostream_iteraotr
{//补充代码
};


int main()
{	const int SIZE = 5;
	int a[SIZE] = {5,21,14,2,3};
	double b[SIZE] = { 1.4, 5.56,3.2,98.3,3.3};
	list<int> lst(a,a+SIZE);
	myostream_iteraotr<int> output(cout,",");
	Copy( lst.begin(),lst.end(),output); 
	cout << endl;
	myostream_iteraotr<double> output2(cout,"--");
	Copy(b,b+SIZE,output2);
	return 0;
}

输入

输出

5,21,14,2,3,
1.4--5.56--3.2--98.3--3.3--

 

/*
程序填空输出指定结果

输入:
无
输出:


5,21,14,2,3,
1.4--5.56--3.2--98.3--3.3--

输入样例
无
输出样例
同输出
*/

#include <iostream>
#include <list>
#include <string>
using namespace std;

template <class T1, class T2>
void Copy(T1 s, T1 e, T2 x)
{
	for (; s != e; ++s, ++x)
		*x = *s;
}


template<class T>
class myostream_iteraotr
{
	//your code starts here 
	ostream &o;//这里必须是ostream&
/*ostream os //不可以
因为ostream设计上是一个基类,它抽象了底层的行为,它本身什么功能都没有,当然不允许构造实例。
ostream *os = new ofstream();
作为基类指针完全可以。*/
	string s;
public:
	void operator ++() { }
	myostream_iteraotr(ostream& o1, const string& s1) :o(o1), s(s1) {}
	myostream_iteraotr& operator*() {
		return *this;
	}
	/*void operator=(T& x){
		o<<x<<s;
	}*///对这题而言这个写法没错,但是不够优雅
	myostream_iteraotr & operator =(const T & t)
	{
		o << t << s;
		return *this;
	}
	//your code ends here	
};


int main()
{
	const int SIZE = 5;
	int a[SIZE] = { 5,21,14,2,3 };
	double b[SIZE] = { 1.4, 5.56,3.2,98.3,3.3 };
	list<int> lst(a, a + SIZE);
	myostream_iteraotr<int> output(cout, ",");
	Copy(lst.begin(), lst.end(), output);
	cout << endl;
	myostream_iteraotr<double> output2(cout, "--");
	Copy(b, b + SIZE, output2);
	getchar();
	return 0;
}

 

7:列表

描述

写一个程序完成以下命令:
new id - 新建一个指定编号为id的序列(id <10000)
add id num-向编号为id的序列加入整数num
merge id1 id2--合并序列id1和id2中的数,并将id2清空
独特的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 4

1 2 3 4

//By Guo Wei,关键是先sort后unique.
#include <list>
#include <string>
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
    list<int> lists[20000];
    int n;
	scanf("%d",&n);
    char cmd[20];
	int id1,id2,num;
	list<int>::iterator li;//don't forget int
    while( n -- ) {
      scanf("%s",cmd);
      switch(cmd[0]) {
		case 'a':
			scanf("%d%d",&id1,&num);
			lists[id1].push_back(num);
			break;
		case 'n':
			scanf("%d",&id1);
			break;
		case 'm':
			scanf("%d%d",&id1,&id2);
			lists[id1].merge (lists[id2]);
			break;
		case 'u':
			scanf("%d",&id1);
			lists[id1].sort();//don't forget bracket
			lists[id1].unique ();
			break;
		case 'o':
			scanf("%d",&id1);
			lists[id1].sort();
			
			for( li = lists[id1].begin(); li != lists[id1].end(); li ++)
				printf("%d ",*li);
			printf("\n");
			break;
      
      }
    }
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值