ACM常见高精度总结(java用法)

ACM模式常见输入输出专题(Java版) Java语言ACM模式算法常见的输入输出练习。 阅读详情
在用C或者C++处理大数时感觉非常麻烦,但是在JAVA中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类,至于两个类的对象能表示最大范围不清楚,理论上能够表示无线大的数,只要计算机内存足够大。
这两个类都在java.math.*包中,因此每次必须在开头处引用该包。

Ⅰ基本函数:

1.valueOf(parament); 将参数转换为制定的类型

比如 int a=3;

BigInteger b=BigInteger.valueOf(a);
则b=3;
String s=”12345”;
BigInteger c=BigInteger.valueOf(s);

则c=12345;


2.add(); 大整数相加
BigInteger a=new BigInteger(“23”);
BigInteger b=new BigInteger(“34”);

a.add(b);


3.subtract(); 相减

4.multiply(); 相乘

5.divide();    相除取整

6.remainder(); 取余

7.pow();   a.pow(b)=a^b

8.gcd();   最大公约数

9.abs(); 绝对值

10.negate(); 取反数

11.mod(); a.mod(b)=a%b=a.remainder(b);

12.max(); min();

13.public int compareTo();

14.boolean equals(); 是否相等

15.BigInteger构造函数:
一般用到以下两种:
BigInteger(String val);
将指定字符串转换为十进制表示形式;
BigInteger(String val,int radix);
将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger

Ⅱ.基本常量:

A=BigInteger.ONE    1

B=BigInteger.TEN    10

C=BigInteger.ZERO   0

Ⅲ.基本操作

1.读入:

用Scanner类定义对象进行控制台读入,Scanner类在java.util.*包中

Scanner cin=new Scanner(System.in);// 读入
while(cin.hasNext)   //等同于!=EOF
{
 int n;
 BigInteger m;
 n=cin.nextInt(); //读入一个int;
 m=cin.nextBigInteger();//读入一个BigInteger;
 System.out.print(m);
}

Ⅳ.运用

四则预算:

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main  
{  
	public static void main(String args[])  
	{
		Scanner sca = new Scanner(System.in);
		BigInteger a,b;
		int c;//为小数设置精度
		char op;//运算符
		String s;
		while(sca.hasNext()){
			a = sca.nextBigInteger();
			s = sca.next();
			op = s.charAt(0);
			b = sca.nextBigInteger();
			if(op == '+')
				System.out.println(a.add(b));
			else if(op == '-')
				System.out.println(a.subtract(b));
			else if(op == '*')
				System.out.println(a.multiply(b));
			else{
				BigDecimal t1,t2,eps;
				String s1,s2,temp;
				s1 = a.toString();
				s2 = b.toString();
				t1 = new BigDecimal(s1);
				t2 = new BigDecimal(s2);
				c = sca.nextInt();
				eps = t1.divide(t2,c,4);
				//System.out.println(a + " " + b + " " + c);  
			    //System.out.println(t1.doubleValue() + " " + t2.doubleValue() + " " + c);
				System.out.print(a.divide(b) +" "+ a.mod(b)+" ");
				if(c != 0){
					temp = "0.";
					for(int i = 0; i < c; i++)
						temp += "0";
					DecimalFormat gd = new DecimalFormat(temp);
					System.out.println(gd.format(eps));
				}
				else
					System.out.println(eps);
			}
		}
	}
}
  
//=====================================================================================//  
charAt
public char charAt(int index)

Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing. 
If the char value specified by the index is a surrogate, the surrogate value is returned.


divide
public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)

Returns a BigDecimal whose value is (this / divisor), and whose scale is as specified. If rounding must be performed to generate a result with the specified scale, the specified rounding mode is applied. 


public DecimalFormat(String pattern)
Creates a DecimalFormat using the given pattern and the symbols for the default locale. This is a convenient way to obtain a DecimalFormat when internationalization is not the main concern.
To obtain standard formats for a given locale, use the factory methods on NumberFormat such as getNumberInstance. These factories will return the most appropriate sub-class of NumberFormat for a given locale.


