我已经使用数组解决了这个问题,但是不能使用向量来解决这个问题。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int age, count = 0, i;
vector<int>length;
cin >> age;
for (i = 0; i <= age; i++)
{
length.push_back(i);
}
sort(length.begin(), length.end());
int max = length.back();
for (i = 0; i <= length.size(); i++)
{
if (length[i] == max)
count++;
}
cout << count;
return 0;
}
我希望第一个测试用例的输出为2
,但实际输出为1
。
问题出在这里-https://www.hackerrank.com/challenges/birthday-cake-candles/problem
答案 0 :(得分:2)
根据问题(在链接的网站中),您错过了n
,该数字表示蛋糕上的蜡烛数。
您错过了。另外,您没有将height
推回向量(不是age
)。应该是
int n;
std::cin>> n; //>>>> this
for(int i=0; i<n; i++) // where n is the number of candles on the cake: equalent to the `age` of the girl
{
int height; std::cin >> height; // height of each candles
length.push_back(height);
}
完整的代码如下:
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
int n; std::cin >> n; //>>>> you missed this
std::vector<int> length;
length.reserve(n); // reserve the memory, as you know the size
for (int i = 0; i < n; i++) // where n is the number of candles on the cake: equalent to the `age` of the girl
{
int height; std::cin >> height; // height of each candles
length.push_back(height);
}
std::sort(length.begin(), length.end());
// see the `const` as `max` will not change latter in the program
const int max = length.back();
int count = 0;
for (const int element : length) // use range based for-loop, if possible
{
if (element == max) count++;
}
std::cout << count;
return 0;
}
但是,如果在输入时可以找到用户输入中的max-element,则可以避免调用std::sort
。另外,还有一个称为std::count
的标准算法函数,也可以用来查找已找到的最大元素的计数,如下所示。
#include <iostream>
#include <algorithm> // std::max, std::count
#include <vector>
#include <numeric> // std::numeric_limits
int main()
{
int n; std::cin >> n; // the number of candles on the cake
std::vector<int> length;
length.reserve(n); // reserve the memory, to avoid un-wanted reallocations
// set the maximum to INT_MIN
int max = std::numeric_limits<int>::min();
while (n--)
{
int element; std::cin >> element;
length.emplace_back(element);
// while inserting to the vector find the maximum element
max = std::max(element, max);
}
// using std::count find the count of `max` element and print!
std::cout << std::count(length.cbegin(), length.cend(), max);
return 0;
}
注意:避免练习using namespace std;
。详细阅读:Why is "using namespace std;" considered bad practice?
答案 1 :(得分:1)
int main()
{
int age, count=0; //, i; // keep variables as local as possible!
std::vector<int> length;
std::cin >> age;
// you don't check for stream state nor validity of `age`:
if(!std::cin || age < 0) // possibly some upper limit, too??
// side note: if you use unsigned int, you only have
// to check this upper limit!
{
// some appropriate error handling
}
// optimisation: we know necessary capacity in advance, so let's prevent
// otherwise necessary re-allocations:
length.reserve(age);
//for(i=0;i<=age;i++)
for(int i = 0; i < age; i++) // variables as local as possible, remember?
// <= reads one candle too much; consider age == 2:
// you'll get candles for i == 0, i == 1 and i == 2!
{
// well no, you don't want to push back the counter itself (0, 1, 2, ...)
// you need to read in candle hights instead!
//length.push_back(i);
int height;
std::cin >> height;
// check input as before!
length.push_back(height);
}
std::sort(length.begin(), length.end());
int max = length.back();
// for(i=0;i<=length.size(); i++)
// ^ again, one element too much!
// ...
现在,您可以变得更聪明;您已经排序,因此从中获利!迭代器的使用使操作更加轻松,您可以直接从后面进行迭代!
for(auto i = length.rbegin(); i != length.rend(); ++i)
{
if(*i != max)
{
count = i - length.rbegin();
break;
}
}
// special case: all candles have same height, then you would't have met the inner if:
if(count == 0)
{
count = length.size();
}
实际上,您可以一次执行而无需先排序:
int max = 0; // now let's assume we checked for negative values right from the start
// otherwise we'd need to use std::numeric_limits<int>::min() instead
// again I'd prefer the unsigned type...
for(auto height : length)
{
if(height == max)
++count;
else if(height > max)
count = 1;
// else: can ignore...
}
嗯...你注意到了吗?我们只是按照与读取值相同的顺序进行处理。这样我们就可以一次完成所有操作:
for(int i = 0; i < age; ++i)
{
unsigned int height;
std::cin >> height;
// check validities!
// instead of pushing back, directly calculate what we are interested in:
if(height == max)
++count;
else if(height > max)
count = 1;
}
答案 2 :(得分:0)
您正在超越向量的末尾。使用<,而不是<=。
for(i=0; i < length.size(); i++)
答案 3 :(得分:0)
fnTrait.someFn({ a: 123 }); // okay at compile time and runtime