【华为笔试题】程序发生段错误,可能是数组越界,堆栈溢出(比如,递归调用层数太多)

赤练仙子_ 2015-09-03 03:40:04
[编程题] 最高分是多少
老师想知道从某某同学当中,分数最高的是多少,现在请你编程模拟老师的询问。当然,老师有时候需要更新某位同学的成绩.

输入描述:
输入包括多组测试数据。
每组输入第一行是两个正整数N和M(0 < N <= 30000,0 < M < 5000),分别代表学生的数目和操作的数目。
学生ID编号从1编到N。
第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩
接下来又M行,每一行有一个字符C(只取‘Q’或‘U’),和两个正整数A,B,当C为'Q'的时候, 表示这是一条询问操作,他询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少
当C为‘U’的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。

输出描述:
对于每一次询问操作,在一行里面输出最高成绩.

输入例子:

5 7
1 2 3 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 4 5
U 2 9
Q 1 5

输出例子:

5
6
5
9
我的代码,调了很久,提示程序发生段错误,可能是数组越界,堆栈溢出(比如,递归调用层数太多),为什么啊,这程序很耗堆栈空间么,不懂。。。。。。求解
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
int N, M, A, B;
int score[N];
int i, max;
char C;

scanf("%d%d", &N, &M);

for (i = 0; i < N; i++)
{
scanf("%d", &score[i]);
}

scanf("%c%d%d", &C, &A, &B);

if (C == 'Q')
{
max = score[A];
for (i = A; i <= B; i++)
{
if (score[i] < score[i+1])
{
max = score[i+1];
}
}
printf("%d\n", max);
}
if (C == 'U')
{
score[A-1] = B;
}
return 0;
}
...全文
2713 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
第一,首先需要注意数组的大小,如果用vector,就不需要在使用前约定范围了;
第二,需要注意最大值查找的范围,如果开始的位置>结束的位置,就会出错,这里需要加一个判断;
第三,多个case,可以输入多组测试用例;
如果这三点都可以解决,就没什么问题了。
lm_whales 2015-09-12
  • 打赏
  • 举报
回复
U A B ID 为A 的成绩更新为 B 假设 A=3,B=6 U 3 6 的意思是 成绩为 1 2 3 4 5 ID 现在和成绩相等 更改后 变成 1 2 6 4 5 估计是你理解错了更改的意思了 假设用数组储存成绩 用ID-1表示下标 int scores[5];表示五个学生的成绩 U 3 6 就是 scores[3-1] =6; 这种题目,其实就相当于,做个 命令语言的解释器 U 3 6 命令 :把第三个学生(ID=3 的学生)的成绩改为6 Q 1 5 命令 :输出 ID从1 到 5 的学生中,的成绩最好学生的分数
赤练仙子_ 2015-09-12
  • 打赏
  • 举报
回复
引用 12 楼 Falleyes 的回复:
你的代码水平,慢慢提高吧,多看看基础的东西。话说你是哪个省的,华为校招已经开始了啊?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

int main(void){
	int N, M, A, B;
	int* score;
	int i, max;
	char C;

	scanf("%d%d", &N, &M);

	score = (int*)malloc(sizeof(int)*N);

	for (i = 0; i < N; i++)
		scanf("%d", &score[i]);

	while (M--){
		fflush(stdin);
		scanf("%c%d%d", &C, &A, &B);
		if (C == 'Q'){
			max = score[A - 1];
			for (i = A - 1; i < B; i++){
				if (max < score[i + 1])
					max = score[i + 1];
			}
			printf("%d\n", max);
		}
		if (C == 'U')
			score[A - 1] = B;
	}

	free(score);

	return 0;
}
max = score[i + 1]这句数组越界,总算对了
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main(void)
{
    char C;
    int N, M, A, B;
    int *score;
    int i, max, temp;
    
    
    while(scanf("%d%d", &N, &M) == 2)
    {
        
    	score = (int *)malloc(sizeof(int)*N);
	
    	for (i = 0; i < N; i++)
    	{
        	scanf("%d", &score[i]);
    	}
  
    
    	while (M--)
    	{
    		scanf(" %c%d%d", &C, &A, &B);
    	
        	if (C == 'Q')
    		{        
                if (A > B)
                {
                    temp = A;
                    A = B;
                    B = temp;
                }
                max = score[A-1];
        	
        		for (i = A; i < B; i++)
        		{
       	    		if (max < score[i])
            		{
            		max = score[i];	
            		}
        		}
        		printf("%d\n", max);
    		}
    		if (C == 'U')
    		{
        		score[A-1] = B;
    		} 
    	}
        free(score);
    }
    return 0;
}
Falleyes 2015-09-05
  • 打赏
  • 举报
