天天看点

int最小负数的绝对值比最大正数绝对值大 1问题来源:问题抽象:解决办法

VS2019中C4146: 一元负运算符应用于无符号类型,结果仍为无符号类型

  • 问题来源:
  • 问题抽象:
  • 解决办法
    • 换成long没用一样报错
    • 换成"x < -2147483647"

问题来源:

#include <iostream>
#include <cmath>
using std::cin;
using std::cout;
int main()
{
	int m, n;
	cin >> m >> n;
	int** ptr = new int* [m];
	for (int i = 0; i < m; ++i)
		* (ptr + i) = new int[n];
	bool flag = false;
	int temp = 0;
	int max = 0;
	int ii = 0;
	int jj = 0;
	for (int i = 0; i < m; ++i) {
		for (int j = 0; j < n; ++j) {
			cin >> temp;
			if (temp == -2147483648) {//错误就在这里
				*(*(ptr + i) + j) = max = temp;
				ii = i;
				jj = j;
				flag = true;
				break;
			}
			else if (abs(temp) > abs(max)) {
				*(*(ptr + i) + j) = max = temp;
				ii = i;
				jj = j;
			}
		}
		if (flag)
			break;
	}
	cout << ++ii << " " << ++jj << " " << max;
	return 0;
}
           

问题抽象:

#include <iostream>
#include <cmath>
using std::cin;
using std::cout;
using std::endl;
int main()
{
	signed int x = 0;
	if(x == -2147483648)
		cout<<"asfd"<<endl;
	return 0;
}
           

解决办法

换成long没用一样报错

换成"x < -2147483647"

#include <iostream>
#include <cmath>
using std::cin;
using std::cout;
using std::endl;
int main()
{
	signed int x = 0;
	if(x < -2147483647)
		cout<<"asfd"<<endl;
	return 0;
}