hdu1002

http://acm.hdu.edu.cn/showproblem.php?pid=1002

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sca = new Scanner(System.in);
    	int T = sca.nextInt(),t = 1;
    	while(t <= T){
    		BigInteger a,b;
    		a = sca.nextBigInteger();
    		b = sca.nextBigInteger();
    		BigInteger c = a.add(b);
    		System.out.println("Case "+ t + ":");
    		System.out.println(a + " + " + b + " = " + c);
    		if(t != T)
    			System.out.println("");
    		t++;
    	}
	}
}

 
hdu1042 

http://acm.hdu.edu.cn/showproblem.php?pid=1042

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sca = new Scanner(System.in);  
		BigInteger a[] = new BigInteger[10005];
		a[0] = BigInteger.ONE;
		for(int i = 1; i <= 10000; i++)
			a[i] = a[i - 1].multiply(BigInteger.valueOf(i));
		int n;
		while(sca.hasNextInt()){
			n = sca.nextInt();
			System.out.println(a[n]);
		}
	}
}

hdu1047

http://acm.hdu.edu.cn/showproblem.php?pid=1047

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		 Scanner sca = new Scanner(System.in);
		 int T = sca.nextInt(), t = 1;
		 while(t <= T){
			 BigInteger sum = BigInteger.ZERO;
			 BigInteger zero =  BigInteger.ZERO;
			 BigInteger tmp;
			 while(sca.hasNextBigInteger()){
				 tmp = sca.nextBigInteger();
				 if(tmp.equals(zero))
					 break;
				 else
					 sum = sum.add(tmp);
			 }
			 System.out.println(sum);
			 if(t != T)
				 System.out.println();
			 t++;
		 }
	}
}

hdu1063

http://acm.hdu.edu.cn/showproblem.php?pid=1063

import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sca = new Scanner(System.in);
		 BigDecimal R,sum;
		 int n;
		 while(sca.hasNextBigDecimal()){
			 sum = BigDecimal.ONE;
			 R = sca.nextBigDecimal();
			 n = sca.nextInt();
			 for(int i = 1; i <= n; i++)
				 sum = sum.multiply(R);
			 sum = sum.stripTrailingZeros();
			 String str = sum.toPlainString();
			 if(str.startsWith("0."))
				 str = str.substring(1);
			 System.out.println(str);
		 }
	}
}

stripTrailingZeros
public BigDecimal stripTrailingZeros()

Returns a BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation. For example, stripping the trailing zeros from the BigDecimal value 600.0, which has [BigInteger, scale] components equals to [6000, 1], yields 6E2 with [BigInteger, scale] components equals to [6, -2]
Returns:a numerically equal BigDecimal with any trailing zeros removed.


toPlainString
public String toPlainString()

Returns a string representation of this BigDecimal without an exponent field. For values with a positive scale, the number of digits to the right of the decimal point is used to indicate scale. For values with a zero or negative scale, the resulting string is generated as if the value were converted to a numerically equal value with zero scale and as if all the trailing zeros of the zero scale value were present in the result. The entire string is prefixed by a minus sign character '-' ('\u002D') if the unscaled value is less than zero. No sign character is prefixed if the unscaled value is zero or positive. Note that if the result of this method is passed to the string constructor, only the numerical value of this BigDecimal will necessarily be recovered; the representation of the new BigDecimal may have a different scale. In particular, if this BigDecimal has a negative scale, the string resulting from this method will have a scale of zero when processed by the string constructor. (This method behaves analogously to the toString method in 1.4 and earlier releases.)
Returns:
a string representation of this BigDecimal without an exponent field.

startsWith
public boolean startsWith(String prefix)

Tests if this string starts with the specified prefix.
Parameters:prefix - the prefix.Returns:true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise. Note also that true will be returned if the argument is an empty string or is equal to this String object as determined by the equals(Object) method.