回复
引用 14 楼 lizi_stdio 的回复:
[quote=引用 12 楼 Falleyes的回复:]你的代码水平,慢慢提高吧,多看看基础的东西。话说你是哪个省的,华为校招已经开始了啊?
你这个只有1组数据。这题怎么提交?[/quote] 可以改成循环测试多组数据,我这个程序只提供基本的改正。 提交。。。我也不清楚楼主的是哪个区的,这已经开始笔试了。总之,我这边山东区还没有开始正式笔试。你投了简历的话就慢慢等吧
707wk 2015-09-05
  • 打赏
  • 举报
回复
参考代码
/**************************************
 *FILE    :C:\Users\Administrator\desktop\214\main.cpp
 *PROJECT :NULL
 *AUTHOR  :707wk
 *CREATED :2015/9/5 10:01:36
 *TEXT    :NULL
 *EMAIL   :gtsoft_wk@foxmail.com
 *CODE    :https://github.com/707wk
 *LOGO    :
               #########                       
              ############                     
              #############                    
             ##  ###########                   
            ###  ###### #####                  
            ### #######   ####                 
           ###  ########## ####                
          ####  ########### ####               
        #####   ###########  #####             
       ######   ### ########   #####           
       #####   ###   ########   ######         
      ######   ###  ###########   ######       
     ######   #### ##############  ######      
    #######  ##################### #######     
    #######  ##############################    
   #######  ###### ################# #######   
   #######  ###### ###### #########   ######   
   #######    ##  ######   ######     ######   
   #######        ######    #####     #####    
    ######        #####     #####     ####     
     #####        ####      #####     ###      
      #####      ;###        ###      #        
        ##       ####        ####              

***************************************/

#include <stdio.h>

#define N 30000
#define M 5000

int score[N];

int main()
{
	int len=0;
	int operatenum=0;
	int A;
	int B;
	
	scanf("%d%d",&len,&operatenum);
	
	for(int i=0;i<len;i++)
	{
		scanf("%d",score+i);
	}
	
	for(int i=0;i<operatenum;i++)
	{
		char operate;
		getchar();
		scanf("%c%d%d",&operate,&A,&B);
		if(operate=='Q')
		{
			int maxindex=A-1;
			for(int j=A;j<B;j++)
			{
				if(*(score+maxindex)<*(score+j))maxindex=j;
			}
			printf("%d\n",*(score+maxindex));
		}	
		else
		{
			score[A-1]=B;
		}
	}
	
	return 0;
}
lizi_stdio 2015-09-05
  • 打赏
  • 举报
回复
引用 15 楼 Falleyes的回复:
[quote=引用 14 楼 lizi_stdio 的回复:] [quote=引用 12 楼 Falleyes的回复:]你的代码水平,慢慢提高吧,多看看基础的东西。话说你是哪个省的,华为校招已经开始了啊?
你这个只有1组数据。这题怎么提交?[/quote] 可以改成循环测试多组数据,我这个程序只提供基本的改正。 提交。。。我也不清楚楼主的是哪个区的,这已经开始笔试了。总之,我这边山东区还没有开始正式笔试。你投了简历的话就慢慢等吧[/quote] 想提交看结果的,算了,我弄弄其他题目。这题在笔试里面只能算简单题。
Falleyes 2015-09-04
  • 打赏
  • 举报
回复
你的代码水平,慢慢提高吧,多看看基础的东西。话说你是哪个省的,华为校招已经开始了啊?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

int main(void){
	int N, M, A, B;
	int* score;
	int i, max;
	char C;

	scanf("%d%d", &N, &M);

	score = (int*)malloc(sizeof(int)*N);

	for (i = 0; i < N; i++)
		scanf("%d", &score[i]);

	while (M--){
		fflush(stdin);
		scanf("%c%d%d", &C, &A, &B);
		if (C == 'Q'){
			max = score[A - 1];
			for (i = A - 1; i < B; i++){
				if (max < score[i + 1])
					max = score[i + 1];
			}
			printf("%d\n", max);
		}
		if (C == 'U')
			score[A - 1] = B;
	}

	free(score);

	return 0;
}
Falleyes 2015-09-04
  • 打赏
  • 举报
