小型手機(jī)網(wǎng)站建設(shè)哪家好百度視頻
A. Point&Circle(復(fù)合類與構(gòu)造)
題目描述
類Point是我們寫過的一個(gè)類,類Circle是一個(gè)新的類,Point作為其成員對象,請完成類Circle的成員函數(shù)的實(shí)現(xiàn)。
在主函數(shù)中生成一個(gè)圓和若干個(gè)點(diǎn),判斷這些點(diǎn)與圓的位置關(guān)系,如果點(diǎn)在圓內(nèi)(包括在圓的邊上),輸出“inside”,否則輸出"outside";然后移動圓心的位置,再次判斷這些點(diǎn)與圓的位置關(guān)系。
輸入
圓的x坐標(biāo) y坐標(biāo) 半徑
點(diǎn)的個(gè)數(shù)n
第一個(gè)點(diǎn)的x坐標(biāo) y坐標(biāo)
第二個(gè)點(diǎn)的x坐標(biāo) y坐標(biāo)
…
第n個(gè)點(diǎn)的x坐標(biāo) y坐標(biāo)
圓心移動到的新的x坐標(biāo) y坐標(biāo)
輸出
第一個(gè)點(diǎn)與圓的關(guān)系
第二個(gè)點(diǎn)與圓的關(guān)系
…
第n個(gè)點(diǎn)與圓的關(guān)系
after move the centre of circle
圓心移動后第一個(gè)點(diǎn)與圓的關(guān)系
圓心移動后第二個(gè)點(diǎn)與圓的關(guān)系
…
圓心移動后第n個(gè)點(diǎn)與圓的關(guān)系
輸入樣例1
0 0 5
4
1 1
2 2
5 0
-6 0
-1 0
輸出樣例1
inside
inside
inside
outside
after move the centre of circle:
inside
inside
outside
inside
AC代碼
#include<bits/stdc++.h>
using namespace std;class Point {double x, y;
public:Point() {}Point(double x,double y):x(x),y(y){}double getX() { return x; }double getY() { return y; }double getDisTo(Point& p) {return sqrt(pow(x - p.x, 2) + pow(y - p.y,2));}void setXY(int x,int y){this->x = x;this->y = y;}~Point(){}
};class Circle {Point centre;double radius;
public:Circle() {}Circle(double x, double y, double r) {centre.setXY(x, y);radius = r;}double getArea() {return 2.1415926 * radius * radius;}void moveCentreTo(double x1, double y1) {centre.setXY(x1, y1);}bool isContain(Point &p) {double distance = centre.getDisTo(p);return distance <= radius;}};int main() {int x, y, r;cin >> x >> y >> r;Circle c(x, y, r);int n;cin >> n;vector<Point>v(n);for (auto& it : v) {cin >> x >> y;it.setXY(x, y);if (c.isContain(it))cout << "inside" << endl;elsecout << "outside" << endl;}cin >> x >> y;c.moveCentreTo(x, y);cout << "after move the centre of circle:" << endl;for (auto& it : v) {if (c.isContain(it))cout << "inside" << endl;elsecout << "outside" << endl;}return 0;
}
B. Complex(類與對象+構(gòu)造)
題目描述
編寫一個(gè)復(fù)數(shù)類,能實(shí)現(xiàn)加、減運(yùn)算,能輸出復(fù)數(shù)的信息。 要求至少包含以下方法:
1、缺省(無參)構(gòu)造函數(shù),設(shè)置實(shí)部與虛部為1;
2、有參構(gòu)造函數(shù),給實(shí)部與虛部賦值;
3、加法運(yùn)算,計(jì)算兩個(gè)復(fù)數(shù)的和;
4、減法運(yùn)算,計(jì)算兩個(gè)復(fù)數(shù)的差;
5、輸出方法,輸出當(dāng)前復(fù)數(shù)的值
輸入
測試數(shù)據(jù)的組數(shù)t 第一組的兩個(gè)復(fù)數(shù)的實(shí)部 虛部 實(shí)部 虛部 第二組的兩個(gè)復(fù)數(shù)的實(shí)部 虛部 實(shí)部 虛部 …
輸出
第一組兩個(gè)復(fù)數(shù)的和 第一組兩個(gè)復(fù)數(shù)的差
輸入樣例1
4
2 1 2 1
2 1 2 -1
3 1 2 -6
3 3 2 2
輸出樣例1
sum:4+2i
remainder:0
sum:4
remainder:2i
sum:5-5i
remainder:1+7i
sum:5+5i
remainder:1+i
AC代碼
#include<bits/stdc++.h>
using namespace std;class Complex
{
public:Complex();Complex(int, int);~Complex();Complex add(Complex&);Complex sub(Complex&);void display();private:int a, b;
};Complex::Complex()
{a = b = 0;
}Complex::Complex(int a, int b) :a(a), b(b)
{
}Complex::~Complex()
{
}Complex Complex::add(Complex& c1)
{return Complex(a + c1.a, b + c1.b);
}Complex Complex::sub(Complex& c1)
{return Complex(a - c1.a, b - c1.b);
}void Complex::display()
{if (!(a || b)) {cout << 0 << endl;return;}if (a)cout << a;if (b > 0) {if(a)cout << "+";if (b != 1)cout << b;cout << "i" ;}else if (b < 0) {if (b != -1)cout << b << "i" ;elsecout << "-i";}cout << endl;
}int main() {int t;cin >> t;while (t--){int a, b, c, d;cin >> a >> b >> c >> d;Complex c1(a, b), c2(c, d);cout << "sum:";c1.add(c2).display();cout << "remainder:";c1.sub(c2).display();}return 0;
}
C. 電話號碼升位(拷貝構(gòu)造函數(shù))
題目描述
定義一個(gè)電話號碼類CTelNumber,包含1個(gè)字符指針數(shù)據(jù)成員,以及構(gòu)造、析構(gòu)、打印及拷貝構(gòu)造函數(shù)。
字符指針是用于動態(tài)創(chuàng)建一個(gè)字符數(shù)組,然后保存外來輸入的電話號碼
構(gòu)造函數(shù)的功能是為對象設(shè)置鍵盤輸入的7位電話號碼,
拷貝構(gòu)造函數(shù)的功能是用原來7位號碼的對象升位為8位號碼對象,也就是說拷貝構(gòu)造的對象是源對象的升級.電話升位的規(guī)則是原2、3、4開頭的電話號碼前面加8,原5、6、7、8開頭的前面加2。
注意:合法的電話號碼:1、長度為7位;2、電話號碼的字符全部是數(shù)字字符;3、第一個(gè)字符只能是以下字符:2、3、4、5、6、7、8。與上述情況不符的輸入均為非法
輸入
測試數(shù)據(jù)的組數(shù) t
第一個(gè)7位號碼
第二個(gè)7位號碼
…
輸出
第一個(gè)號碼升位后的號碼
第二個(gè)號碼升位后的號碼
…
如果號碼升級不成功,則輸出報(bào)錯(cuò)信息,具體看示例
輸入樣例1
3
6545889
3335656
565655
輸出樣例1
26545889
83335656
Illegal phone number
AC代碼
#include<bits/stdc++.h>
using namespace std;class CTelNumber {string id;
public:CTelNumber() {cin >> id;}~CTelNumber(){id.clear();}void print() {cout << id << endl;}bool isLeagle() {if (id.length() != 7)return false;for (auto& it : id)if (!isdigit(it))return false;if (id[0] == '0' || id[0] == '1' || id[0] == '9')return false;return true;}CTelNumber(const CTelNumber& p) {id = p.id;if (id[0] >= '2' && id[0] <= '4')id = "8" + id;else if (id[0] >= '5' && id[0] <= '8')id = "2" + id;}};int main() {int t;cin >> t;while (t--){CTelNumber c;if (c.isLeagle()) {CTelNumber c1(c);c1.print();}else cout << "Illegal phone number" << endl;}return 0;
}
D. 身份證設(shè)定(復(fù)合類+拷貝構(gòu)造)
題目描述
定義一個(gè)身份證類PID,包含私有屬性:身份證類型、身份證號碼、出生日期;另外包含方法:構(gòu)造、拷貝構(gòu)造打印等。
身份證類型表示一代身份證或者二代身份證,分別用1和2表示
身份證號碼是一個(gè)字符串,長度為15或者18
出生日期是一個(gè)類,包含私有屬性年、月、日,以及構(gòu)造函數(shù)等(根據(jù)需要添加其他方法)
構(gòu)造函數(shù)要注意是復(fù)合類,要考慮復(fù)合類成員的構(gòu)造
打印函數(shù)把身份證的所有屬性都輸出,輸出格式看示例
拷貝構(gòu)造作用:如果身份證號碼是15位的就升級為18位,包括把身份證類型改為2,然后把號碼擴(kuò)展,規(guī)則如下:
-
原15位身份證的第7位到12位表示出生年月日,每個(gè)兩位;把年份的2位擴(kuò)展為四位。
-
把擴(kuò)展后的17個(gè)數(shù)字求和,取總和的末尾數(shù)字,如果末尾數(shù)字是0,則將0改為X,然后把這個(gè)數(shù)字作為第18位
3.如果身份證號碼已經(jīng)是18位,就無需升級
例如身份證123456910203000,表示91年2月3日出生,然后根據(jù)類屬性出生日期知道是1991年,不是2091年。因此擴(kuò)展為12345619910203000
接著把17個(gè)數(shù)字相加得到46,取末尾6,最終擴(kuò)展為123456199102030006
輸入
第一行輸入t表示t個(gè)示例
第二行輸入一個(gè)身份證的5個(gè)屬性,順序?yàn)?#xff1a;類型、號碼、出生年、月、日
依次輸入t行
輸出
采用拷貝構(gòu)造函數(shù)的方法對身份證號碼升級,然后輸出
輸入樣例1
3
1 123456910203000 1991 2 3
2 654321200001018889 2000 1 1
1 234567001217000 2000 12 17
輸出樣例1
type=2 birth=1991.02.03
ID=123456199102030006
type=2 birth=2000.01.01
ID=654321200001018889
type=2 birth=2000.12.17
ID=23456720001217000X
AC代碼
#include<bits/stdc++.h>
using namespace std;class Date {int y, m, d;
public:Date() { }Date(int y, int m, int d) :y(y), m(m), d(d) {}int getYear() { return y; }int getMonth() { return m; }int getDay() { return d; }void init() { cin >> y >> m >> d; }
};class Pid {int type;string id;Date birthday;
public:Pid() {cin >> type >> id;birthday.init();}Pid(const Pid& pid) {type = pid.type;id = pid.id;birthday = pid.birthday;if (type == 1) {id.erase(6, 2);id.insert(6, to_string(birthday.getYear()));int sum = 0;for (auto& it : id) {sum += it - '0';}sum %= 10;if (sum == 0)id.push_back('X');elseid.push_back(sum + '0');type = 2;}}void display() {cout << "type=" << type << " ";cout << "birth=" << birthday.getYear() << "." << setfill('0') << setw(2) << birthday.getMonth() << "." << setfill('0') << setw(2) << birthday.getDay() << endl;cout << "ID=" << id << endl;}};int main() {int t;cin >> t;while (t--) {Pid id;Pid update_id(id);update_id.display();}return 0;
}
E. 軟件備份(拷貝構(gòu)造函數(shù))
題目描述
軟件作為一種對象也可以用類來描述,軟件的屬性包括軟件名稱、類型(分別用O、T和B表示原版、試用版還是備份)、有效截至日期(用CDate類子對象表示)和存儲介質(zhì)(分別用D、H和U表示光盤、磁盤和U盤)等。軟件拷貝可通過拷貝構(gòu)造函數(shù)來實(shí)現(xiàn),此時(shí)在拷貝構(gòu)造函數(shù)中軟件類型改成“B”, 存儲介質(zhì)改為"H",其它不變。試完成該類的拷貝構(gòu)造、構(gòu)造和打印(包括從2015年4月7日算起有效期還有多少天,是否過期)成員函數(shù)的實(shí)現(xiàn)。
當(dāng)輸入軟件有效截止日期是0年0月0日,表示無日期限制,為unlimited;當(dāng)輸入日期在2015年4月7日之前,則是過期,表示為expired;如果輸入日期在2015年4月7日之后,則顯示之后的剩余天數(shù)。具體輸出信息看輸出范例。
附CDate類的實(shí)現(xiàn):
class CDate
{
private:
int year, month, day;
public:
CDate(int y, int m, int d) { year = y; month = m; day = d; }
bool isLeapYear() { return (year%4 == 0 && year%100 != 0) || year%400 == 0; }
int getYear() { return year; }
int getMonth() { return month; }
int getDay() { return day; }
int getDayofYear() //計(jì)算日期從當(dāng)年1月1日算起的天數(shù)
{
int i, sum=day;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int b[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
if (isLeapYear())
for(i=0;i<month;i++) sum +=b[i];
else
for(i=0;i<month;i++) sum +=a[i];
return sum;
}
};
輸入
測試數(shù)據(jù)的組數(shù) t
第一個(gè)軟件名稱
第一個(gè)軟件類型 第一個(gè)軟件介質(zhì)類型 第一個(gè)軟件有效期年 月 日
第二個(gè)軟件名稱
第二個(gè)軟件類型 第二個(gè)軟件介質(zhì)類型 第二個(gè)軟件有效期年 月 日
…
輸出
name: 第一個(gè)軟件名稱
type: 第一個(gè)軟件類型
media: 第一個(gè)軟件介質(zhì)類型
第一個(gè)軟件2015-4-7后的有效天數(shù)
name: 第一個(gè)軟件名稱
type: backup
media: hard disk
第一個(gè)軟件2015-4-7后的有效天數(shù)
…
輸入樣例1
3
Photoshop_CS5
O D 0 0 0
Audition_3.0
B U 2015 2 3
Visual_Studio_2010
T H 2015 5 5
輸出樣例1
name:Photoshop_CS5
type:original
media:optical disk
this software has unlimited use
name:Photoshop_CS5
type:backup
media:hard disk
this software has unlimited use
name:Audition_3.0
type:backup
media:USB disk
this software has expired
name:Audition_3.0
type:backup
media:hard disk
this software has expired
name:Visual_Studio_2010
type:trial
media:hard disk
this software is going to be expired in 28 days
name:Visual_Studio_2010
type:backup
media:hard disk
this software is going to be expired in 28 days
AC代碼
#include<bits/stdc++.h>
using namespace std;class CDate
{int year, month, day;
public:CDate() {};CDate(int y, int m, int d) { year = y; month = m; day = d; }CDate(const CDate& c) {year = c.year;month = c.month;day = c.day;}bool isLeapYear() { return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; }int getYear() { return year; }int getMonth() { return month; }int getDay() { return day; }int getDayofYear() //計(jì)算日期從當(dāng)年1月1日算起的天數(shù){int i, sum = day;int a[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };int b[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };if (isLeapYear())for (i = 0; i < month; i++) sum += b[i];elsefor (i = 0; i < month; i++) sum += a[i];return sum;}void goTomorrow() {int a[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if (isLeapYear()) {a[2] += 1;}day++;if (day > a[month]) {day = 1;month++;}if (month > 12) {month = 1;year++;}}bool operator!=(CDate& date) {if (year != date.year)return true;if (month != date.month)return true;if (day != date.day)return true;return false;}};class Software {string name;char type, media;CDate ddl;
public:Software() {}Software(string name, char type, char hard, CDate ddl) {this->name = name;this->type = type;this->media = hard;this->ddl = ddl;}Software(const Software& s) {this->name = s.name;this->type = 'B';this->media = 'H';this->ddl = s.ddl;}string getName() {return name;}bool isOverdue() {if (ddl.getYear() < 2015)return true;if (ddl.getYear() > 2015)return false;if (ddl.getMonth() < 4)return true;if (ddl.getMonth() > 4)return false;if (ddl.getDay() < 7)return true;if (ddl.getDay() >= 7)return false;}int getRestDay() {CDate today(2015, 4, 7);int cnt = 0;while (today != ddl) {cnt++;today.goTomorrow();}return cnt;}void print() {cout << "name:" << name << endl;cout << "type:";if (type == 'B')cout << "backup" << endl;else if (type == 'T')cout << "trial" << endl;elsecout << "original" << endl;cout << "media:";if (media == 'H')cout << "hard disk" << endl;else if (media == 'D')cout << "optical disk" << endl;else cout << "USB disk" << endl;if (!(ddl.getYear() || ddl.getMonth() || ddl.getDay()))cout << "this software has unlimited use" << endl;else if (isOverdue())cout << "this software has expired" << endl;elsecout << "this software is going to be expired in " << getRestDay() << " days" << endl;cout << endl;}};int main() {int t;cin >> t;while (t--) {string name;char type, media;int year, month, day;cin >> name >> type >> media >> year >> month >> day;Software s(name, type, media, CDate(year, month, day));s.print();Software copy_s(s);copy_s.print();}return 0;
}
F. 購物車(復(fù)合類)
題目描述
定義一個(gè)商品類,包含私有數(shù)據(jù)成員:商品編號、名稱、顏色、尺碼、單價(jià)、數(shù)量。成員函數(shù)有:計(jì)算總價(jià)(單價(jià)*數(shù)量)、輸出商品信息。具體輸出格式見樣例輸出。構(gòu)造函數(shù)及其它函數(shù)可根據(jù)題目需要自行添加。
定義一個(gè)購物車類,包含私有數(shù)據(jù)成員:商品對象集合、商品總數(shù)、購物車所有商品總價(jià)。方法有:添加商品、刪除商品、減少商品數(shù)量、增加商品數(shù)量,輸出購物車中的商品清單。構(gòu)造函數(shù)及其它函數(shù)可根據(jù)題目需要自行添加。
編寫主函數(shù),定義上述類對象,根據(jù)樣例的輸入、輸出實(shí)現(xiàn)購物車的簡單模擬。
購物車操作分別用ADD、DELETE、UP、DOWN表示,具體格式描述如下:
ADD 商品編號 商品名稱 顏色 尺碼 單價(jià) 數(shù)量 //添加1個(gè)或多個(gè)同類商品,若購物車已有指定編號商品,只需增加數(shù)量;若無,添加在購物車首部。
DELETE 商品編號 //刪除購物車中給定商品編號的所有商品,不存在刪除不成功的情況,即購物車中一定有給定編號的物品。
UP 商品編號 //購物車中商品編號的商品數(shù)量加1,不存在操作不成功的情況。
DOWN 商品編號 //購物車中商品編號的商品數(shù)量減1,且最小為1。
為更好理解題目,可在京東購物車?yán)镌嚥僮?。樣例中的?shù)據(jù)來源于該網(wǎng)站,包括顏色和尺碼。為簡化題目,假設(shè)同一商品不同顏色、尺碼,不同編號。
輸入
測試次數(shù)t
每組測試數(shù)據(jù)為:
購物車操作次數(shù)n,后跟n行操作。
輸出
對每組測試數(shù)據(jù),輸出操作結(jié)束后的購物車商品清單,輸出格式見樣例,商品統(tǒng)計(jì)前輸出10個(gè)-符號。所有測試數(shù)據(jù)操作結(jié)束后的購物車均非空。
輸入樣例1
1
8
ADD 2018040801 格力變頻冷暖空調(diào)KFR-26GW 大1匹 變頻掛機(jī) 2999 1
ADD 2018040802 長虹65D2P高清HDR平板LED液晶 1 1 4799 1
ADD 2018040803 康佳LED55X9人工智能平板電視機(jī) null 55寸 4999 1
UP 2018040802
UP 2018040803
DOWN 2018040803
DELETE 2018040802
ADD 2018040802 長虹65D2P高清HDR平板LED液晶 1 1 4799 2
輸出樣例1
商品清單:
商品,顏色,尺碼,單價(jià),數(shù)量,小計(jì)
長虹65D2P高清HDR平板LED液晶,1,1,4799.00,2,9598.00
康佳LED55X9人工智能平板電視機(jī),null,55寸,4999.00,1,4999.00
格力變頻冷暖空調(diào)KFR-26GW,大1匹,變頻掛機(jī),2999.00,1,2999.00
----------
4件商品,總商品金額17596.00
AC代碼
#include<bits/stdc++.h>
using namespace std;class Good {string id, name, color, size;double price;int num;
public:Good() {cin >> id >> name >> color >> size >> price >> num;}double getSumPrice() {return num * price;}string getId() {return id;}int getNum() {return num;}void display() {cout << name << "," << color << "," << size << "," << fixed << setprecision(2) << price << "," << num << "," << fixed << setprecision(2) << getSumPrice() << endl;}void add(int n = 1) {num += n;}void down() {num--;if (num < 1)num = 1;}};class Cat {list<Good>set;
public:Cat() {}void ADD() {Good new_good;for (auto& good : set) {if (good.getId() == new_good.getId()) {good.add(new_good.getNum());return;}}set.push_front(new_good);}void UP() {string new_id;cin >> new_id;for (auto& good : set) {if (good.getId() == new_id) {good.add();return;}}}void DOWN() {string new_id;cin >> new_id;for (auto& good : set) {if (good.getId() == new_id) {good.down();return;}}}void DELETE() {string s;cin >> s;for (auto it = set.begin(); it != set.end(); it++){if (it->getId() == s) {set.erase(it);return;}}}int getGoodNum() {int cnt = 0;for (auto& it : set)cnt += it.getNum();return cnt;}double getSumPrice() {double sum = 0;for (auto& it : set) {sum += it.getSumPrice();}return sum;}void print() {cout << "商品清單:" << endl;cout << "商品,顏色,尺碼,單價(jià),數(shù)量,小計(jì)" << endl;for (auto& it : set) {it.display();}cout << "----------" << endl;cout << getGoodNum() << "件商品,總商品金額" << fixed << setprecision(2) << getSumPrice() << endl;}};int main() {int t;cin >> t;while (t--) {Cat c;int n;cin >> n;while (n--) {string s;cin >> s;if (s == "ADD")c.ADD();else if (s == "UP")c.UP();else if (s == "DOWN")c.DOWN();elsec.DELETE();}c.print();}return 0;
}