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

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

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

从FinFET到NanoSheet:一文看懂TSMC制程技术的架构革命与性能飞跃 本文深入解析了TSMC从FinFET到NanoSheet的制程技术架构革命。FinFET通过三维立体结构解决了短沟道效应,而NanoSheet采用环绕栅极设计,实现了更强的静电控制与设计灵活性,结合EUV光刻技术,共同推动了芯片性能与能效的飞跃,为下一代AI与高性能计算奠定基石。 阅读详情

相关推荐

Xilinx Vitis arm-xilinx-eabi-gcc.exe: error: *.c: Invalid argument

This problem may be a bugof version 2021.1 only. Unfortunately, I'm just currently using this version. After Build Project: Find where this Makefile is through "make: Leaving directory" information. For me, it is located atZynq_CPU_wrapper_hw_platfo...

Ayka的博客 6674

sql bug之repalce:Argument data type ntext is invalid

sql bug之repalce:Argument data type ntext is invalid

m0_47448095的博客 1315

xHCI驱动学习(1) 核心数据结构

虽然Linux内核拥有C语言构建的身体,但它骨子里散发的是面向对象的气质,这一个个的对象就是struct。面对一个内核模块的时候,首先要找出关键的struct和它们之间的关系,才能摸清代码的骨骼脉络。今天来理清USB xHCI相关的数据结构类型usb_hcd、xhci_hcd、hc_driver和全局变量xhci_pci_driver、xhci_hc_driver、xhci_pci_hc_driver之间的关系。

1万+

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

Windows 10下用vcpkg一键搞定libcurl开发环境(Code::Blocks 20.03配置指南)

本文详细介绍了在Windows 10系统下使用vcpkg工具快速配置libcurl开发环境的完整指南,特别针对Code::Blocks 20.03 IDE进行优化设置。通过vcpkg自动化管理依赖项,简化libcurl的安装与配置过程,帮助开发者高效搭建网络通信功能开发环境,并提供了常见问题解决方案和实际项目中的最佳实践。

weixin_28040125的博客 260

DNN加速器脉动阵列轻量级瞬时故障检测

本文系统综述了面向深度神经网络(DNN)脉动阵列加速器的轻量级瞬时故障检测技术。通过分析比较算法级校验(ABFT)、数值异常监测(剪裁、中值滤波等)、硬件编码检错(AN码、剩余码)以及选择性冗余等方案,指出这些方法能以5%-10%的开销实现对权重和激活比特翻转的高效检测,远低于传统ECC/冗余计算的成本。研究显示,结合多种轻量技术可构建多层防护体系,在保障推理可靠性的同时保持加速器的高性能特性。该成果为安全关键领域的DNN加速器设计提供了重要参考。

欢迎交流! 1085

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

QGIS-空间分析-核密度分析

本文在QGIS软件中使用核密度估计法分析全球历年的地震数据,旨在分析出地震频发且严重的区域,为地震防控提供参考依据。全球历年地震数据来源于网络,仅用于技术学习探索。通过核密度分析,可以观察到南美、美国西部海岸线和太平洋北部区域在全球地震分布状态中是最为集中和强烈的。

imxiezy的博客 2477

(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

【温室气体】GOSAT 数据产品

【温室气体】GOSAT 数据产品

WW、forever的博客 249

FPGA上的时间数字转换器(TDC):详细概述及FPGA开发

本文对基于FPGA的时间数字转换器(TDC)进行了详细的概述,并提供了一个简单的示例代码,展示了如何在FPGA上实现TDC功能。在本文中,我们将详细概述基于FPGA的TDC,并提供相应的源代码示例,以帮助读者理解和实现这一功能。需要注意的是,这只是一个简单的示例,实际的TDC设计可能需要更多的功能和优化,以满足特定的需求。读取和重置:最后,测量结果可以通过读取计数器的值来获取,并且在每次读取后需要将计数器重置为零,以便进行下一次测量。TDC的基本原理是测量信号的到达时间与参考时钟之间的时间差。

EsqpWeller的博客 6899

高精度问题之大数求幂

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

yolov26改进 | 融合改进篇 | 轻量级移动端网络ShuffleNetV2融合CCFM参数量仅百万(附代码+修改教程)

本文介绍ShuffleNetV2与跨尺度特征融合模块CCFM融合改进的YOLOv26。ShuffleNetV2以硬件效率为导向,通过通道分割、轻量卷积和Channel Shuffle减少冗余计算与内存访问,在降低参数量和GFLOPs的同时保持高效特征提取;CCFM进一步加强不同尺度特征交互,弥补轻量主干语义表达不足的问题。本人在轻量模型上测试后,参数量和计算量明显下降,检测精度有一定提升,适合移动端、边缘设备及实时检测任务。文中提供完整代码、配置文件、模块注册及Codex修改思路,方便复现、对比和二次创新。

Snu77的博客 216

java在ACM中的应用

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

自由女神 2149

假人有限元模型

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

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

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

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

qq_46844003的博客 384

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

总结下吧...(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的专栏 475

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

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万+

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

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址