1单选(1分)
1) 关于复制构造函数,下列说法正确的是
A. 系统不会生成缺省复制构造函数,因此必须自己实现
B. 复制构造函数是形如X::X(X)的函数
C. Myclass c1, c2; c1.n = 1; c2 = c1;第三句将会调用复制构造函数
D. 调用函数A Func() { A a(4); return a; }时,将会调用A的复制构造函数
正确答案:D
2)关于虚函数,下列说法不正确的是
-
A. 不允许以虚函数作为构造函数
-
B. 没有虚函数便无法实现多态
-
C. 一般来讲,如果一个类中定义了虚函数,则不可将析构函数也定义为虚函数
-
D. 不能用抽象类定义对象
正确答案:C
3) 关于 this 指针,以下说法不正确的是
-
A. static成员函数内部不可以使用this指针
-
B. 在构造函数内部可以使用this指针
-
C. 在析构函数内部可以使用 this 指针
-
D. const成员函数内部不可以使用this 指针
正确答案:D
4) 以下关于多态的说法那个不正确?
-
A. 在成员函数中调用虚函数,是多态
-
B. 通过“基类对象名.函数名"的方式调用虚函数,不是多态
-
C. 多态的函数调用语句中,函数一定是虚函数
-
D. 通过“基类引用名.函数名"的方式调用虚函数,是多态
正确答案:A
5) map的每个元素包括KEY(first)和VALUE(second)。关于map容器,下列哪种说法错误
-
A. map支持下标运算符
-
B. map的不同元素可以有相同的VALUE
-
C. map支持STL的sort算法
-
D. map支持双向迭代器
正确答案:C
6) 下列说法错误的是
-
A. 可以在一个类的友元函数中使用this指针
-
B 每个类只有一个析构函数
-
C 抽象类至少包含一个纯虚函数
-
D 构造函数不可以是virtual函数
正确答案:A
7) 关于继承和派生的描述中,下列说法错误的是:
-
A. 派生类的成员函数中,不能访问基类的private成员
-
B. 在派生类的析构函数执行之前,会先调用基类的析构函数
-
C. 派生类对象的地址可以赋值给基类指针
-
D. 派生类可以有和基类同名同参数的成员函数`
正确答案:B
8) 以下哪种使用std::sort算法的方式是不合法的:
-
A. vector<int> a; …; sort(a.begin(), a.end());
-
B. bool b[99]; …; sort(b, b + 99);
-
C. string c = “2333”; …; sort(c.begin(), c.end());
-
D. list<int> d; …; sort(d.begin(), d.end());
正确答案:D
9) 类A重载的运算符声明是int operator<(A &other) const,那么以下说法中正确的是:
-
A. 小于号左侧的A对象不可以是const的
-
B. 小于号右侧的A对象不可以是const的
-
C. 这个写法是错误的,因为小于号的返回类型必须是bool
-
D. 使用小于号的时候,other参数处,传进来的对象实际上会被复制一次
正确答案:B
10) 以下STL中的函数模板哪个可以作用于set
-
A. sort
-
B. random_shuffle
-
C. find
-
D. 都不行
正确答案:C
001:编程填空:二进制输出
描述
给出一个int表示范围内的正整数x,输出其二进制表示。一共要输出31位,不足处要补0。
#include <iostream> #include <string> using namespace std; string dec2bin(int x){
} int main(){ int n; cin >> n; while(n--) { int x; cin >> x; cout << dec2bin(x) << endl; } return 0; }
输入
第一行是整数n(n<15),表示有n个正整数要处理
第二行是n个正整数
输出
对每个给出的正整数,输出其二进制表示。不足31位则用0补齐到31位
样例输入
3 1 2 3
样例输出
0000000000000000000000000000001 0000000000000000000000000000010 0000000000000000000000000000011
答案:
#include <iostream>
#include <string>
using namespace std;
string dec2bin(int x){
string bina = "";
while (x != 0) {
bina += to_string(x % 2);
x /= 2;
}
while (bina.length() < 31) {
bina += to_string(0);
}
string b = "";
b.resize(31);
for (int i = 0; i <= 30; i++) {
b[i] = bina[30 - i];
}
return b;
}
int main(){
int n;
cin >> n;
while(n--) {
int x;
cin >> x;
cout << dec2bin(x) << endl;
}
return 0;
}
002:编程填空:统计动物数量
描述
代码填空,使得程序能够自动统计当前各种动物的数量
#include <iostream> using namespace std;
void print() { cout << Animal::number << " animals in the zoo, " << Dog::number << " of them are dogs, " << Cat::number << " of them are cats" << endl; } int main() { print(); Dog d1, d2; Cat c1; print(); Dog* d3 = new Dog(); Animal* c2 = new Cat; Cat* c3 = new Cat; print(); delete c3; delete c2; delete d3; print(); }
输入
无
输出
0 animals in the zoo, 0 of them are dogs, 0 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats
6 animals in the zoo, 3 of them are dogs, 3 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats
样例输入
None
样例输出
0 animals in the zoo, 0 of them are dogs, 0 of them are cats 3 animals in the zoo, 2 of them are dogs, 1 of them are cats 6 animals in the zoo, 3 of them are dogs, 3 of them are cats 3 animals in the zoo, 2 of them are dogs, 1 of them are cats
答案:
#include <iostream>
using namespace std;
class Animal {
public:
static int number;
Animal() {
number++;
}
Animal(Animal &a) {
number++;
}
virtual ~Animal() {
number--;
}
};
class Dog :public Animal {
public:
static int number;
Dog() {
number++;
}
Dog(Dog &a) {
number++;
}
~Dog() {
number--;
}
};
class Cat :public Animal {
public:
static int number;
Cat() {
number++;
}
Cat(Cat &a) {
number++;
}
~Cat() {
number--;
}
};
int Animal::number = 0, Dog::number = 0,Cat::number=0;
void print() {
cout << Animal::number << " animals in the zoo, " << Dog::number << " of them are dogs, " << Cat::number << " of them are cats" << endl;
}
int main() {
print();
Dog d1, d2;
Cat c1;
print();
Dog* d3 = new Dog();
Animal* c2 = new Cat;
Cat* c3 = new Cat;
print();
delete c3;
delete c2;
delete d3;
print();
}
003:编程填空:简单的计算
描述
补充代码,使程序按要求输出
#include <iostream> using namespace std; template <class T> class Add{ public:
}; int main(){ double f; int n; while( cin >> f >> n) { Add<double> a1(f); Add<int> a2(n); double x,y; int p,q; cin >> x >> y >> p >> q; cout << a1(x, y) << endl; cout << a2(p, q) << endl; } return 0; }
输入
有若干组数据
每组数据三行
第一行是一个浮点数f和一个整数 n
第二行是两个浮点数 x 和 y
第三行是两个整数 p 和q
输出
对每组数据
先输出 x + y - f
再输出 p + q - n
样例输入
2.2 3 1.0 2.0 10 20 4.5 30 4.8 9.2 100 200
样例输出
0.8 27 9.5 270
答案:
#include <iostream>
using namespace std;
template <class T>
class Add{
public:
float minus;
Add(float a) :minus(a) {}
float operator ()(T x, T y) {
return (float)(x + y) - minus;
}
};
int main(){
double f;
int n;
while( cin >> f >> n) {
Add<double> a1(f);
Add<int> a2(n);
double x,y;
int p,q;
cin >> x >> y >> p >> q;
cout << a1(x, y) << endl;
cout << a2(p, q) << endl;
}
return 0;
}
004:编程填空:MyClass
补充下列代码,使得程序的输出为:
A:3
A:15
B:5
3
15
5
#include <iostream> using namespace std; class CMyClassA { int val; public: CMyClassA(int); void virtual print(); }; CMyClassA::CMyClassA(int arg) { val = arg; printf("A:%d\n", val); } void CMyClassA::print() { printf("%d\n", val); return; }
int main(int argc, char** argv) { CMyClassA a(3), *ptr; CMyClassB b(5); ptr = &a; ptr->print(); a = b; a.print(); ptr = &b; ptr->print(); return 0; }
输入
无
输出
见样例
样例输入
None
样例输出
A:3 A:15 B:5 3 15 5
答案:
#include <iostream>
using namespace std;
class CMyClassA {
int val;
public:
CMyClassA(int);
void virtual print();
};
CMyClassA::CMyClassA(int arg) {
val = arg;
printf("A:%d\n", val);
}
void CMyClassA::print() {
printf("%d\n", val);
return;
}
class CMyClassB :public CMyClassA {
public:
int val2;
CMyClassB(int x) :CMyClassA(3 * x), val2(x) {
printf("B:%d\n", val2);
}
void print() {
printf("%d\n", val2);
}
};
int main(int argc, char** argv) {
CMyClassA a(3), *ptr;
CMyClassB b(5);
ptr = &a; ptr->print();
a = b;
a.print();
ptr = &b; ptr->print();
return 0;
}
005:编程填空:又是MyClass
描述
补充下列代码,使得程序能够按要求输出
#include <iostream> #include <cstring> #include <vector> #include <cstdio> using namespace std;
int a[40]; int main(int argc, char** argv) { int t; scanf("%d",&t); while ( t -- ) { int m; scanf("%d",&m); for (int i = 0;i < m; ++i) scanf("%d",a+i); char s[100]; scanf("%s",s); CMyClass<int> b(a, m); CMyClass<char> c(s, strlen(s)); printf("%d %c\n", b[5], c[7]); } return 0; }
输入
第一行是整数t表示数据组数
每组数据有两行
第一行开头是整数m,然后后面是m个整数(5 < m < 30)
第二行是一个没有空格的字符串,长度不超过50
输出
对每组数据 先输出m个整数中的第5个,然后输出字符串中的第7个字符。
"第i个"中的 i 是从0开始算的。
样例输入
1 6 1 3 5 5095 8 8 helloworld
样例输出
8 r
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
using namespace std;
template<class T>
class CMyClass {
public:
T *p;
CMyClass(T*s, int wid) {
p = new T[wid];
for (int i = 0; i < wid; i++) {
p[i] = *s;
s++;
}
}
T operator[](int x) {
return *(p + x);
}
};
int a[40];
int main(int argc, char** argv) {
int t;
scanf("%d",&t);
while ( t -- ) {
int m;
scanf("%d",&m);
for (int i = 0;i < m; ++i)
scanf("%d",a+i);
char s[100];
scanf("%s",s);
CMyClass<int> b(a, m);
CMyClass<char> c(s, strlen(s));
printf("%d %c\n", b[5], c[7]);
}
return 0;
}
006:编程填空:去除重复元素排序
描述
程序填空,使其按要求输出
#include <iterator> #include <vector> #include <map> #include <set> #include <queue> #include <algorithm> #include <stack> #include <iostream> #include <set> using namespace std; int main() { int t; int a[100]; cin >> t; while(t--) { for(int i = 0;i < 12; ++i) cin >> a[i];
std::copy(b.begin(), b.end(), c); cout << endl; } return 0; }
#include <iterator>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <stack>
#include <iostream>
#include <set>
using namespace std;
int main() {
int t;
int a[100];
cin >> t;
while(t--) {
for(int i = 0;i < 12; ++i)
cin >> a[i];
sort(a,a+12);
vector<int> b;
b.push_back(a[0]);
int tmp=0;
for(int i=1;i<12;i++){
if(b[tmp] == a[i])
continue;
else{
b.push_back(a[i]);
tmp++;
}
}
ostream_iterator<int> c(cout," ");
std::copy(b.begin(), b.end(), c);
cout << endl;
}
return 0;
}
007:编程填空:按要求输出
描述
下列程序的输出为"10 13 18 15 17 12 16 19", 请补充代码
#include <iterator> #include <vector> #include <map> #include <set> #include <queue> #include <algorithm> #include <stack> #include <iostream> #include <set> using namespace std; int a[10] = {0, 6, 7, 3, 9, 5, 8, 6, 4, 9}; int b[10] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; int main(int argc, char** argv) {
for(int i=0; i<10; i++) c[a[i]] = b[i]; for(it=c.begin(); it!=c.end(); it++) cout<<it->second<<" "; return 0; }
输入
无
输出
10 13 18 15 17 12 16 19
样例输入
None
样例输出
10 13 18 15 17 12 16 19
#include <iterator>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <stack>
#include <iostream>
#include <set>
using namespace std;
int a[10] = {0, 6, 7, 3, 9, 5, 8, 6, 4, 9};
int b[10] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
int main(int argc, char** argv) {
map<int, int>c;
for(int i=0; i<10; i++)
c[a[i]] = b[i];
map<int, int>::iterator it;
for(int i=0; i<10; i++)
c[a[i]] = b[i];
for(it=c.begin(); it!=c.end(); it++)
cout<<it->second<<" ";
return 0;
}
008:编程填空:还是Fun和Do
描述
填写代码,使输出结果为
A::Fun
B::Do
C::Fun
C::Do
A::Fun
B::Do
#include <iostream> using namespace std; class A { public: virtual void Fun() { cout << "A::Fun" << endl; }; virtual void Do() { cout << "A::Do" << endl; } };
{ p.Fun(); p.Do(); } void Call2(B p) { p.Fun(); p.Do(); } int main() { C c; B b; Call1(b); Call1(c); Call2(c); return 0; }
输入
无
输出
A::Fun
B::Do
C::Fun
C::Do
A::Fun
B::Do
样例输入
None
样例输出
A::Fun B::Do C::Fun C::Do A::Fun B::Do
#include <iostream>
using namespace std;
class A {
public:
virtual void Fun() {
cout << "A::Fun" << endl;
};
virtual void Do() {
cout << "A::Do" << endl;
}
};
class B:public A {
public:
void Do() {
cout << "B::Do" << endl;
}
};
class C:public B {
public:
void Do() {
cout << "C::Do" << endl;
}
void Fun() {
cout << "C::Fun" << endl;
};
};
void Call1(A &p)
{
p.Fun();
p.Do();
}
void Call2(B p) {
p.Fun();
p.Do();
}
int main() {
C c;
B b;
Call1(b);
Call1(c);
Call2(c);
return 0;
}
009:编程填空:简单的对象
描述
程序填空,使得程序输出:
2
1
1
0
#include <iostream> using namespace std; class A { static int num; public: A(){num+=1;} void func() { cout<< num <<endl; }
}; int A::num=1; int main() { A a1; const A a2 = a1; A & a3 = a1; const A & a4 = a1; a1.func(); a2.func(); a3.func(); a4.func(); return 0; }
输入
无
输出
2
1
1
0
样例输入
None
样例输出
2 1 1 0
#include <iostream>
using namespace std;
class A
{
static int num;
public:
A(){num+=1;}
void func()
{
cout<< num <<endl;
}
void func()const {
num--;
cout << num << endl;
}
};
int A::num=1;
int main()
{
A a1;
const A a2 = a1;
A & a3 = a1;
const A & a4 = a1;
a1.func();
a2.func();
a3.func();
a4.func();
return 0;
}
010:编程填空:回调函数
描述
输入x1 x2 x3 x4 x5 ,输出y = x5^5 + x4^4 + x3^3 + x2^2 + x1^1 + 1的y的值
#include <algorithm> #include <iostream> #include <stack> #include <queue> #include <vector> #include <cstring> #include <cstdlib> #include <string> #include <cmath> #include <map> #include <set> using namespace std; class MyFunc {
}; int main() { int n; cin >> n; while(n--) { vector<MyFunc> v; for (int i = 0; i < 5; ++i) v.push_back(MyFunc(i+1)); int ans = 1; for (int i = 0; i < 5; ++i) { int m; cin >> m; ans += v[i](m); } cout << ans <<endl; } }
输入
多组数据。第一行是数据组数 n
每组数据为一行,5个整数,x1 x2 x3 x4 x5。数值不大,不必考虑溢出
输出
对每组数据,输出一个整数y, y = x5^5 + x4^4 + x3^3 + x2^2 + x1^1 + 1
样例输入
2 2 2 2 2 2 1 1 1 1 1
样例输出
63 6
#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <map>
#include <set>
using namespace std;
class MyFunc
{
public:
int mult;
MyFunc(int i):mult(i){}
int operator()(int m) {
return pow(m, mult);
}
};
int main()
{
int n;
cin >> n;
while(n--) {
vector<MyFunc> v;
for (int i = 0; i < 5; ++i)
v.push_back(MyFunc(i+1));
int ans = 1;
for (int i = 0; i < 5; ++i)
{
int m;
cin >> m;
ans += v[i](m);
}
cout << ans <<endl;
}
}
011:编程填空:前K大的偶数
描述
输入n个整数,输出整数数列中大小排名前k的偶数
#include <algorithm> #include <iostream> #include <stack> #include <queue> #include <vector> #include <cstring> #include <cstdlib> #include <string> #include <map> #include <set> using namespace std; class MyQueue {
}; int main() { int t; cin >> t; while(t--) { int n, k; cin >> n >> k; MyQueue q(k); for (int i = 0; i < n; ++i) cin >> q; cout<<q; cout << endl; } return 0; }
输入
有多组数据
第一行是数据组数 t
对每组数据:
第一行为整数n (n>=3)和k
接下来的一行为n个整数,保证这些整数中至少有k个偶数。
输出
对每组数据,输出k个整数,降序排列,表示选出来的大小排名前k的偶数
样例输入
2 9 4 1 2 4 3 6 6 7 8 9 3 2 18 16 14
样例输出
8 6 6 4 18 16
#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <string>
#include <map>
#include <set>
using namespace std;
class MyQueue
{
public:
int k;
multiset<int, greater<int>> que;
MyQueue(int _k):k(_k){}
friend istream & operator>>(istream&is, MyQueue &a) {
int l;
is >> l;
if (l % 2 == 0)a.que.insert(l);
return is;
}
friend ostream & operator <<(ostream&os, MyQueue &a) {
multiset<int>::iterator p=a.que.begin();
int count = 0;
for (;count<=a.k-1; p++) {
if (count)os << " ";
os << *p ;
count++;
}
return os;
}
};
int main()
{
int t;
cin >> t;
while(t--) {
int n, k;
cin >> n >> k;
MyQueue q(k);
for (int i = 0; i < n; ++i)
cin >> q;
cout<<q;
cout << endl;
}
return 0;
}
012:编程填空:Printer
描述
完成以下程序,使得输入的整数x,以及若干正整数,将
大于x的正整数输出;然后输入若干字符串,将字符串长度大于x的字符串输出
#include<iostream> #include<algorithm> #include<vector> #include<bitset> using namespace std; class Printer{
int main(){ int t; cin >> t; while(t--) { int n,x; cin>>x>>n; vector<int> intVec; for(int i = 0;i < n; ++i) { int y; cin >> y; intVec.push_back(y); } for_each(intVec.begin(), intVec.end(), Printer(x)); cout<<endl; vector<string> strVec; for(int i = 0;i < n; ++i) { string str; cin >> str; strVec.push_back(str); } for_each(strVec.begin(), strVec.end(), Printer(x)); cout<<endl; } return 0; }
输入
第一行是整数t,表示一共t组数据
每组数据有三行
第一行是整数x和整数 n
第二行是n个整数
第三行是n个不带空格的字符串
输出
对每组数据
先按原序输出第一行中大于x的正整数(数据保证会有输出)
再按原序输出第二行中长度大于x的字符串 (数据保证会有输出)
样例输入
2 5 6 1 3 59 30 2 40 this is hello please me ha 1 1 4 this
样例输出
59,30,40, please, 4, this,
#include<iostream>
#include<algorithm>
#include<vector>
#include<bitset>
using namespace std;
class Printer{
public:
int x;
Printer(int _x):x(_x){}
void operator()(int a) {
if (a > x)
cout << a << ",";
}
void operator()(string a) {
if (a.length() > x)
cout << a << ",";
}
};
int main(){
int t;
cin >> t;
while(t--) {
int n,x;
cin>>x>>n;
vector<int> intVec;
for(int i = 0;i < n; ++i) {
int y;
cin >> y;
intVec.push_back(y);
}
for_each(intVec.begin(), intVec.end(), Printer(x));
cout<<endl;
vector<string> strVec;
for(int i = 0;i < n; ++i) {
string str;
cin >> str;
strVec.push_back(str);
}
for_each(strVec.begin(), strVec.end(), Printer(x));
cout<<endl;
}
return 0;
}
013:编程填空:三生三世
描述
近年来,国内电视剧吸引了越来越多的关注;有的以当红的演员阵容而吸引观众,比如《三生三世十里桃花》(Life After Life,Blooms Over Blooms);有的以贴近时代的剧情而备受关注,比如《人民的名义》(In the Name of People);有的则以精湛的演技赢得观众的喜欢,比如《大明王朝:1566》(Ming Dynasty: 1566)。
你的任务是根据电视剧的不同属性(演员、剧情和演技)对电视剧进行排行。
#include<iostream> #include<cstring> #include<list> #include<algorithm> using namespace std; class TV_Drama{ public: char name[100]; int actor; int story; int acting_skill;
int main(){ list<TV_Drama> lst; int n; cin>>n; char _name[100]; int _actor, _story, _acting_skill; for (int i=0; i<n; i++){ cin.ignore(); cin.getline(_name,100); cin>>_actor>>_story>>_acting_skill; lst.push_back(TV_Drama(_name, _actor, _story, _acting_skill)); } lst.sort(); for_each(lst.begin(), lst.end(), Printer); cout<<endl; lst.sort(comparator_1); for_each(lst.begin(), lst.end(), Printer); cout<<endl; lst.sort(comparator_2()); for_each(lst.begin(), lst.end(), Printer); cout<<endl; return 0; }
输入
首先输入整数n,代表电视剧的个数。接下来,对于每个电视剧有两行输入:第一行一个字符串(可能含有空格,逗号,冒号等标点符号)作为电视剧的名字;第二行包括三个整数,分别为演员阵容、剧情和演技的评分。
输出
输出包括三行,分别为电视剧按演员阵容、剧情和演技的排行榜(评分由高到低),电视剧名字之间以分号隔开
样例输入
3 In the Name of People 98 97 99 Life After Life, Blooms Over Blooms 99 82 73 Ming Dynasty: 1566 97 100 100
样例输出
Life After Life, Blooms Over Blooms;In the Name of People;Ming Dynasty: 1566; Ming Dynasty: 1566;In the Name of People;Life After Life, Blooms Over Blooms; Ming Dynasty: 1566;In the Name of People;Life After Life, Blooms Over Blooms;
#include<iostream>
#include<cstring>
#include<list>
#include<algorithm>
using namespace std;
class TV_Drama{
public:
char name[100];
int actor;
int story;
int acting_skill;
TV_Drama(char *_name, int _actor, int _story, int _ac) :actor(_actor), story(_story), acting_skill(_ac) {
int len = 0;
for (int i = 0; _name[i] != '\0'; i++) {
name[i] = _name[i];
len++;
}
name[len] = '\0';
}
bool operator<(TV_Drama&l) {
return actor > l.actor;
}
};
void Printer(TV_Drama x) {
cout << x.name << ";";
}
bool comparator_1(TV_Drama &x1,TV_Drama &x2) {
return x1.story > x2.story;
}
class comparator_2{
public:
comparator_2() {}
bool operator() (TV_Drama &x1, TV_Drama &x2) {
return x1.acting_skill > x2.acting_skill;
}
};
int main(){
list<TV_Drama> lst;
int n;
cin>>n;
char _name[100];
int _actor, _story, _acting_skill;
for (int i=0; i<n; i++){
cin.ignore();
cin.getline(_name,100);
cin>>_actor>>_story>>_acting_skill;
lst.push_back(TV_Drama(_name, _actor, _story, _acting_skill));
}
lst.sort();
for_each(lst.begin(), lst.end(), Printer);
cout<<endl;
lst.sort(comparator_1);
for_each(lst.begin(), lst.end(), Printer);
cout<<endl;
lst.sort(comparator_2());
for_each(lst.begin(), lst.end(), Printer);
cout<<endl;
return 0;
}
014:编程填空:矩形排序
描述
给定一系列边长已知的矩形,输出对矩形进行两种排序的结果。
在第一种排序中,先按矩形的面积从大到小排序;若两个矩形的面积相同,则周长大的排在前。
在第二种排序中,先按矩形的周长从小到大排序;若两个矩形的周长相同,则面积小的排在前。
<span style="color:#231f17">#include <iostream>
#include <set>
using namespace std;</span>
<span style="color:#231f17">int main() {
multiset<Rectangle> m1;
multiset<Rectangle, Comp> m2;
int n, a, b;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a >> b;
m1.insert(Rectangle(a, b));
m2.insert(Rectangle(a, b));
}
for (multiset<Rectangle>::iterator it = m1.begin(); it != m1.end(); it++) {
cout << *it << endl;
}
cout << endl;
for (multiset<Rectangle>::iterator it = m2.begin(); it != m2.end(); it++) {
cout << *it << endl;
}
return 0;
}</span>
输入
第一行是一个整数n,表示输入的矩形个数。
接下来n行表示了n个矩形。每行有两个整数a与b,表示该矩形的长与宽。
输出
先用n行输出第一种排序的结果。每行两个整数,依次表示该矩形的面积与周长。
再输出一个空行。
最后用n行输出第二种排序的结果。每行两个整数,依次表示该矩形的面积与周长。
样例输入
6
3 8
4 6
10 2
6 6
4 8
3 6
样例输出
36 24
32 24
24 22
24 20
20 24
18 18
18 18
24 20
24 22
20 24
32 24
36 24
#include <iostream>
#include <set>
using namespace std;
class Rectangle
{
public:
int a,b,area,premiere;
Rectangle(int c,int d)
{
a = c;
b = d;
area = a*b;
premiere = (a+b)*2;
}
friend bool operator < (const Rectangle& a1, const Rectangle& a2)
{
if(a1.area>a2.area)
return true;
else if(a1.area == a2.area)
{
if(a1.premiere>a2.premiere)
return true;
else
return false;
}
else
return false;
}
friend class Comp;
friend ostream & operator<<(ostream &o, const Rectangle& a1)
{
o << a1.area << " "<<a1.premiere;
return o;
}
};
struct Comp{
bool operator()(const Rectangle& a1, const Rectangle& a2)
{
if(a1.premiere<a2.premiere)
return true;
else if(a1.premiere == a2.premiere)
{
if(a1.area < a2.area)
return true;
else
return false;
}
else
return false;
}
};
int main() {
multiset<Rectangle> m1;
multiset<Rectangle, Comp> m2;
int n, a, b;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a >> b;
m1.insert(Rectangle(a, b));
m2.insert(Rectangle(a, b));
}
for (multiset<Rectangle>::iterator it = m1.begin(); it != m1.end(); it++) {
cout << *it << endl;
}
cout << endl;
for (multiset<Rectangle>::iterator it = m2.begin(); it != m2.end(); it++) {
cout << *it << endl;
}
return 0;
}