回复
引用 9 楼 lonely1206 的回复:
我的代码,我用vim测试了下,一组数据情况下,输入数据不多是成功的,输入的数据多了,就发生段错误,不懂。。。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int N, M, A, B;
char C;
int main(void)
{
    
    int score[N];
    int i, max;
    
    
    scanf("%d%d", &N, &M); 
	
    for (i = 0; i < N; i++)
    {
        scanf("%d", &score[i]);
    }
  
    
    while (M--)
    {
    	scanf(" %c%d%d", &C, &A, &B);
    	
        if (C == 'Q')
    	{
        	max = score[A-1];
        	
        	for (i = A; i < B; i++)
        	{
       	    	if (score[i-1] < score[i])
            	{
            	max = score[i];
        		
            	}
        	}
        	printf("%d\n", max);
    	}
    	if (C == 'U')
    	{
        	score[A-1] = B;
    	}
    }
    return 0;
}
你没有对N进行赋值,然后就声明int score[N],这个数组大小的N,是依据环境决定的。之后你读取值赋值给N,但是score数组的大小没有改变啊。
赤练仙子_ 2015-09-04
  • 打赏
  • 举报
回复
我的程序提交运行的结果是: 答案错误:您提交的程序没有通过所有的测试用例 测试用例: 9 10 28 49 11 35 40 17 57 4 6 Q 9 9 U 9 79 Q 9 5 Q 4 8 U 2 27 U 8 40 U 4 77 U 7 71 U 4 44 U 8 51 对应输出应该为: 6 79 57
赤练仙子_ 2015-09-04
  • 打赏
  • 举报
回复
我的代码,我用vim测试了下,一组数据情况下,输入数据不多是成功的,输入的数据多了,就发生段错误,不懂。。。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int N, M, A, B;
char C;
int main(void)
{
    
    int score[N];
    int i, max;
    
    
    scanf("%d%d", &N, &M); 
	
    for (i = 0; i < N; i++)
    {
        scanf("%d", &score[i]);
    }
  
    
    while (M--)
    {
    	scanf(" %c%d%d", &C, &A, &B);
    	
        if (C == 'Q')
    	{
        	max = score[A-1];
        	
        	for (i = A; i < B; i++)
        	{
       	    	if (score[i-1] < score[i])
            	{
            	max = score[i];
        		
            	}
        	}
        	printf("%d\n", max);
    	}
    	if (C == 'U')
    	{
        	score[A-1] = B;
    	}
    }
    return 0;
}
赤练仙子_ 2015-09-04
  • 打赏
  • 举报
回复
引用 7 楼 xiaoxuhao 的回复:
你的代码里有多处错误,主要是: 1、int score[N];这样定义是不行的,编译器不知道怎么给score分配内存; 2、scanf("%c%d%d", &C, &A, &B);这一句存在吃回车问题,需要规避一下; 3、缺少用M来构造的外层for循环; 4、if (C == 'Q')分支中,数组下标和比较逻辑都有问题; 我把这些问题改了一下,在vs2010下调好了,请参考:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main(void)
{
    int N, M, A, B;
    int score[10];
    int i,j,max;
    unsigned char C[2];
     
    scanf("%d%d", &N, &M); 
 
    for (i = 0; i < N; i++)
    {
        scanf("%d", &score[i]);
    }

	for(j = 0; j < M; j++)
	{ 
	    scanf("%1s%d%d",&C, &A, &B);
	  
	    if (C[0] == 'Q')
	    {
	        max = score[A-1];
	        for (i = A; i <= (B-1); i++)
	        {
	               if (score[i] > max)
	            {
	                max = score[i];
	            }
	        }
	        printf("%d\n", max);
	    }
	    if (C[0] == 'U')
	    {
	        score[A-1] = B;
	    }

		printf("scores:");
        for (i = 0; i < N; i++)
	    {
	        printf("%d ", score[i]);
	    }   
		printf("\n");

	}

    return 0;
}
谢谢,后来我也改了一下,但还是不行,你这个也还是通不过。。。
lizi_stdio 2015-09-04
  • 打赏
  • 举报
回复
引用 12 楼 Falleyes的回复:
你的代码水平,慢慢提高吧,多看看基础的东西。话说你是哪个省的,华为校招已经开始了啊?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

