ACM模拟题讲解(1)-高精度

Java中提供了byte、short、int和long表示整数,float和double来表示浮点数,每种类型都有一定的表示范围,当超过了这个范围之后就不能处理了。为了提供对非常大的整数和浮点数的处理,Java提供了BigDecimal和BigInteger。下面的代码演示了BigDecimal和BigInteger的基本用法:

BigDecimal data1 = new BigDecimal("23232123456789.123456789");

BigDecimal data2 = new BigDecimal("23423423123456789.123456789");

BigDecimal data3 = data1.add(data2);

System.out.println(data3.toString());

BigInteger iData1 = new BigInteger("123123123456456789789123456789");

BigInteger iData2 = new BigInteger("12312312323232456456789789123456789");

BigInteger iData3 = iData1.add(iData2);

System.out.println(iData3.toString());

如果不使用这些类库如何实现大整数和大浮点数的计算呢?下面通过ACM模拟题介绍。

1、Exponentiation(不想看英文,可以直接跳到后面的中文部分)

Description

Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.
This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

Input

The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.

Output

The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer.

Sample Input

95.123 12

0.4321 20

5.1234 15

6.7592 9

98.999 10

1.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721

.00000005148554641076956121994511276767154838481760200726351203835429763013462401

43992025569.928573701266488041146654993318703707511666295476720493953024

29448126.764121021618164430206909037173276672

90429072743629540498.107596019456651774561044010001

1.126825030131969720661201

翻译:题目的含义是计算R(0.0 < R < 99.999)的n(0 < n <= 25)次方。

题目解析:

这个题目的关键是如何表示大的整数或者浮点数。针对本题目,计算n次方。可以单独处理小数的位置,如果R中的包含x位小数,则进行运算后的结果应该包括x*n位小数。可以采用数组表示R,然后进行运算即可。下面的方法供参考。

public static String exponentiation(String data,int n){

// 找小数点

int index = data.indexOf(".");

// 去掉小数点

String temp = data.replace(".","");

// 用数组表示输入的数据

byte[] inputData=new byte[temp.length()];

// 表示计算结果

byte[] result = new byte[160];

// 表示进位

byte pre=0;

// 初始化

for(int i=0;i<inputData.length;i++){

result[i] = Byte.parseByte(temp.substring(temp.length()-i-1,temp.length()-i));

inputData[i] = result[i];

}

// 计算n-1遍

for(int i=0;i<n-1;i++){

// 表示上一次的计算结果

byte[] tempResult = Arrays.copyOf(result,result.length);

// 对result初始化

Arrays.fill(result,0,result.length,(byte)0);

// 用上一次的计算结果与输入的数据相乘

for(int k=0;k<inputData.length;k++){

for(int j=0;j<150;j++){

// 两位相乘

byte tempMul = (byte)(tempResult[j]*inputData[k]+result[j+k]);

// 处理个位

result[j+k]=(byte)(tempMul%10);

// 处理进位

pre=(byte)(tempMul/10);

// 表示进到第几位

int pp=1;

// 处理进位

while(pre>0){

tempMul=(byte)(pre+result[j+k+pp]);

result[j+k+pp]=(byte)(tempMul%10);

pre=(byte)(tempMul/10);

pp++;

}

}

}

}

// 计算小数位

int digits;

if(index==-1)

digits = 0 ;

else

digits = n*(data.substring(index+1).length());

StringBuffer stringResult = new StringBuffer();

if(index==-1){ // 没有小数点

boolean b=false;

for(int i=result.length-1;i>=0;i--){

if(!b){ //前面都是0

if(result[i]==0)

continue;

else

b=true;

}

stringResult.append(result[i]);

}

}else{ // 有小数点

boolean b=false;

// 处理小数点之前

for(int i=result.length-1;i>=digits;i--){

if(!b){ //前面都是0

if(result[i]==0)

continue;

else

b=true;

}

stringResult.append(result[i]);

}

stringResult.append(".");

for(int i=digits-1;i>=0;i--){

stringResult.append(result[i]);

}

}

return stringResult.toString();

}

2、A + B Problem

Problem Description

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.

Output

For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.

Sample Input

2

1 2

112233445566778899 998877665544332211

Sample Output

Case 1:

1 + 2 = 3

Case 2:

112233445566778899 + 998877665544332211 = 1111111111111111110

翻译:题目要求就算两个大的整数的和。

题目解析:两个整数分别使用数组表示,然后每一位相加,并对进位处理即可。上一题的内容已经包含这个过程。这里不再给出参考代码。

