#include
#include
#include
#include
using namespace std;
template
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 (n,Distance1));
for(int i = 0;i < 10; ++i)
cout << a[i] << "," ;
cout << endl;
sort(b,b+6,Closer (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
#include
#include
#include
using namespace std;
template
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(n, Distance1));
for (int i = 0; i < 10; ++i)
cout << a[i] << ",";
cout << endl;
sort(b, b + 6,
Closer(s, Distance2));
for (int i = 0; i < 6; ++i)
cout << b[i] << ",";
cout << endl;
}
return 0;
}
#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(//补充代码
);
for(list::iterator i = lst.begin(); i != lst.end(); ++i)
cout << * i << "," ;
return 0;
}
输入
无
输出
9.8,7.3,3.4,2.6,1.2,
#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(greater()//list有个greater的排序方式//不过VS2015貌似不认
//don't forget double
);
for (list::iterator i = lst.begin(); i != lst.end(); ++i)
cout << *i << ",";
return 0;
}
#include
#include
#include
using namespace std;
template
void Copy(T1 s,T1 e, T2 x)
{
for(; s != e; ++s,++x)
*x = *s;
}
template
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 lst(a,a+SIZE);
myostream_iteraotr output(cout,",");
Copy( lst.begin(),lst.end(),output);
cout << endl;
myostream_iteraotr 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
#include
#include
using namespace std;
template
void Copy(T1 s, T1 e, T2 x)
{
for (; s != e; ++s, ++x)
*x = *s;
}
template
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< lst(a, a + SIZE);
myostream_iteraotr output(cout, ",");
Copy(lst.begin(), lst.end(), output);
cout << endl;
myostream_iteraotr output2(cout, "--");
Copy(b, b + SIZE, output2);
getchar();
return 0;
}
描述
写一个程序完成以下命令:
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
#include
#include
#include
using namespace std;
int main()
{
list lists[20000];
int n;
scanf("%d",&n);
char cmd[20];
int id1,id2,num;
list::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;
}
}
}