int main(void){
	int N, M, A, B;
	int* score;
	int i, max;
	char C;

	scanf("%d%d", &N, &M);

	score = (int*)malloc(sizeof(int)*N);

	for (i = 0; i < N; i++)
		scanf("%d", &score[i]);

	while (M--){
		fflush(stdin);
		scanf("%c%d%d", &C, &A, &B);
		if (C == 'Q'){
			max = score[A - 1];
			for (i = A - 1; i < B; i++){
				if (max < score[i + 1])
					max = score[i + 1];
			}
			printf("%d\n", max);
		}
		if (C == 'U')
			score[A - 1] = B;
	}

	free(score);

	return 0;
}
你这个只有1组数据。这题怎么提交?
xiaoxuhao 2015-09-04
  • 打赏
  • 举报
回复
你的代码里有多处错误,主要是: 1、int score[N];这样定义是不行的,编译器不知道怎么给score分配内存; 2、scanf("%c%d%d", &C, &A, &B);这一句存在吃回车问题,需要规避一下; 3、缺少用M来构造的外层for循环; 4、if (C == 'Q')分支中,数组下标和比较逻辑都有问题; 我把这些问题改了一下,在vs2010下调好了,请参考:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main(void)
{
    int N, M, A, B;
    int score[10];
    int i,j,max;
    unsigned char C[2];
     
    scanf("%d%d", &N, &M); 
 
    for (i = 0; i < N; i++)
    {
        scanf("%d", &score[i]);
    }

	for(j = 0; j < M; j++)
	{ 
	    scanf("%1s%d%d",&C, &A, &B);
	  
	    if (C[0] == 'Q')
	    {
	        max = score[A-1];
	        for (i = A; i <= (B-1); i++)
	        {
	               if (score[i] > max)
	            {
	                max = score[i];
	            }
	        }
	        printf("%d\n", max);
	    }
	    if (C[0] == 'U')
	    {
	        score[A-1] = B;
	    }

		printf("scores:");
        for (i = 0; i < N; i++)
	    {
	        printf("%d ", score[i]);
	    }   
		printf("\n");

	}

    return 0;
}
lizi_stdio 2015-09-04
  • 打赏
  • 举报
回复
楼主,请问在哪儿提交,能不能给我提交地址?我想试试。 或者楼主帮我跑一下3L的程序吧,应该可以过的。
赤练仙子_ 2015-09-04
  • 打赏
  • 举报
回复
引用 2 楼 zxh707wk 的回复:
N没初始化。。。
N是用户输入赋值的
赤练仙子_ 2015-09-04
  • 打赏
  • 举报
回复
引用 1 楼 Falleyes 的回复:
他给你的例子,U 3 6将第3个学生的成绩改为第6个的成绩,但是他只有5个学生啊,你如果不判断越界的话,肯定会出错的。。。
U 3 6是把第三个学生的成绩改为6,不是改为第6个的成绩
lizi_stdio 2015-09-03
  • 打赏
  • 举报
回复
首先多组数据,你这个只能测试1组;而且你的操作明显只能操作一回,肯定错了。我的代码仍有错误,不知为啥
#include <stdio.h>
int scr[30000];
int main(void){
	int N,M;
	char C;
	int i,j,k,a,b;
	while(~scanf("%d%d",&N,&M)){
		for(i=0;i<N;i++){
			scanf("%d",&scr[i]);
		}
		for(i=0;i<M;i++){
			scanf("%c%d%d",&C,&a,&b);
			if(C=='Q'){
				k=a-1;
				for(j=k+1;j<b;j++){
					if(scr[k]<scr[j]){ k=j;}
				}
				printf("%d\n",scr[k]);
			}
			else scr[a-1]=b;
		}
	}
	return 0;

}
autocccc 2015-09-03
  • 打赏
  • 举报
回复
输入成绩是时将学生1-N的成绩存到score数组中的1-N而不是1-N-1 huozhe, for 循环中 A B 都减1 不然最后一个学生N的成绩score[N]会越界,或者说低底第N 个学生的成绩是score[N-1]
707wk 2015-09-03
  • 打赏
  • 举报
回复
N没初始化。。。
Falleyes 2015-09-03
  • 打赏
  • 举报
回复
他给你的例子,U 3 6将第3个学生的成绩改为第6个的成绩,但是他只有5个学生啊,你如果不判断越界的话,肯定会出错的。。。

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