hdu1316

http://acm.hdu.edu.cn/showproblem.php?pid=1316

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sca = new Scanner(System.in);
		 int i,sum;
		 BigInteger a,b;
		 BigInteger f[] = new BigInteger[505];
		 BigInteger zero = BigInteger.ZERO;
		 f[1] = new BigInteger("1");
		 f[2] = new BigInteger("2");
		 for(i = 3; i <= 500; i++)
			 f[i] = f[i - 1].add(f[i - 2]);
		 while(sca.hasNextBigInteger()){
			 sum = 0;
			 a = sca.nextBigInteger();
			 b = sca.nextBigInteger();
			 if(a.equals(zero) && b.equals(zero))
				 break;
			 for(i = 1; i <= 500; i++){
				 if(a.compareTo(f[i]) <= 0 && b.compareTo(f[i]) >= 0)
					 sum++;
				 if(b.compareTo(f[i]) < 0)
					 break;
			 }
			 System.out.println(sum);
		 }
	}
}

hdu1715

http://acm.hdu.edu.cn/showproblem.php?pid=1715

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sca = new Scanner(System.in);
		 int t = 1, T = sca.nextInt();
		 BigInteger f[] = new BigInteger[1005];
		 f[1] = f[2] = BigInteger.ONE;
		 for(int i = 3; i <= 1000; i++)
			 f[i] = f[i - 1].add(f[i - 2]);
		 while(sca.hasNext()){
			 int n = sca.nextInt();
			 System.out.println(f[n]);
		 }
	}
}

hdu1753

http://acm.hdu.edu.cn/showproblem.php?pid=1753

import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sca = new Scanner(System.in);
		 BigDecimal a,b,c;
		 while(sca.hasNext()){
			 a = sca.nextBigDecimal();
			 b = sca.nextBigDecimal();
			 c = a.add(b);
			 c = c.stripTrailingZeros();
			 String str = c.toPlainString();
			 if(str.startsWith("0."))
				 str = str.substring(1);
			 System.out.println(str);
		 }
	}
}


ACM模式常用方法总结(Java篇) 主要记录ACM模式下的编码方式 阅读详情

相关推荐

Java中常用函数汇总

Java中常用函数汇总,汇总了acm java的部分代码!譬如:lastIndexOf()这个方法是在字符串中查找第一次出现某个特定字符串的位置,方向是从后向前,找到之后则返回特定字符的索引,找不到匹配的字符串则返回-1。

Java语言中ACM模式输入输出- 1(大厂 笔试题必备知识)

ACM模式 练习,主要包含一些 输入输出 格式的练习,笔试题目 必会技能,防止卡在输入关。

qq_40222433的博客 4604

教学计划编制源代码c语言,《数据结构C语言》教学计划编制问题.doc

课 程 设 计 报 告课程名称 数据结构课程设计课题名称 教学计划编制问题2011年 月 日教学计划编制问题.输入参数包括:学期总数,一学期的学分上限,每门课的课程号(固定占3位的字母数字串)、学分和直接先修课的课程号。.允许用户指定下列两种编排策略之一:一是使学生在各学期中的学习负担尽量均匀;.若根据给定的条件问题无解,则报告适当的信息;否则,将教学计划输出到用户指定...

weixin_29001655的博客 2405

快速学会ACM模式 -- Java

快速学会ACM模式

Alex抱着爆米花的博客 1557

基于AR和SLAM技术的商场智能导视系统技术原理详解

本文针对智慧商场室内导航需求,提出基于AR和SLAM技术的商场智能导视系统技术方案,实现了沉浸式导览体验并促进消费转化,助力商场运营升级。

维小帮定位导航的博客 1257

基于STM32控制直流电机加减速正反转仿真设计

*单片机设计介绍,基于STM32控制直流电机加减速正反转仿真设计。

2201_75940997的博客 1615

java中biginteger和bigdecimal在大数计算中的使用

