【发布时间】:2018-07-07 04:10:38
【问题描述】:
Ratiorg 从 CodeMaster 那里得到了不同尺寸的雕像作为生日礼物,每个雕像都有一个非负整数大小。因为他喜欢把事情做得完美,所以他想把它们从小到大排列,这样每个雕像都会比前一个大一倍。他可能需要一些额外的雕像才能做到这一点。帮助他计算出最少需要多少个额外雕像。
例子
对于雕像 = [6, 2, 3, 8],输出应该是 makeArrayConsecutive2(statues) = 3.
这是代码冲突的问题。 这是我写在下面的代码
#include <iostream>
#include <vector>
#include<algorithm>
using std::vector;
int makeArrayConsecutive2(std::vector <int> statues) {
vector<int>::size_type size = statues.size();
sort( statues.begin(), statues.end() );
int counter = 0;
for( int i = 0; i<size; i++ )
{
int dif = statues[i+1] - statues[i] - 1;
if( dif >= 1 ) {counter+=dif;}
}
return counter;
}
int main()
{
vector<int> c = {1,2,3,4,5,6,7,8,9,10};
std :: cout<<"You need "<<makeArrayConsecutive2(c)<<" statues"<<std::endl;
return 0;
}
当我使用向量 c 的这个特定值运行代码时,它会输出误解值。所有其他情况都运行正确,但是当我声明 10 维向量(我的意思是具有 10 个值的向量)时,它就不能正常工作。请问说明什么问题?
【问题讨论】:
-
...你怎么能混淆维度和值?
-
你试过调试吗? (提示:定义
_GLIBCXX_DEBUG,大多数情况下,当您误用 STL 组件时,程序会发出警告。) -
太好了,你不用
using namespace std。
标签: c++ sorting c++11 vector stdvector