运行时错误-非零异常
run time error - non zero exception
我的编程老师让我用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;
}
我在ubuntu
的gcc
编译器上执行了这段代码,它在满足所有约束的情况下完美地工作,但是当我将这段代码上传到我老师的门户网站上时,它有一个编译器并执行了代码,它说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;
相关文章:
- 删除指向指针的指针是运行时错误吗
- c++中的指针和运行时错误
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 对单向链表进行排序时出现运行时错误
- 为什么此代码存在运行时错误?
- 你能解释一下什么运行时错误是如何解决它的吗?
- 为什么会出现 gettnig 运行时错误:加载类型为"_Bit_type"(stl_bvector.h) 的空指针?
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 运行时错误:引用绑定到类型为"int"的空指针
- 为什么当 vector 为空时会显示运行时错误?
- C++运行时错误与快速排序算法抛出堆栈转储错误
- 运行时错误:矢量下标超出范围:正在检查空集
- 分配给gslice_array会导致运行时错误
- cout 新创建的对象引发运行时错误
- 寻求与类型为 std::invalid_argument 的未捕获异常相关的运行时错误的建议: stoi:无转换
- GDB算术异常 - 无零分割 - 运行时错误
- 使用Throw和Catch的未处理异常运行时错误
- 运行时错误-非零异常
- 符号可见性、异常、运行时错误