手机相机Tuning专项调优(一):基础流程与图像主观评价标准建立实战解析 本篇为手机相机系统“Tuning专项调优”系列的开篇,深入剖析了当前手机相机Tuning的基本流程与图像主观评价标准的构建方法。结合实际工程经验与最新业界案例,详细介绍ISP调优和3A控制中涉及的主观评价标准,如锐度、色彩饱和度、噪声感知等,并提出一套可工程化落地的标准制定与评测实践体系,帮助工程师有效提升图像品质。 阅读详情

相关推荐

白话详细解读(七)----- CBAM:Convolutional Block Attention Module

一、CBAM简介 论文链接:https://x.sci-hub.org.cn/scholar?hl=zh-TW&as_sdt=0%2C5&q=CBAM%3A+Convolutional+Block+Attention+Module&btnG=&oq=CB CBAM这篇论文是在2018年提出的,自提出之后,已经被引用上千次,可见其影响力。CBAM是一种专门为了卷积神经网络而设计的注意力模块,传统的卷积存在只关注局部信息,而往往忽略全局信息的弊端,以3*3卷积为例,滤波器有9个

奋斗の博客 1万+

sql bug之repalce:Argument data type ntext is invalid

sql bug之repalce:Argument data type ntext is invalid

m0_47448095的博客 1311

ACS驱动器直线电机回零程序与误差补偿Buffer实战解析

本文深入解析了ACS驱动器控制直线电机时,实现高精度回零与误差补偿的完整实战流程。核心在于利用驱动器的D-Buffer功能,将激光干涉仪测量的误差数据导入补偿数组,通过ERRORMAP1D指令进行映射,从而实时修正定位误差,显著提升设备重复定位精度。文章详细拆解了回零程序逻辑、补偿参数设置及关键调试避坑指南。

650

POJ1001 Exponentiation【高精度乘方】

题目: Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for

淡然坊 1620

基于奥比中光Orbbec Astra Pro与ROS实现实时3D环境建模:orb_slam2实战指南

本文提供了一份详细的实战指南,介绍如何使用奥比中光Orbbec Astra Pro深度摄像头与ROS系统,部署并运行ORB-SLAM2算法,实现实时3D环境建模与定位。内容涵盖从驱动安装、环境配置、算法编译到摄像头标定与性能调优的全流程,旨在帮助开发者快速搭建一套高性价比的视觉SLAM系统。

weixin_29054399的博客 337

【ceph】OSD-基本存储单元

OSD 是 Ceph 的基本存储单元,负责对象的存储、检索和数据副本的管理。每个 OSD 进程通常对应于存储集群中的一个物理或虚拟存储设备(如硬盘或 SSD)。OSD 是 Ceph 的关键组成部分,负责对象的存储、管理和复制。通过多个 OSD 共同协作,Ceph 能够提供高可用性、可扩展性和容错能力,适用于各种存储需求。

qq_42853133的博客 1999

2021-01-30ACM——高精指数函数

ACM题解——杂题Q 题目描述 Q - Exponentiation Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems. This problem req

Biangbangbing的博客 452

YOLOv11改进策略【损失函数篇】| Slide Loss,解决简单样本和困难样本之间的不平衡问题

本文记录的是改进YOLOv11的损失函数,将其替换成Slide Loss,并详细说明了优化原因,注意事项等。Slide Loss函数可以有效地解决样本不平衡问题,为困难样本赋予更高的权重,使模型在训练过程中更加关注困难样本。若是在自己的数据集中发现容易样本的数量非常大,而困难样本相对稀疏,可尝试使用Slide Loss来提高模型在处理复杂样本时的性能。

Limiiiing的博客 4569

(HDU 1063)Exponentiation(c++实现)

Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many compu

ROOM 1986

保姆级教程解决Edge Copilot该服务在你所在的地区不可用及Sign in to continue your chat.——电脑篇

相信很多人更新到Win11之后都注意到自带的浏览器右上角多了一个Copilot,这个副驾驶是微软与OpenAI合作,免费使用,包括插件功能。相当于你可以在电脑上免费的使用GPT4。很可惜,点开提示该服务在你所在的地区不可用。很多人第一时间想到科学的上网应该就可以解决了,尝试后又提示you’ve reached the conversation limit for today.Sign in to continue your chat.明明登录账号了为什么还是不能用?不是免费吗,为什么提示今天受限制。 其实上

weixin_44887342的博客 5万+