java中的BigInteger和BigIntegerDecimal 当我们在做Acm的大数题时,我们会发现int,double,表示的范围有限,不能够满足要求,对于c/c++而言,我们就只能采用数组模拟的方法来实现高精度大数的操作。然而java的jdk1.5后就可以使用math包中的BigInteger和BigDecimal来帮助我们解决高精度大数和小数的问题。 1 BigInteg

啦啦啦啦的成长之路 4341

BambuStudio学习笔记:OpenCSG - 实时构造实体几何(CSG)渲染的 OpenGL 方案

**OpenCSG** 是一个基于 **OpenGL** 的 **构造实体几何(CSG)** 实时渲染库,通过 **深度剥离算法** 实现布尔运算(并集、交集、差集)的可视化。其核心目标是简化复杂几何体的交互式建模,适用于 CAD 原型设计、3D 打印预览、教育工具等场景。

zzzkk2009的专栏 940

ACM/ICPC Java高精度计算

<br /><br />ACM/ICPC竞赛中会遇到许多高精度计算的题目,比如两个大整数相加(1111111111111111111111111111111111111111 + 22222343259435894305894301243)。如果我们还采用普通类型比如long long来存储这些值,是装不下这么大的值的了。<br />我们通常的做法是以字符串的形式保存值,然后编程模拟人的计算过程,例如<br />X = “111”;<br />Y = “999”;<br />X + Y用如下方法计算:<br

深秋的落叶 1778

EPLAN 电气制图(六):电机正反转副勾主电路绘制

【插入】→【设备】→选择 “SIE.3RV2011-1AA15”(Q1)和 “SIE.1TL0001-1DB3”(M1),放置于图纸左侧。生成端子排 X3:【端子排导航器】→新建编号 “1-20”,部件 “PXC.3211814”,放置于电机上方,功能文本标注 “电机端子”。属性设置:双击结构盒,在 “显示设备标识符” 中输入位置代号(如 “+ 柜外”),并在 “格式” 选项卡中调整线型与颜色。实用技巧:勾选【选项】→【设置】→【图形的编辑】中的 “绘制带有空白区域的结构盒”,可使元件布局更清晰。

电工 2054

人工智能训练师(三级)操作技能考核·自测卷

为您定制了一份。这份试卷严格拆分了“业务分析”、“智能训练”、“智能系统设计”、“培训与指导”四大模块,所有填空题均来源于上一轮整理的。您可以先用它进行“第二步(抛开答案自己填)”,填完后对照末尾的答案速查进行“第一步(补全并运行)”的校验,最后标记错题进行“第三步(针对性重复练习)”。

suny8088的博客 133

ACM之Java输入输出

一、Java之ACM注意点 1. 类名称必须采用public class Main方式命名 2. 在有些OJ系统上,即便是输出的末尾多了一个“ ”,程序可能会输出错误,所以在我看来好多OJ系统做的是非常之垃圾 3. 有些OJ上的题目会直接将OI上的题目拷贝过来,所以即便是题目中有输入和输出文件,可能也不需要,因为在OJ系统中一般是采用标准输入输出,不需要文件 4. 在有多行

samjustin的博客 1万+

以 Joule 驱动的生成式开发:把需求文档直接变成可运行的 SAP 应用

摘要:SAP BTP的AI助手Joule正在改变开发方式,支持从自然语言描述自动生成数据模型、服务、UI及测试代码。在SAP Business Application Studio中,Joule能根据业务需求生成结构化代码,并提供代码补全、重构、解释等功能。开发者需注意避免上传敏感数据,并对AI生成的代码进行人工审核。Joule特别针对ABAP开发进行了优化,提供符合SAP规范的代码建议,帮助迁移遗留系统。此外,它还能快速生成CAP项目原型,但所有AI生成内容都需经过严格测试验证。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 358

【acm模板整理】java高精度

