1065. A+B and C (64bit) (20)

本文详细介绍了C/C++中64位整型(long long、__int64)的数值范围,并给出了一种安全的数值比较方法,避免了溢出错误。同时对比了不同数据类型间的差异。

我写的时候其实有测试点没同过,参考别人的,呜呜。

这里重要的是long long数据类型范围,这里我在网上搜索了下其他的各种类型的范围:

int  、long 、long long数据类型范围

unsigned   int   0~4294967295   
int   2147483648~2147483647 
unsigned long 0~4294967295
long   2147483648~2147483647
long long的最大值:9223372036854775807
long long的最小值:-9223372036854775808
unsigned long long的最大值:18446744073709551615

__int64的最大值:9223372036854775807
__int64的最小值:-9223372036854775808
unsigned __int64的最大值:18446744073709551615

#include<iostream>
#include<climits>
using namespace std;
bool cmp(const long long &a,const long long &b,const long long &c)
{
	if(a>0&&b>0)
	{
		if(a>LLONG_MAX-b)return true;
	}
	if(a<0&&b<0)
	{
		if(a<LLONG_MIN-b)return false;//好奇怪,为什么这里不能写成a+b>LLONG_MIN.仔细想想 a+b的范围可能无法表示,
	}                                   //所以要写成这样
	return a+b>c;
}
int main()
{
	long long a,b,c;
	int t;
	int count=1;
	cin>>t;
	while(t--)
	{
		cin>>a>>b>>c;
		cout<<"Case #"<<count<<": ";
		if(cmp(a,b,c))cout<<"true"<<endl;
		else cout<<"false"<<endl;
		count++;
	}
	system("pause");
	return 0;
}

这里用到了头文件climits ,其中定义了类型的数值的最大值和最小值

通过这里需要了解一下__

__int64 与long long 的区别

在C/C++中,64为整型一直是一种没有确定规范的数据类型。现今主流的编译器中,对64为整型的支持也是标准不一,形态各异。一般来说,64位整型的定义方式有long long和__int64两种(VC还支持_int64),而输出到标准输出方式有printf(“%lld”,a),printf(“%I64d”,a),和cout << a三种方式。

