编程计算int类型整数的最大值和最小值
方法一:将一个int类型整数不断加1,加到最大值,再加1,就变成负值(最小值)
最大值就是除最高位外,其余位都为1,-1即是所有位全部是1,右移1位后最高位变0
最小值即是最高位为1,其余位为0,所以最大值+1之后就变成了最小值
//#include <stdio.h>
#include <iostream>
//#include <limits.h> using namespace std; int main(){
int i = , min, max;
while(){
if(i+<=){
max = i;
min = i+;
break;
}
i++;
}
cout << "min is " << min <<endl;
cout << "max is " << max << endl;
getchar();
return ;
}
方法二:利用标准头文件limits.h,该文件包含一些很有用的常量,它们定义了各种类型所能容纳的值,其中int类型最大值最小值的预定义为
//#include <stdio.h>
#include <iostream>
#include <limits.h> using namespace std; int main(){
cout << "最小值是:" << INT_MIN << endl;
cout << "最大值是:" << INT_MAX << endl;
getchar();
return ;
}
方法三:C++模板numeric_limits(对模板不熟悉啊)可以参考:
http://blog.163.com/wujiaxing009@126/blog/static/7198839920124135147911/
代码:
#include <iostream>
#include <limits>
using namespace std; int main(){
cout<<(numeric_limits<int>::max)()<<endl;
cout<<(numeric_limits<int>::min)()<<endl;
cout<<(numeric_limits<size_t>::max)()<<endl;
cout<<(numeric_limits<size_t>::min)()<<endl;
cout<<(numeric_limits<float>::max)()<<endl;
cout<<(numeric_limits<float>::min)()<<endl;
cout<<(numeric_limits<double>::max)()<<endl;
cout<<(numeric_limits<double>::min)()<<endl;
getchar();
return ;
}
以上是在网上看到的三种方法,学习学习。
编程计算int类型整数的最大值和最小值的更多相关文章
- [转载] 不查资料确定int型整数的最大值和最小值
原文地址:http://blog.csdn.net/zhanghuoding/article/details/42719213 想法来自于书中的习题. Java的话直接输出 Integer.MAX_V ...
- 通过移位与或非运算获取整形最大值,最小值,以及获取输入的int类型整数的二进制表示
以上是最终效果 实现类: package com.corejava.chap02; public class IntBin { private int value; public IntBin(int ...
- C++-int类型整数超出范围后的处理
最近做了一道题目: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: ...
- mysql varchar类型转换int类型找出最大值
(1) 不严谨的,最简单的 select MAX(字段名 + 0) from 表名; (2) 使用函数实现 select MAX(cast(字段名 as SIGNED INTEGER)) from 表 ...
- 对int类型最小值INT_MIN取负值结果不变
在32位系统中,int类型的最大值是0x7fffffff(即除了最高的1Bit其他31位都为1),而最小值是0x80000000(除了最高1bit,其他31位都为0). 显然,对于最小值求负数是不存在 ...
- double类型转换为int类型四舍五入工具类
package com.qiyuan.util; import java.math.BigDecimal; import java.text.DecimalFormat; public class G ...
- C语言:通过返回指针的形式找出数组的最大值和最小值
// // main.c // Pointer_max_min(return) // // Created by ma c on 15/8/2. // Copyright (c) 2015年 ...
- int类型的整数转换成汉字
int类型的整数转换成汉字 一.源代码:IntegerNumberToChinese.java package cn.com.zfc.example; import java.util.Scanner ...
- C# 求余 int a = 371 / 100 % 10,求a的结果为多少?//nt 和int类型计算得到的结果还是int类型
//int 和int类型计算得到的结果还是int类型 eg:int a = 371 / 100 % 10,求a的结果为多少? 首先371除以100,再让此结果除以10求余数. 一 371除以100得到 ...
随机推荐
- Gym 100507D Zhenya moves from the dormitory (模拟)
Zhenya moves from the dormitory 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/D Descrip ...
- jquery easyui添加图标扩展
easyui中有很多通过iconCls="icon-reload"这样的属性引入小图标显示,当然我们也可以自己添加自己的小图标. 方式:1.我们可以在jquery easyui的文 ...
- code::blocks编译多文件 没有定义的引用
code::blocks是一款据说灰常强大的IDE,以前虽然也经常使用,但一没用过高度功能,二来没用它写过工程性的东西,简单点说就是一个以上的源文件并且加入其他非标准的头文件,今天想做一个多文件的语法 ...
- Hibernate映射解析——七种映射关系
首先我们了解一个名词ORM,全称是(Object Relational Mapping),即对象关系映射.ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现,这样开发人员就可以把对 ...
- wiki1169-传纸条(dp)
http://wikioi.com/problem/1169/ 四维数组和三维数组: #include<iostream> #include<cstdio> #include& ...
- 80端口被NT kernel & System 占用pid 4
前段时间停止了Apache,结果在打开的时候发现无法打开,80端口被占用,于是win+r 运行cmd 输入netstat -ano 可以看到80端口被PID4占用,于是打开任务管理器-进程-查看,选择 ...
- 无废话C#设计模式系列文章
不错的系列文章 原文:http://www.cnblogs.com/lovecherry/archive/2007/10/17/927728.html 本系列文章从公司内部的知识分享修改而来,有错误或 ...
- Codeforces Round #326 (Div. 2) D. Duff in Beach dp
D. Duff in Beach Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/588/probl ...
- Codeforces Gym 100610 Problem E. Explicit Formula 水题
Problem E. Explicit Formula Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- [Javascript] Either Functor
Either Functor: // API Right(val) // resolve the value Left(val) // return error message Examples: m ...