vue3 - 详细实现用户收货地址识别解析“姓名/手机号/省市区/街道/乡镇村庄“关键字,用户复制粘贴一段含有姓名电话/详细地址的文字文本信息,vue3自动识别拆分并填入到对应表单字段中,详细示例代码

vue3,nuxt3,快递地址读取,收货地址解析、快递地址解析、物流地址解析自动识别,收货地址解析,从发货地址中拆出姓名电话地址,根据粘贴的字符串提取姓名地址,vue3地址信息自动识别,ts,vue3智能识别地址,提取姓名电话地址信息,自动识别出省市区街道姓名手机号等,根据文本自动解析出收货信息,怎么识别文本中的手机号,粘贴板地址识别,地址智能识别,如何识别收货地址中的关键字,粘贴一段文字后,自动识别到姓名/手机号/收货地址,vue3仿淘宝地址智能识别解析实现,省市区街道智能解析,vue3收货地址,识别地址

✅ 精选推荐文章 5503

java在ACM中的应用

 ACM模拟题讲解(1)-高精度       Java中提供了byte、short、int和long表示整数,float和double来表示浮点数,每种类型都有一定的表示范围,当超过了这个范围之后就不能处理了。为了提供对非常大的整数和浮点数的处理,Java提供了BigDecimal和BigInteger。下面的代码演示了BigDecimal和BigInteger的基本用法:     

自由女神 2148

AI Agent开发第82课-手把手教你把paddleocr-vl做成mcp使其在DIFY里运行

本文详解如何将国产顶尖多模态 OCR 模型 PaddleOCR-VL 封装为标准 MCP(Model Calling Protocol)服务,并通过自研的 HTTP-based MCP Client 接入 Dify 1.10,实现 AI Agent 在对话中自动识别并调用 OCR 能力。该方案已在某头部保险公司真实业务流程中落地,使用了当前最先进的“Agentic AI 原生”设计思想。

打造全国最全的AI Agent开发知识领域的博客 1626

NEUQ-ACM预备队第一次双周赛

NEUQ-ACM预备队第一次双周赛

qq_46844003的博客 384

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

总结下吧...(2008-05-10 09:11)

直接把上交的总结扔上来了...------------------------------GDCPC2008总结分割线-----------------------------最后一场正式比赛结束了, 结果不如预想中的好. 不过走出赛场, 倒也没多少遗憾的感觉. 要说遗憾, 去年已经够遗憾了. 而且就自己来说, 这次比赛还算是发挥得不错. 先流水帐一下过程吧:拿到题目, XJ看A,B,

ecchi's blog 1942

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

POJ1001

Exponentiation: Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experien

u011715291的专栏 472

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

高精度问题之大数求幂

Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many

Aleks 1704

Acm入门1:模拟、枚举、递推、递归(第二篇博客)

一、模拟 模拟:顾名思义,写程序来模拟题目中所要求的操作,只要按照题目意思来写即可。 例1:回文串 输入一个字符串,判断这个字符串是不是回文串,如果是,输出Yes,否则输出No。 回文串是一个正读反读都一样的字符串。 比如level、noon,但是windows不是。 /*输入一个字符串,判断这个字符串是否是回文串*/ #include <bits/stdc++.h>//万能头文件 using namespace std;//命名空间 int main(int argc, char

qq_64246637的博客 593

PostgreSQL数据库中的常见错误

 我们在操作数据库的时候,我们总会遇到很多错误。下面整理了一下常见的错误。错误1      FATAL: connection limit exceeded for non-superusers      原因:非超级用户的连接数(max_connections - superuser_reserved_connections)超过了设定值      解决办法:增加max_co

步行者的专栏 11万+

mysql解析json字符串,字符串查找,子串截取和字段类型转换相关总结

mysql解析json字符串,字符串查找,子串截取和字段类型转换相关总结 很多时候,我们需要在sql里面直接解析json字符串。这里针对mysql5.7版本的分水岭进行区分。 1.对于mysql5.7以上版本     使用mysql的内置函数JSON_EXTRACT(column, '$.key'),这个函数有两个参数,第一个参数column代表json列的列名;第二个参数key代表json...

helloxiaozhe的博客 5万+

Argument data type text is invalid for argument 1 of replace function

update Table set content= replace(content,'222','') ERROR Argument data type text is invalid for argument 1 of replace function 更改为 updateTableset content=replace(cast(content as varcha...

weixin_30670965的博客 2128
上一篇: 如果有这样的机会,你应该抓住
下一篇: ACM模拟题详解(2)——简单数论
Java_Web_Book
博客等级 码龄18年 8粉丝 3原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值