# # There is insufficient memory for the Java Runtime Environment to continue. # Native memory allocation (malloc) failed to allocate 952528 bytes. Error detail: Chunk::new # Possible reasons: # The system is out of physical RAM or swap space # This process is running with CompressedOops enabled, and the Java Heap may be blocking the growth of the native heap # Possible solutions: # Reduce memory load on the system # Increase physical memory or swap space # Check if swap backing store is full # Decrease Java heap size (-Xmx/-Xms) # Decrease number of Java threads # Decrease Java thread stack sizes (-Xss) # Set larger code cache with -XX:ReservedCodeCacheSize= # JVM is running with Unscaled Compressed Oops mode in which the Java heap is # placed in the first 4GB address space. The Java Heap base address is the # maximum limit for the native heap growth. Please use -XX:HeapBaseMinAddress # to set the Java Heap base and to place the Java Heap above 4GB virtual address. # This output file may be truncated or incomplete. # # Out of Memory Error (arena.cpp:168), pid=97348, tid=7672 # # JRE version: OpenJDK Runtime Environment JBR-21.0.5+8-631.30-jcef (21.0.5+8) (build 21.0.5+8-b631.30) # Java VM: OpenJDK 64-Bit Server VM JBR-21.0.5+8-631.30-jcef (21.0.5+8-b631.30, mixed mode, sharing, tiered, compressed oops, compressed class ptrs, g1 gc, windows-amd64) # No core dump will be written. Minidumps are not enabled by default on client versions of Windows # --------------- S U M M A R Y ------------ Command Line: abort vfprintf -XX:ErrorFile=C:\Users\h60095350\java_error_in_pycharm_%p.log -XX:HeapDumpPath=C:\Users\h60095350\java_error_in_pycharm.hprof -Xms256m -Xmx2048m -XX:ReservedCodeCacheSize=512m -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -XX:CICompilerCount=2 -XX:+IgnoreUnrecognizedVMOptions -ea -Dsun.io.useCanonCaches=false -Dsun.java2d.metal=true -Djbr.catch.SIGABRT=true -Djdk.http.auth.tunneling.disabledSchemes="" -Djdk.attach.allowAttachSelf=true -Djdk.module.illegalAccess.silent=true -Djdk.nio.maxCachedBufferSize=2097152 -Djava.util.zip.use.nio.for.zip.file.access=true -Dkotlinx.coroutines.debug=off -XX:+UnlockDiagnosticVMOptions -XX:TieredOldPercentage=100000 -Dwelcome.screen.defaultWidth=1000 -Dwelcome.screen.defaultHeight=720 -Dwsl.use.remote.agent.for.nio.filesystem=true -Djava.nio.file.spi.DefaultFileSystemProvider=com.intellij.platform.core.nio.fs.MultiRoutingFileSystemProvider -Djava.security.manager=com.intellij.platform.core.nio.fs.CoreBootstrapSecurityManager -Didea.cycle.buffer.size=4096 -Djb.vmOptionsFile=C:\Users\h60095350\AppData\Roaming\JetBrains\PyCharm2024.3\pycharm64.exe.vmoptions -Djava.system.class.loader=com.intellij.util.lang.PathClassLoader -Didea.vendor.name=JetBrains -Didea.paths.selector=PyCharm2024.3 -Djna.boot.library.path=D:\JetBrains\PyCharm 2024.3.2/lib/jna/amd64 -Dpty4j.preferred.native.folder=D:\JetBrains\PyCharm 2024.3.2/lib/pty4j -Djna.nosys=true -Djna.noclasspath=true -Dintellij.platform.runtime.repository.path=D:\JetBrains\PyCharm 2024.3.2/modules/module-descriptors.jar -Didea.platform.prefix=Python -Dsplash=true -Daether.connector.resumeDownloads=false -Dcompose.swing.render.on.graphics=true --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.ref=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.nio=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.text=ALL-UNNAMED --add-opens=java.base/java.time=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.locks=ALL-UNNAMED --add-opens=java.base/jdk.internal.vm=ALL-UNNAMED --add-opens=java.base/sun.net.dns=ALL-UNNAMED --add-opens=java.base/sun.nio.ch=ALL-UNNAMED --add-opens=java.base/sun.nio.fs=ALL-UNNAMED --add-opens=java.base/sun.security.ssl=ALL-UNNAMED --add-opens=java.base/sun.security.util=ALL-UNNAMED --add-opens=java.desktop/com.sun.java.swing=ALL-UNNAMED --add-opens=java.desktop/java.awt=ALL-UNNAMED --add-opens=java.desktop/java.awt.dnd.peer=ALL-UNNAMED --add-opens=java.desktop/java.awt.event=ALL-UNNAMED --add-opens=java.desktop/java.awt.font=ALL-UNNAMED --add-opens=java.desktop/java.awt.image=ALL-UNNAMED --add-opens=java.desktop/java.awt.peer=ALL-UNNAMED --add-opens=java.desktop/javax.swing=ALL-UNNAMED --add-opens=java.desktop/javax.swing.plaf.basic=ALL-UNNAMED --add-opens=java.desktop/javax.swing.text=ALL-UNNAMED --add-opens=java.desktop/javax.swing.text.html=ALL-UNNAMED --add-opens=java.desktop/sun.awt=ALL-UNNAMED --add-opens=java.desktop/sun.awt.datatransfer=ALL-UNNAMED --add-opens=java.desktop/sun.awt.image=ALL-UNNAMED --add-opens=java.desktop/sun.awt.windows=ALL-UNNAMED --add-opens=java.desktop/sun.font=ALL-UNNAMED --add-opens=java.desktop/sun.java2d=ALL-UNNAMED --add-opens=java.desktop/sun.swing=ALL-UNNAMED --add-opens=java.management/sun.management=ALL-UNNAMED --add-opens=jdk.attach/sun.tools.attach=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-opens=jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED --add-opens=jdk.jdi/com.sun.tools.jdi=ALL-UNNAMED -Dide.native.launcher=true -Djcef.sandbox.ptr=0000015B99EFEDE0 -Djcef.sandbox.cefVersion=122.1.9+gd14e051+chromium-122.0.6261.94 com.intellij.idea.Main Host: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz, 12 cores, 31G, Windows 10 , 64 bit Build 19041 (10.0.19041.4717) Time: Wed Aug 20 10:54:06 2025 Windows 10 , 64 bit Build 19041 (10.0.19041.4717) elapsed time: 64.235615 seconds (0d 0h 1m 4s) --------------- T H R E A D --------------- Current thread (0x0000015bc5abdd10): JavaThread "C2 CompilerThread0" daemon [_thread_in_native, id=7672, stack(0x000000093ba00000,0x000000093bb00000) (1024K)] Current CompileTask: C2:64235 41919 4 com.intellij.openapi.editor.impl.view.IterationState::advance (339 bytes) Stack: [0x000000093ba00000,0x000000093bb00000] Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code) V [jvm.dll+0x6e52b9] V [jvm.dll+0x8c3633] V [jvm.dll+0x8c5b8e] V [jvm.dll+0x8c6273] V [jvm.dll+0x288f46] V [jvm.dll+0xc66dd] V [jvm.dll+0xc6c13] V [jvm.dll+0x3c456b] V [jvm.dll+0x3901c7] V [jvm.dll+0x38f63a] V [jvm.dll+0x2507a2] V [jvm.dll+0x24fd71] V [jvm.dll+0x1cd6a4] V [jvm.dll+0x25f5dc] V [jvm.dll+0x25db26] V [jvm.dll+0x3ff5e6] V [jvm.dll+0x86b248] V [jvm.dll+0x6e3abd] C [ucrtbase.dll+0x21bb2] C [KERNEL32.DLL+0x17374] C [ntdll.dll+0x4cc91] com.intellij.diagnostic.JBRCrash
最新发布
08-21
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值