在很多的算法题目中经常会遇见设置一个边界状态,如正无穷,负无穷等。在此,也总结了一些方法和技巧供大家参考
1:直接利用计算机的编码特点:
#include
int main(){
printf("INT_MAX = %d\n",unsigned(-1)>>1); //编译器默认-1为int型,所以在转换为unsigned型后并右移一位中,最高位也要随之移动。
printf("-INT_MAX = %d\n",-(unsigned(-1)>>1)-1); //由于计算机采用的补码方式存储,所以对于int型的最小值则为-2^31,也即 -INT_MAX-1。
printf("UNSIGNED_INT_MAX = %u\n",unsigned(-1)); //由于-1的补码的二进制位全为1。所以unsigned(-1)的编码为全1状态的无符号的值。也即无符号的最大值。
return 0;
}
2:利用编译器自带的头文件
/*
C语言中的整数类型
int 是 C 语言的基本整数类型,可以满足我们处理一般数据的需求。
C 语言还提供了四个可以修饰 int 的关键字:short、long、signed,以及 unsigned。
利用这四个关键字,C 语言标准定义了以下整数类型:
1) short int(可简写为 short),和 int 一样,也是有符号整数
2) long int(简写:long),有符号整数
3) long long int(简写:long long),C99标准添加的类型,有符号整数
4) unsigned int(简写:unsigned),无符号整数,不能表示负数
5) unsigned long int(简写:unsigned long),无符号整数,不能表示负数
6) unsigned short int(简写:unsigned short),无符号整数,不能表示负数
7) unsigned long long int(简写:unsigned long long),C99添加的类型,无符号整数
8) 所有没有标明 unsigned 的整数类型默认都是有符号整数。
在这些整数类型前面加上 signed 可以使读者更清楚地知道
这些是有符号整数,尽管有没有 signed 都表示有符号整数。
例如:signed int 等同于 int 。
注意:C语言只规定short <= int <=long int。具体还得看具体的编译器,long int型并不能肯定就是64位的,很多时候long int和int表示的范围是一致的。
一般我们把 short 称为短整型,把 long 称为长整型,把 long long 称为超长整型,把 int 称为整型。unsigned 打头的那些整数类型统称为无符号整型。
例如:我们称 unsigned short 为无符号短整型。以此类推。
*/
#include
#include
int main(){
printf("INT_MAX = %d(2*10^10)\n",INT_MAX); //括号内为大约的级数
printf("INT_MIN = %d(-2*10^10)\n",INT_MIN);
printf("UINT_MAX = %u(4*10^10)\n",UINT_MAX);
printf("LONG_LONG_MAX = %lld(9*10^19)\n",LLONG_MAX);
printf("LONG_LONG_MIN = %lld(-9*10^19)\n",LLONG_MIN);
printf("UNSIGNED_LONG_LONG_MAX = %llu(1*10^20)\n",ULONG_LONG_MAX);
return 0;
}
3:扩展:GCC编译器中limits头文件:
/**
* @file limits.h
* Copyright 2012, 2013 MinGW.org project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef _LIMITS_H
#define _LIMITS_H
#pragma GCC system_header
#include <_mingw.h>
/*
* Functions for manipulating paths and directories (included from io.h)
* plus functions for setting the current drive.
*
* Defines constants for the sizes of integral types.
*
* NOTE: GCC should supply a version of this header and it should be safe to
* use that version instead of this one (maybe safer).
*/
/*
* File system limits
*
* TODO: NAME_MAX and OPEN_MAX are file system limits or not? Are they the
* same as FILENAME_MAX and FOPEN_MAX from stdio.h?
* NOTE: PATH_MAX is the POSIX equivalent for Microsoft's MAX_PATH; the two
* are semantically identical, with a limit of 259 characters for the
* path name, plus one for a terminating NUL, for a total of 260.
*/
# define PATH_MAX 260
/*
* Characteristics of the char data type.
*
* TODO: Is MB_LEN_MAX correct?
*/
#define CHAR_BIT 8
#define MB_LEN_MAX 2
#define SCHAR_MIN (-128)
#define SCHAR_MAX 127
#define UCHAR_MAX 255
#ifndef _CHAR_UNSIGNED
#define CHAR_MIN SCHAR_MIN
#define CHAR_MAX SCHAR_MAX
#else
#define CHAR_MIN 0
#define CHAR_MAX UCHAR_MAX
#endif
/*
* Maximum and minimum values for ints.
*/
#define INT_MAX 2147483647
#define INT_MIN (-INT_MAX-1)
#define UINT_MAX 0xffffffff
/*
* Maximum and minimum values for shorts.
*/
#define SHRT_MAX 32767
#define SHRT_MIN (-SHRT_MAX-1)
#define USHRT_MAX 0xffff
/*
* Maximum and minimum values for longs and unsigned longs.
*/
#define LONG_MAX 2147483647L
#define LONG_MIN (-LONG_MAX-1)
#define ULONG_MAX 0xffffffffUL
#define SSIZE_MAX LONG_MAX
#define LLONG_MAX 9223372036854775807LL
#define LLONG_MIN (-LLONG_MAX - 1)
#define ULLONG_MAX (2ULL * LLONG_MAX + 1)
#define LONG_LONG_MAX 9223372036854775807LL
#define LONG_LONG_MIN (-LONG_LONG_MAX-1)
#define ULONG_LONG_MAX (2ULL * LONG_LONG_MAX + 1)
/* MSVC compatibility */
#define _I64_MIN LONG_LONG_MIN
#define _I64_MAX LONG_LONG_MAX
#define _UI64_MAX ULONG_LONG_MAX
#endif /* not _LIMITS_H */