前天看的时候发现46到64题忘了做,相当于补档把;
#include <iostream>
using namespace std;
class BaseCalculator {
public:
int m_A;
int m_B;
// write your code here......
virtual int getResult() = 0;
};
// 加法计算器类
class AddCalculator : public BaseCalculator {
// write your code here......
virtual int getResult(){
return m_A+m_B;
}
};
// 减法计算器类
class SubCalculator : public BaseCalculator {
// write your code here......
virtual int getResult(){
return m_A-m_B;
}
};
int main() {
BaseCalculator* cal = new AddCalculator;
cal->m_A = 10;
cal->m_B = 20;
cout << cal->getResult() << endl;
delete cal;
cal = new SubCalculator;
cal->m_A = 20;
cal->m_B = 10;
cout << cal->getResult() << endl;
delete cal;
return 0;
}
多态的具体运用:
当两个子类继承一个公共父类时,在父类中的虚函数不进行任何函数实现,也就是纯虚函数,而两个不同的子类中定义与父类纯虚函数同名的虚函数;在主函数main()中,用父类实例化一个指针,指向一个堆区开辟的子类,通过该指针调用即可访问子类的同名虚函数实现,这种技术叫做多态。
#include <iostream>
// write your code here......
#include <vector>
using namespace std;
int main() {
// write your code here......
vector<int> vec;
for (int i =0 ; i!=5; ++i) {
int temp;
cin>>temp;
vec.push_back(temp);
}
for(vector<int>::iterator it =vec.begin();it!=vec.end();++it){
cout<<*it<< " ";
}
cout<<endl;
for(vector<int>::iterator it =vec.end()-1;it>=vec.begin();--it){
cout<<*it<< " ";
}
return 0;
}
#include <iostream>
#include <deque>
using namespace std;
class Guest {
public:
string name;
bool vip;
Guest(string name, bool vip) {
this->name = name;
this->vip = vip;
}
};
int main() {
Guest guest1("张三", false);
Guest guest2("李四", false);
Guest vipGuest("王五", true);
deque<Guest> deque;
// write your code here......
deque.emplace_back(guest1);
deque.emplace_back(guest2);
deque.emplace_front(vipGuest);
for (Guest g : deque) {
cout << g.name << " ";
}
return 0;
}
感觉这个题是题目有点问题,怎么输入就固定死了,不是用户自定义输入;
#include <cstring>
#include <iostream>
// write your code here......
#include <set>
using namespace std;
int main() {
char str[100] = { 0 };
cin.getline(str, sizeof(str));
// write your code here......
int len=strlen(str);
set<char> s(str,str+len);
for(auto it:s){
cout<<it;
}
return 0;
}
set初始化可以用指针,传入起始指针和终点指针就可以对set进行初始化
#include <cctype>
#include <iostream>
// write your code here......
#include <map>
using namespace std;
int main() {
char str[100] = { 0 };
cin.getline(str, sizeof(str));
// write your code here......
map<char,int>m;
for(int i=0;str[i]!='\0';++i){
if(isalpha(str[i])){
m[str[i]]++;
}
}
for(auto it:m){
cout<<it.first<<":"<<it.second<<endl;
}
return 0;
}
这个题我是看了解析,因为一开始对map的插值一直不成功,在数组上对map进行插值时,判断循环终止条件 写 str[i]!='\0',if内容的意义为判断数组某一下标的内容是否是str,是的话对其值++
#include <functional>
#include <iostream>
#include <vector>
// write your code here......
#include <algorithm>
using namespace std;
int main() {
int num;
vector<int> v;
for (int i = 0; i < 5; i++) {
cin >> num;
v.push_back(num);
}
// write your code here......
sort(v.begin(), v.end(), greater<int>());
for(auto it:v){
cout<<it<<" ";
}
return 0;
}
在C++中的sort函数内部实现的有3种不同的具体实现,可以根据传入参数的数量级使用快速排序,插入排序和堆排序。其调用参数有3个
其中参数一:排序数组的起始
参数二:排序数组的终末
参数三:默认为从小到大排序,若要从大到小,需要传入greater<int>()函数
#include <cctype>
#include <iostream>
#include <cstring>
using namespace std;
int main() {
int letter = 0;
int digit = 0;
int space = 0;
int other = 0;
char buf[1024] = {0};
cin.getline(buf, sizeof(buf));
// write your code here......
for(auto it=buf;*it!='\0';it++){
if(isalpha(*it)){
letter++;
}else if (isdigit(*it)) {
digit++;
}else if (isspace(*it)) {
space++;
}else{
other++;
}
}
cout << "letter:" << letter << " digit:" << digit << " space:" << space << " other:" << other << endl;
return 0;
}
循环条件中,把buf赋值给it,那么auto关键字会自动将其匹配为指针,终止条件为!='\0',it为指针,所有后续都需要对其解引用;
#include <iostream>
// write your code here......
#include <string>
#include <iomanip>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
class A{
private:
double m_pro;//税率
int m_dollor;//速算扣除元
public:
double getpro(){
return m_pro;
}
int getdollor(){
return m_dollor;
}
void judge(int res){
if(res<=1500){
m_pro=0.03;
m_dollor=0;
}else if (res>1500 && res<=4500) {
m_pro = 0.1;
m_dollor = 105;
}else if (res >4500 && res <=9000) {
m_pro = 0.2;
m_dollor =555;
}else if (res > 9000 && res<=35000) {
m_pro = 0.25;
m_dollor = 1005;
}else if (res >35000 && res <=55000) {
m_pro = 0.3;
m_dollor = 2775;
}else if (res >55000 && res<=80000) {
m_pro = 0.35;
m_dollor = 5505;
}else{
m_pro =0.45;
m_dollor = 13505;
}
}
};
class Employee {
friend class A;
private:
string name;
double salary;
// write your code here......
public:
Employee(string m_name,double m_salary):name(m_name),salary(m_salary){}
int getsalary(){
return salary;
}
void showPrioty(){
A a;
int Month = salary-3500;
a.judge(Month);
double res = Month * a.getpro() -a.getdollor();
cout<<name<<"应该缴纳的个人所得税是:"<<fixed<<setprecision(1)<<res<<endl;
}
};
bool mysort(Employee &em1, Employee &em2){
return em1.getsalary() >em2.getsalary();
}
int main() {
// write your code here......
vector<Employee> vec;
Employee a("张三",6500);
Employee b("李四",8000);
Employee c("王五",100000);
vec.push_back(a);
vec.push_back(b);
vec.push_back(c);
sort(vec.begin(),vec.end(),mysort);
for(auto it:vec){
it.showPrioty();
}
return 0;
}
这个题很有意思,做了我差不多二十多分钟;排序这里自定排序规则是个难点,我也是查了资料才知道C++ algorithm中 的sort函数的第
三个参数可以自定义已完成对自定类型的vector排序
#include <iostream>
#include <strings.h>
using namespace std;
int main() {
char str[100] = { 0 };
cin.getline(str, sizeof(str));
// write your code here......
string a="";
string b="";
string c="";
int space=0;
for(int i=0; str[i] != '\0';i++){
if (str[i]==' ') {
space++;
continue;
}
else if (space==0) {
a +=str[i];
}else if (space==1) {
b+=str[i];
}else if (space==2) {
c+=str[i];
}
}
int first=atoi(b.c_str());
int second=atoi(c.c_str());
if(strcasecmp(a.c_str(), "add")==0){
cout<<first+second<<endl;
}else if(strcasecmp(a.c_str(), "sub")==0){
cout<<first-second<<endl;
}else if(strcasecmp(a.c_str(), "mul")==0){
cout<<first * second <<endl;
}else if(strcasecmp(a.c_str(), "div")==0){
if(second==0){
cout<<"Error"<<endl;
return -1;
}
cout<<(double)first/second<<endl;
}
return 0;
}
这个题给我恶心坏了,我自己写的检测怎么都不对,我还因为想着用正则表达式做,但是正则表达式用的太少了,我咋想都没思路,最后用字符串的方法自己写,写出来怎么检测都不对;我自己在if else中加了一个最后的else输出cout<<"检测出错"<<endl;结果牛客网不管怎么输出都输出我自定义的检测出错,我把它删了还是输出检测出错;最后也是参考了一下题解做出来;
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
string toHexString(int n);
int main() {
int n;
cin >> n;
string hexStr = toHexString(n);
cout << hexStr << endl;
return 0;
}
string toHexString(int n) {
// write your code here......
string res="";
while(n>0){
int m=n%16;
if(m>=0&&m<=9) res+=m+'0';
else res+=m-10+'A';
n=n/16;
}
reverse(res.begin(), res.end());
return res;
}
#include<bits/stdc++.h>
#include <string>
using namespace std;
int main(){
string s;
cin>>s;
// write your code here......
int a=0,b=0,c=0;
for(int i=0;i<s.length();++i){
if (s[i]=='a') {
a++;
}else if (s[i]=='b') {
b++;
}else{
c++;
}
}
cout<<a<<" "<<b<<" "<<c<<endl;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
// write your code here......
int *arr=new int[n*n];
for(int i=0;i!=n;++i){
for(int j=0;j!=n;++j){
arr[i*n+j]=i+j;
cout<<arr[i*n+j]<<" ";
}
cout<<endl;
}
for(int i=0;i!=n*n;++i){
delete []arr;
}
arr=NULL;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
// write your code here......
void rever(string &s){
for(int i=0;i!=(s.length())/2;++i){
int temp=s[i];
s[i]=s[s.length()-i-1];
s[s.length()-i-1]=temp;
}
}
int main(){
string s;
getline(cin,s);
// write your code here......
rever(s);
cout<<s;
return 0;
}
这里互换代码中的length()-i-1需要细细斟酌一下,因为我们数数都是从1开始,而计算机往往是从0开始,所以-1才比较准确;
#include<bits/stdc++.h>
using namespace std;
class rectangle{
private:
int length,width;
public:
void set(int x,int y){
length=x;
width=y;
}
int getlength(){
return length;
}
int getwidth(){
return width;
}
int area(){
return length*width;
}
void compare(rectangle a){
// write your code here......
if(this->area()>a.area()){
cout<<1<<endl;
}else{
cout<<0<<endl;
}
}
};
int main(){
int l1,w1,l2,w2;
cin>>l1>>w1>>l2>>w2;
rectangle a,b;
a.set(l1,w1);
b.set(l2,w2);
a.compare(b);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
class rectangle{
private:
int length,width;
public:
void set(int x,int y){
length=x;
width=y;
}
int getlength(){
return length;
}
int getwidth(){
return width;
}
int area(){
return length*width;
}
// write your code here......
string cancover(rectangle a){
string res;
if(max(this->length,this->width)>=max(a.getlength(),a.getwidth())
&& min(this->length,this->width)>=min(a.getlength(),a.getwidth()))
{
res="yes";
}else {
res="no";
}
return res;
}
};
int main(){
int l1,w1,l2,w2;
cin>>l1>>w1>>l2>>w2;
rectangle a,b;
a.set(l1,w1);
b.set(l2,w2);
cout<<a.cancover(b);
return 0;
}
逻辑也比较简单,要包含则大的大于大的,小的大于小的即可;
#include<bits/stdc++.h>
using namespace std;
class Array{
private:
int n;//数组大小
int *a;//数组
public:
// write your code here......
Array(){
int myn;
cin>>myn;
n=myn;
a =new int[n];
for(int i=0;i!=n;++i){
cin>>a[i];
}
}
~Array(){
delete []a;
}
void show(){
for (int i=0;i<n;i++) cout<<a[i]<<' ';
}
};
int main(){
Array a;
a.show();
return 0;
}
#include<bits/stdc++.h>
using namespace std;
class Array{
private:
int n;//数组大小
int *a;//数组
public:
Array(){
cin>>n;
a=new int [n];
for (int i=0;i<n;i++) cin>>a[i];
}
~Array(){
delete []a;
}
int getlen(){
return n;
}
int get(int i){
return a[i];
}
// write your code here......
Array(Array &b){
delete [] a;
n=b.getlen();
a=new int[n];
for(int i=0;i!=n;++i) a[i]=b.get(i);
}
void show(){
for (int i=0;i<n;i++) cout<<a[i]<<' ';
}
};
int main(){
Array a;
Array b=a;
b.show();
return 0;
}
这里系统把getlen和get写好是有原因的,我发现我真的是笨,半天都没想到
#include<bits/stdc++.h>
using namespace std;
class myphone;
class phone{
// write your code here......
friend myphone;
private:
int price;
public:
phone(int x){
price=x;
}
};
class myphone{
private:
phone a;
public:
myphone(int x):a(x){
}
int getprice(){
return a.price;
}
};
int main(){
int p;
cin>>p;
myphone a(p);
cout<<a.getprice();
return 0;
}
#include <iostream>
using namespace std;
class Time {
public:
int hours; // 小时
int minutes; // 分钟
Time() {
hours = 0;
minutes = 0;
}
Time(int h, int m) {
this->hours = h;
this->minutes = m;
}
void show() {
cout << hours << " " << minutes << endl;
}
// write your code here......
bool operator<(const Time t2){
if(hours*60+minutes < t2.hours*60+t2.minutes){
return true;
}else {
return false;
}
}
};
int main() {
int h, m;
cin >> h;
cin >> m;
Time t1(h, m);
Time t2(6, 6);
if (t1<t2) cout<<"yes"; else cout<<"no";
return 0;
}
在类内重载符号时,只需要传入 符号右值的对象,自身属于这个类不需要加定义域