注:本系列只为整理、保存比赛模板用,并非代码均为自己所写。会给出原代码的链接。 原链接:http://pepcy.cf/icpc-templates/008Others/javabi.html 一个好用的模板: import java.math.BigInteger; import java.util.Scanner; public class Main { public sta...

eulers_student的博客 347

机器人中的数值优化(二十一)—— 伴随灵敏度分析、线性方程组求解器的分类和特点、优化软件

本系列文章主要是我在学习《数值优化》过程中的一些笔记和相关思考,主要的学习资料是深蓝学院的课程《机器人中的数值优化》和高立编著的《数值最优化方法》等,本系列文章篇数较多,不定期更新,上半部分介绍无约束优化,下半部分介绍带约束的优化,中间会穿插一些路径规划方面的应用实例

慕羽★的博客 4211

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

java在ACM竞赛中的使用

java简单功能强大,高精度类可以帮队员轻松地实现对大数操作,而不用C/C++手写高精度。使用java做题,不容易犯一些低级的错误,如: if (a = b)。刚刚接触acm的时候,笔者还不会使用java,为此还特地花了一个月时间学习了一下java语言。现在回过头来发现,学会在比赛中熟练地使用java其实根本不需要花很长时间......所以在这里小结一下java在acm比赛中的使用。

仰望圣光的plusplus7 4545

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

ACM中Java的使用

判断是否有下一个输入可以用 cin.hasNext() 或 cin.hasNextInt() 或 cin.hasNextDouble() 等,具体见 TOJ 1001 例程。判断是否有下一个输入可以用 cin.hasNext() 或 cin.hasNextInt() 或 cin.hasNextDouble() 等,具体见 TOJ 1001 例程。(3) 在一般比赛中,Java程序会有额外的时间和空间,而实际上经过实验,在执行计算密集任务的时候Java并不比C/C++慢多少,只是IO操作较慢而已。

2401_87167658的博客 846

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

ACM_高精度算法

ACM算法 ——高精度算法。

小安的博客 1503

acm 用java_ACM中使用JAVA的详细介绍

0x01:Java的优缺点各种书上都有,这里只说说用Java做ACM-ICPC的特点:(1)最明显的好处是,学会Java,可以参加JavaChallenge。(2)对于熟悉C/C++的程序员来说,Java并不难学,找本书,一两周业余时间就可以搞定了。当然,这里只是指一般编程,想熟悉所有的Java库还是需要些时间的。事实上,Java只相当于C++的一个改进版,所有的语法都几乎是C++的,...

weixin_31565723的博客 983

JAVA 在 ACM 中的使用

ACM中java的使用 这里指的java速成,只限于java语法,包括输入输出,运算处理,字符串和高精度的处理,进制之间的转换等,能解决OJ上的一些高精度题目。 1. 输入: 格式为:Scanner cin = new Scanner (new BufferedInputStream(System.in)); 例程: import java.io.*; import java.ma

free斩 1364

java acm_ACM中java的使用

ACM中java的使用这里指的java速成,只限于java语法,包括输入输出,运算处理,字符串和高精度的处理,进制之间的转换等,能解决OJ上的一些高精度题目。1. 输入:格式为:Scanner cin = new Scanner (new BufferedInputStream(System.in));例程:import java.io.*;import java.math.*;import ja...

weixin_36036343的博客 239

acm java题_求ACM大神告知JAVA在一些ACM题目中的用法

展开全部先说一下Java对于ACM的一些优点吧:(1) 对于熟悉C/C++的程序员来说Java 并不难学,两周时间e5a48de588b662616964757a686964616f31333332643837基本可以搞定一般的编程,再用些时间了解一下Java库就行了。Java的语法和C++非常类似,可以说是C++的升级版,只是更加强调面向对象思想而已。(个人见解。。。)(2) 在一般比赛中,Ja...

weixin_28862967的博客 237
上一篇: 蓝桥杯 算法训练 字串统计
下一篇: 解决HDU爆栈的问题
yphacker
博客等级 码龄12年 199粉丝 540原创
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址