运行时错误-非零异常

run time error - non zero exception

本文关键字:异常 运行时错误      更新时间:2023-10-16

我的编程老师让我用c写这个问题:给定由N个整数A和一个数字k组成的数组,在一个回合中,选择所有Ai的最大值,我们称之为MAX。那么Ai =MAX - Ai对应每个1 <= i <= N。帮助Roman找出K转弯后数组的样子。

输入

数字N和K在输入的第一行给出。然后在第二行给出N个整数,表示数组a。

输出

单行输出N个数字。它应该是K转后的数组A。

约束
* 1 <= N <= 10^5
* 0 <= K <= 10^9
* Ai does not exceed 2 * 10^9 by it's absolute value.

例子
Input:
4 1
5 -1 7 0
Output:
2 8 0 7

,我的代码是:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

long int Max(long int *arr, int low, int high)
{
long int max,i;
max = arr[low];
for(i=0;i<=high;i++)
{
    if(max<=arr[i])
      max = arr[i];
}
  return max;
}
 /* Driver program to test above function */
 int main()
 {
    long int max,*arr;
    long int n,k,c1,c2,c3,i,j;
    c1 = (long int)pow(10,5);
    c2 = (long int)pow(10,9);
    c3 = 2*c2;
    scanf("%ld %ld",&n,&k);
    if(n<1||n>c1)
      exit(1);
    else if(k<0||k>c2)
      exit(1);
    else
    {
    arr = (long int *)malloc(sizeof(int)*n);
    for(i=0;i<n;i++)
      {
        scanf("%ld",&arr[i]);
        if(abs(arr[i])>c3)
          exit(1);
      }
    if(k%2 == 0)
      {
        for(i=0;i<2;i++)
        {
            max = Max(arr, 0, n-1);
            for(j=0;j<n;j++)
            {   
                arr[j] = max-arr[j];
                if(abs(arr[j])>c3)
                            exit(1);
            }
        }
      }
    else if(k%2 != 0)
    {
        max = Max(arr, 0, n-1);
                    for(j=0;j<n;j++)
                       {       
                        arr[j] = max-arr[j];
                            /*if(abs(arr[j])>c3)
                              exit(1);*/
                       }
    }
    /*  for(m=0;m<n;m++)
                printf("%ld ",arr[m]);
        printf("n");*/
    for(i=0;i<n;i++)
      printf("%ld ",arr[i]);
    printf("n");
   }  
   return 0;
 }

我在ubuntugcc编译器上执行了这段代码,它在满足所有约束的情况下完美地工作,但是当我将这段代码上传到我老师的门户网站上时,它有一个编译器并执行了代码,它说Runtime错误-

nzec which means a non-zero exception which is used to signify that main() does not have "return 0;" statement or exception thrown by c++ compiler .

请,谁能帮助我什么是错误的在我的代码,因为有一个返回0;语句在我的代码中。请帮助。

每个人都指出出口的多种用途…我可以用其他方法代替exit()来减少它们吗?

我的猜测是,这与您的各种exit(1)语句的错误条件有关。

Dave Costa指出,exit(1)可能是原因

另一个可能的问题是分配的数组的大小:

arr = (long int *)malloc(sizeof(int)*n);
应:

arr = malloc(sizeof(long int)*n);

请注意,您不需要使用pow常量:

c1 = (long int)pow(10,5);
c2 = (long int)pow(10,9);

可以替换为:

c1 = 1e5L;
c2 = 1e9L;