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

小学期acm训练第一发(简单数学题) 这一部分为刘汝佳的《算法竞赛入门经典》(第一版)第三章的数学杂题部分,知识点的确比较杂,但还是以模拟为主。 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=43038#overview 题目分析: A.让你对一个高精度的数求开方,直接开高精模板好像会T,所以使用了JAVA(JAVA自带快速幂,所以算乘方的时候回比较快),在 阅读详情

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

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

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

信息收集-服务器信息 服务器上面可以运行大量的系统服务和第三方应用服务,如果操作系统或者第三方软件没有及时升级打补丁,攻击者就有可能直接通过服务器上运行的服务进行攻击。 阅读详情

相关推荐

运行stable-diffusion出现的问题

解决:即用 https://github.com/CompVis/taming-transformers/blob/master/taming/modules/vqvae/quantize.py 新文件,替换错误提示文件中的全部内容即可。5.新建文件夹 CompVis/stable-diffusion-safety-checker/将下载的四个文件放入 解决!后面还会遇到'openai/clip-vit-large-patch14离线下载的问题,同样步骤解决。解决:下载后离线,步骤如下。

weixin_52121118的博客 2348

sql bug之repalce:Argument data type ntext is invalid

sql bug之repalce:Argument data type ntext is invalid

m0_47448095的博客 1311

傅里叶变换实战:用Python手把手教你分析音频频谱(附完整代码)

本文详细介绍了如何使用Python实现傅里叶变换进行音频频谱分析,涵盖音频加载、预处理、FFT应用、频谱可视化及特征提取等关键步骤。通过实战案例和完整代码,帮助开发者掌握数字信号处理技术,特别适合音频分析和音乐信息检索领域的应用。

weixin_29090677的博客 150

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

Quantum Espresso Hands-On实战训练(四)——PDOS计算与轨道分析

本文详细介绍了Quantum Espresso中PDOS计算与轨道分析的实战方法,重点解析了PDOS在材料电子结构研究中的核心价值,包括原子级分辨率、轨道级解析和自旋极化分析。通过具体案例和操作步骤,帮助读者掌握从输入文件准备到结果解读的全流程,并提供了高级分析技巧和疑难问题解决方案。

weixin_29211309的博客 504

常见卫星图源下载教程

上面列出的8个常见的亚米级别数据源都可以通过xyz协议或者wmts/wms协议进行下载。这些协议是什么?地理信息数据为了方便网络传输,制定了一系列的地理数据传输标准(wms/wmts/xyz等)。协议特点优点缺点栅格数据支持矢量数据支持WMS通过HTTP请求获取GIS数据,服务器会将数据渲染成图像高度定制的地图;交互性强服务器渲染图像,响应时间较长支持有限支持WMTS提供预渲染的地图瓦片速度快,因为地图瓦片可以预先生成并在请求时快速提供瓦片的内容是预先确定的,无法进行定制支持。

锐多宝的地理空间 1万+

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

自定义View系列教程04--Draw源码分析及其实践

通过之前的详细分析,我们知道:在measure中测量了View的大小,在layout阶段确定了View的位置。 完成这两步之后就进入到了我们相对熟悉的draw阶段,在该阶段真正地开始对视图进行绘制。按照之前的惯例,我们来瞅瞅View中draw( )的源码 public void draw(Canvas canvas) { final int privateFlags = mPriv

谷哥的小弟 2万+

(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

OpenDataLab 大模型训练数据集下载记录

大模型 训练数据集

u013558123的博客 3348

如何用libwebsocket库在stm32F407上实现websocket客户端?

🏆本文收录于 《全栈 Bug 调优(实战版)》 专栏。专栏聚焦真实项目中的各类疑难 Bug,从成因剖析 → 排查路径 → 解决方案 → 预防优化全链路拆解,形成一套可复用、可沉淀的实战知识体系。无论你是初入职场的开发者,还是负责复杂项目的资深工程师,都可以在这里构建一套属于自己的「问题诊断与性能调优」方法论,助你稳步进阶、放大技术价值 。

**My Coding Family** 1135

高精度问题之大数求幂

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

【RT-DETR实战】112、NCNN模型转换与层实现支持:从模型文件到推理引擎的硬核实战

本文详细记录了将RT-DETR模型部署到NCNN框架的全过程及关键要点。作者首先遇到MultiHeadAttention等算子不支持的问题,通过ONNX中间转换和自定义层实现解决。文章具体介绍了PyTorch到NCNN的权重转换技巧、模型优化方法(包括量化和线程优化)以及调试经验,强调逐层对比输出、预处理检查和边界情况测试的重要性。最后分享了保持转换流程可复现性的实用建议,指出手动调试虽然耗时但能加深对模型的理解,体现了工程师在解决技术难题时的专业精神与成就感。

企鹅码农的专属播客,分享10多年的实战案例和经验解读,包含YOLO、RT-DETR、AI等各个工作领域。 44

java在ACM中的应用

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

自由女神 2148

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

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

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

qq_46844003的博客 384

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

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

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

ecchi's blog 1942

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

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

UVA - 748 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.

七夜 604

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

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

qq_64246637的博客 593

SQL CHARINDEX

We use the SQL CHARINDEX function to find the position of a substring or expression in a given string. We might have a character in different positions of a string. SQL CHARINDEX returns the first...

culuo4781的博客 1497

PostgreSQL数据库中的常见错误

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

步行者的专栏 11万+
上一篇: 从作弊谈设计
下一篇: ACM模拟题讲解(1)-高精度
老李-大连
博客等级 码龄20年 3206粉丝 308原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值