我注意到,如果我使用 push_back 在向量中插入整数,我会得到 1 的答案,但如果我通过 vector[0] 将整数插入向量中,我会得到正确的答案。我的问题是,push_back 做的不仅仅是将我的整数插入向量中吗?
#include<vector>
#include<iostream>
using namespace std;
typedef vector<int> vi;
class KeyDungeonDiv2 {
public:
int countDoors(vi doorR, vi doorG, vi keys)
{
int r = keys[0];
int g = keys[1];
int w = keys[2];
int numdoors = 0;
for(int i = 0; i < doorR.size(); i++) {
if(r >= doorR[i] && g >= doorG[i])
numdoors++;
else if (r < doorR[i] && g >= doorG[i]) {
if(r + w >= doorR[i]) {
numdoors++;
}
}
else if(r >= doorR[i] && g < doorG[i]) {
if(g + w >= doorG[i]) {
numdoors++;
}
}
else if (r < doorR[i] && g < doorG[i]) {
if(w >= (doorR[i] - r ) + (doorG[i] - g)) {
numdoors++;
}
}
else if(doorR[i] == 0 && doorG[i] == 0)
numdoors++;
}
return numdoors;
}
};
int main()
{
vector<int> redDoors (4);
redDoors[0] = 2;
redDoors[1] = 0;
redDoors[2] = 5;
redDoors[3] = 3;
vector<int> greenDoors(4);
greenDoors[0] = 1;
greenDoors[1] = 4;
greenDoors[2] = 0;
greenDoors[3] = 2;
vector<int> keys (3);
keys[0] = 2;
keys[1] = 3;
keys[2] = 1;
KeyDungeonDiv2 d;
cout << d.countDoors(redDoors,greenDoors,keys) << endl;
return 0;
}
----对----
vector<int> redDoors (4);
redDoors.push_back(2);
redDoors.push_back(0);
redDoors.push_back(5);
redDoors.push_back(3);
vector<int> greenDoors(4);
greenDoors.push_back(1);
greenDoors.push_back(4);
greenDoors.push_back(0);
greenDoors.push_back(2);
vector<int> keys (3);
keys.push_back;
keys.push_back(3);
keys.push_back(1);