(。・∀・)ノ゙嗨!
我打代码!
像cxk!!!!!!!
https://cloud.tencent.com/developer/news/400671
————————————————————————————————————————————————————————————————————————
言归正传
第一题:


1 #ifndef CAR_H 2 #define CAR_H 3 4 #include<string> 5 #include<iostream> 6 using namespace std; 7 class Car{ 8 private: 9 string maker,model; 10 int year,odometer; 11 public: 12 Car(string a,string b,int c); 13 void updateOdometer(int a); 14 friend ostream & operator<<(ostream &out,const Car &a); 15 }; 16 17 #endif


1 #include<string> 2 #include<iostream> 3 using namespace std; 4 5 #include"car.h" 6 Car::Car(string a,string b,int c):maker(a),model(b),year(c),odometer(0){ 7 } 8 void Car::updateOdometer(int a){ 9 odometer=a; 10 } 11 ostream & operator<<(ostream &out,const Car &a){ 12 out<<"maker:"<<a.maker 13 <<"\nmodel:"<<a.model 14 <<"\nyear:"<<a.year 15 <<"\nodometer:"<<a.odometer; 16 return out; 17 }


1 #ifndef BATTERY_H 2 #define BATTERY_H 3 4 class Battery{ 5 private: 6 int batterysize; 7 public: 8 Battery(); 9 int getBattery()const; 10 }; 11 12 #endif


1 #include"battery.h" 2 #include<iostream> 3 using namespace std; 4 Battery::Battery():batterysize(70){ 5 } 6 int Battery::getBattery()const{ 7 return batterysize; 8 }


1 #ifndef ELECTRICCAR_H 2 #define ELECTRICCAR_H 3 4 #include<iostream> 5 #include<string> 6 using namespace std; 7 8 #include"car.h" 9 #include"battery.h" 10 class ElectricCar:public Car,public Battery{ 11 private: 12 Battery battery; 13 public: 14 ElectricCar(string a,string b,int c); 15 friend ostream & operator<<(ostream &out,const ElectricCar &a); 16 }; 17 18 #endif


1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 #include"electricCar.h" 6 ElectricCar::ElectricCar(string a,string b,int c):Car(a,b,c){ 7 } 8 ostream & operator<<(ostream &out,const ElectricCar &a){ 9 out<<(Car)a<<endl; 10 out<<"battery:"<<a.battery.getBattery()<<"-Wh"<<endl; 11 return out; 12 }


1 #include <iostream> 2 using namespace std; 3 4 #include "car.h" 5 #include "electricCar.h" 6 int main() { 7 // 测试Car类 8 Car oldcar("Audi","a4",2016); 9 cout << "--------oldcar's info--------" << endl; 10 oldcar.updateOdometer(25000); 11 cout << oldcar << endl; 12 13 // 测试ElectricCar类 14 ElectricCar newcar("Tesla","model s",2016); 15 newcar.updateOdometer(2500); 16 cout << "\n--------newcar's info--------\n"; 17 cout << newcar << endl; 18 19 system("pause"); 20 21 return 0; 22 }
第二题:
补足部分:
arrayInt.h:
int & operator[](int a);
arrayInt.cpp:
int & ArrayInt::operator[](int a){
return p[a];
}
可能遇到的问题:
1、Id returned 1 exit status
解决方案:
看到那个按钮了没有?对,点它!
2、"nullptr" is not declared in this scope
解决方案:
nullptr是c++11开始使用的指针(-str=c++11),如果出现该问题,请升级编译器或直接换一个编译器。?
3、不能访问被继承的类的部分:
解决方案:
看到那个const了没有?
什么?没有?
那我不告诉你(pia~~)。
嗯哼。
以我第一次码的代码为例,
1 ostream & operator<<(ostream &out,const ElectricCar &a){ 2 out<<(Car)a<<endl; 3 out<<"battery:"<<a.battery.getBattery()<<"-Wh"<<endl; 4 return out; 5 }
class Battery{ private: int batterysize; public: Battery(); int getBattery(); };
解决方案:
问题出在,ElectricCar中为
ostream & operator<<(ostream &out,const ElectricCar &a)
而在Battery中为
int getBattery()
这涉及到了权限扩大问题,const中出现了非const(大概是这个意思吧,错了别打我)
所以,方法有二,
一、ostream & operator<<(ostream &out,ElectricCar &a)
二、int getBattery() const
以上です
——————————————————————————————————————————————————————————————————
关于oj:
1、貌似oj会检测所有程序结束是未释放空间的变量(即作用域持续到程序结束的变量),如果输出结果有要求,例如为long,若在未释放空间变量中存在非long型,则会报错(wrong answer)
2、八皇后问题:


1 #include<iostream> 2 using namespace std; 3 int a[8][8]={0}; 4 int sum=0; 5 int pan(int ai,int aj){ 6 for(int i=0;i<8;i++) 7 if(a[ai][i]==1) return 0; 8 for(int i=0;i<8;i++) 9 if(a[i][aj]==1) return 0; 10 for(int i=0;i<8&&ai+i<8&&aj+i<8;i++) 11 if(a[ai+i][aj+i]==1) return 0; 12 for(int i=0;i<8&&ai-i>=0&&aj-i>=0;i++) 13 if(a[ai-i][aj-i]==1) return 0; 14 for(int i=0;i<8&&ai+i<8&&aj-i>=0;i++) 15 if(a[ai+i][aj-i]==1) return 0; 16 for(int i=0;i<8&&ai-i>=0&&aj+i<8;i++) 17 if(a[ai-i][aj+i]==1) return 0; 18 return 1; 19 } 20 int last(int i){ 21 if(i==0) return 1; 22 for(int j=0;j<8&&i;j++){ 23 if(a[i-1][j]==1) return 1; 24 } 25 return 0; 26 } 27 void f(int i0){ 28 for(int i=i0;i<8&&last(i);i++) 29 for(int j=0;j<8&&last(i);j++){ 30 if(pan(i,j)){ 31 if(i==7) sum++; 32 else{ 33 a[i][j]=1; 34 f(i+1); 35 a[i][j]=0; 36 } 37 } 38 } 39 } 40 int main(){ 41 for(int i=0;i<8;i++) 42 for(int j=0;j<8;j++) 43 cin>>a[i][j]; 44 //a[i][j]=0; 45 f(0); 46 cout<<sum<<endl; 47 }
以上です
。 鸡你太美
鸡你实在太美
鸡你是太美
鸡你太美
实在是太美鸡你
鸡你 实在是太美鸡你 美
鸡你 实在是太美鸡美 太美
鸡你 实在是太美鸡美 太美
鸡你 实在是太美鸡美 太美
鸡你 鸡你实在是美太美 美蓝球球
鸡 鸡 鸡你实在是太美 篮球篮球球
鸡 鸡你太美裆鸡太啊 蓝篮球
鸡你太美裆裆鸡美
鸡你美裆 裆鸡美
鸡太美 鸡太美
鸡美 鸡美
鸡美 鸡美
鸡美 鸡美
鸡太 鸡太
金 猴 金猴
皮 鞋 皮鞋金猴
金光 金光 大道
大道