C++内部体量
C++ int massives
我试图通过分类和向不同的体量投掷重物来解决众所周知的任务"石头堆"。但程序现在工作起来很奇怪,它在调试模式下显示了正确的体量和元素总数,但在运行中显示了不同的、不正确的答案。
这是代码:
void Sort(int pile[],int N)
{
int tmp=0;
for(int i=0; i<N-1; i++)
{
for(int j=0; j<N-i-1; j++)
{
if(pile[j]<pile[j+1])
{
tmp=pile[j];
pile[j]=pile[j+1];
pile[j+1]=tmp;
}
}
}
}
void Show(int pile[], int N)
{
cout<<endl;
for(int i=0; i<N; i++)
{
cout<<pile[i]<<" ";
}
}
void Alternate()
{
while(1)
{
int *pile , *LHeap, *RHeap, N=0, RCount=0, LCount=0, RIter=1,LIter=1;
cout<<Rus("nВведите количество камней: ");
cin>>N;
///dynamic memory for N-size mass
pile=new int(N);
LHeap=new int(N);
RHeap=new int(N);
///App close if 0 size of mass entered
if(N==0) break;
else
{
cout<<Rus("Введите вес всех камнейn");
for(int i=0; i<N; i++)
{
cin>>pile[i];
}
Sort(pile, N);
Show(pile, N);
///make first elements of heap's equal to 1 and 2 elements of pile
///Count'z to count the current size of mass
LHeap[0]=pile[0];
LCount+=pile[0];
RHeap[0]=pile[1];
RCount+=pile[1];
///fulfill both massive
for(int i=2; i<N; i++)
{
if(LCount<=RCount)
{
LHeap[LIter]=pile[i];
LIter++;
LCount+=pile[i];
}
else
{
RHeap[RIter]=pile[i];
RIter++;
RCount+=pile[i];
}
}
cout<<Rus("nкуча 1: ");
Show(LHeap,LIter);
cout<<Rus("nкуча 2: ");
Show(RHeap,RIter);
cout<<Rus("nСумы в кучах: ")<<LCount<<" "<<RCount<<endl;
}
}
}
int main()
{
//Primitive();
Alternate();
_getch();
return 0;
}
由于程序未定义,因此会得到不同的结果。
这个
new int(N)
将值CCD_ 2分配给一个CCD_
未定义从该指针索引0以外的任何索引。
要分配阵列,请使用
new int[N]
相关文章:
- 将函数类成员映射到类本身内部
- Boost Spirit,获取迭代器内部语义动作
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 内联函数中具有内部链接的全局变量
- 在函数内部的声明中初始化数组,并在外部使用它
- 如何在不知道向量大小的情况下输入向量内部的向量?
- 卷曲bracers内部结构的声明
- 从函数角度看ID到文件路径的内部与外部映射
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- 如何在pugixml中获取节点的内部XML
- 使用C链接在函数内部创建C++模板
- 指针没有更新它在void函数内部指向的值
- 方法内部但循环仍得到预期的不合格id错误C++
- C++:具有内部链接的正向声明常量
- SDL_PollEvent() 无法捕获类函数内部SDL_QUIT?
- libcurl 和 DNS ttl 中的内部连接管理
- 如何修改 lambda 内部字符串的向量
- 如果我将嵌套映射作为多重映射的值,如何将值插入内部映射?
- 来自 Android 应用程序内部的 boost 类型的 boost::wrapexcept<boost::system::system_error> 的未捕获异常
- 类内部和外部静态 constexpr 元组之间的差异