求绝对值最大值 1147

//附上地址https://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/1147.html
//这道题很简单 最初做的时候 在OJ上提交 怎么弄都是 Runtime Error  
//后来开始多次检查调试,发现是下面代码中b[i]>=max 这里没有取等号
//附上 Runtime Error  的其他可能情况:
//Runtime Error:  运行时错误,这个一般是程序在运行期间执行了非法的操作造成的。以下列出常见的错误类型:

//Runtime Error(ARRAY_BOUNDS_EXCEEDED) //数组越界

//Runtime Error(DIVIDE_BY_ZERO) //除零

//Runtime Error(ACCESS_VIOLATION) //非法内存访问

//Runtime Error(STACK_OVERFLOW) //堆栈溢出

#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<cmath>
using namespace std;
int main()
{ 
	int n, i;
	int a[1000], b[1000];
	scanf("%d", &n);
	for (i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
		b[i] = abs(a[i]);
	}
	int max,temp;
	max = b[0];
	for (i = 0; i < n; i++)
	{
		if (b[i]>=max)
		{
			max = b[i];
			temp = i;
		}
	}
	cout << a[temp] << endl;
   	return 0;
}


猜你喜欢

转载自blog.csdn.net/cjava__/article/details/80780186
今日推荐