C 我的代码工作不正常,我做错了吗?
最大元素 您有一个空序列,并且将为您提供N个查询。每个查询都是以下三种类型之一: 1 x-将元素x推入堆栈。 2-删除堆栈顶部的元素。 3-打印堆栈中的最大元素。C 我的代码工作不正常,我做错了吗?,c,C,最大元素 您有一个空序列,并且将为您提供N个查询。每个查询都是以下三种类型之一: 1 x-将元素x推入堆栈。 2-删除堆栈顶部的元素。 3-打印堆栈中的最大元素。 输入格式 输入的第一行包含一个整数N。接下来的N行分别包含上述查询。(保证每个查询都是有效的。) 约束 1.≤N≤105 1.≤x≤109 1.≤类型≤3 输出格式 对于每个类型3查询,在新行上打印堆栈中的最大元素 样本输入 10 197 2 120 2 126 120 2 3 191 3 样本输出 26 91 我的代码是: #包括
输入格式 输入的第一行包含一个整数N。接下来的N行分别包含上述查询。(保证每个查询都是有效的。) 约束
1.≤N≤105
1.≤x≤109
1.≤类型≤3
输出格式 对于每个类型3查询,在新行上打印堆栈中的最大元素 样本输入 10
197
2
120
2
126
120
2
3
191
3
样本输出 26
91 我的代码是:
#包括
#包括
#定义最大尺寸100000
长a[最大尺寸];
int top=-1;
整数推送(长x){
如果(顶部==最大尺寸-1){
}
top++;
a[顶部]=x;
返回0;
}
int-Pop(){
如果(顶部==-1){
}
顶部--;
返回0;
}
int Print(){
int i;
对于(i=0;i而言,请求为:
“对于每个类型3查询,在新行上打印堆栈中的最大元素。”
您的代码所做的是打印所有堆栈元素。
顺便说一句,如果Push
和Pop
中的语句体为空,则是什么?发布的代码的主要问题是Print()
函数正在打印所有内容,而不是遵循只打印当前堆栈上的最大值的约束
给出发布的代码,稍微清理一下,修复Print()
函数,使用有意义的变量名,并使用垂直和水平间距以提高可读性,结果是:
#include <stdio.h>
#include <stdlib.h>
#define MAX_STACK_SIZE (105)
// prototypes
void Print( void );
void Pop( void );
void Push( int value );
int stack[ MAX_STACK_SIZE ];
int stackIndex = -1;
enum action {pushValue=1, popValue=2, printMax=3};
int main( void )
{
int value;
int numEntries;
enum action myAction;
// get number of entries range 1...105
scanf("%d",&numEntries);
for( int i=0; i<numEntries; i++ )
{
// get action range 1...3
scanf("%d", (int*)&myAction);
switch( myAction )
{
case pushValue :
// get value range 1...109
scanf("%d",&value);
Push( value );
break;
case popValue :
Pop();
break;
case printMax :
Print();
break;
} // end switch
} // end for
return 0;
} // end function: main
void Push(int value)
{
stackIndex++;
stack[stackIndex] = value;
}
void Pop()
{
if( 0 <= stackIndex)
{ // then stack not empty
stackIndex--;
}
}
void Print()
{
int maxValue = -1;
int i;
for( i = 0; i<= stackIndex; i++ )
{
if( stack[i] > maxValue )
{
maxValue = stack[i];
}
}
if( -1 != maxValue )
{
printf("%d\n", maxValue);
}
}
这在Dev-c编译器中运行良好,但在hackerrack online编译器中不起作用。更多的描述问题标题将很有帮助……您的Push
和Pop
函数具有if
语句,这些语句不起任何作用。(看起来您打算在此处执行一些错误处理。)这是为了回答这个问题吗?如果不是,那应该是一个评论。@KeithThompson:嗯,问题是“我做错了什么”,这就是答案——打印整个堆栈而不是顶部元素。
#include <stdio.h>
#include <stdlib.h>
#define MAX_STACK_SIZE (105)
// prototypes
void Print( void );
void Pop( void );
void Push( int value );
int stack[ MAX_STACK_SIZE ];
int stackIndex = -1;
enum action {pushValue=1, popValue=2, printMax=3};
int main( void )
{
int value;
int numEntries;
enum action myAction;
// get number of entries range 1...105
scanf("%d",&numEntries);
for( int i=0; i<numEntries; i++ )
{
// get action range 1...3
scanf("%d", (int*)&myAction);
switch( myAction )
{
case pushValue :
// get value range 1...109
scanf("%d",&value);
Push( value );
break;
case popValue :
Pop();
break;
case printMax :
Print();
break;
} // end switch
} // end for
return 0;
} // end function: main
void Push(int value)
{
stackIndex++;
stack[stackIndex] = value;
}
void Pop()
{
if( 0 <= stackIndex)
{ // then stack not empty
stackIndex--;
}
}
void Print()
{
int maxValue = -1;
int i;
for( i = 0; i<= stackIndex; i++ )
{
if( stack[i] > maxValue )
{
maxValue = stack[i];
}
}
if( -1 != maxValue )
{
printf("%d\n", maxValue);
}
}
26
91