Java in ACM/ICPC
目录
Java在ACM/ICPC中的特点
在ACM/ICPC中使用Java需要注意的问题
Java与高精度计算
1.Java在ACM/ICPC中的特点
Java的语法和C++几乎相同
Java在执行计算密集任务的时候并不比C/C++慢多少,只是IO操作较慢而已
Java 简单而功能强大,有些东西用Java实现起来更为方便
比如:输入输出、字符串解析、高精度
Java不易犯细微的错误
C/C++中的指针
“if (n = m) ... ”
Java与Eclipse
2.在ACM/ICPC中使用Java需要注意的问题
java程序结构
Java I/O
JDK1.5.0新增的Scanner类很适合用于AMC/ICPC的输入
使用Scanner类的一般步骤
1.导入Scanner类
import java.util.Scanner;
Scanner cin=new Scanner(System.in); //从标准输入读入数据
Scanner cin=new Scanner(“12 30”)); //从字符串读入数据
cin.nextInt()
cin.nextDouble();
…
Scanner类的常用方法
Scanner类方法 |
对应C操作 |
对应C++操作 |
int n = cin.nextInt(); |
scanf("%d", &n); |
cin >> n; |
String s = cin.next(); |
scanf("%s", s); |
cin >> s; |
double t = cin.nextDouble(); |
scanf("%lf", &t); |
cin >> t; |
String s = cin.nextLine(); |
gets(s); |
cin.getline(...); |
BigDecimal b=cin.nextBigDecimal(); |
cin.hasNext() 或 cin.hasNextInt() 或 cin.hasNextDouble()
Scanner类的用方法示例:
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
char ch=(char)sc.nextInt();
System.out.print(ch);
}
}
}
import java.io.*;
public class Main{
static PrintStream cout=System.out;
public static void main(String[] args){
int n=3,m=5;
cout.println(n); // 输出3
//同一行输出多个整数可以用
cout.println(n+" "+m);
}
}
用DecimalFormat类控制浮点数小数位数
import java.text.DecimalFormat;
控制方法
构造特定的DecimalFormat对象:DecimalFormat f=new DecimalFormat(“#.00#”);
构造函数中的参数是模式字符串,0指一位数字,#指除0以外的数字
使用DecimaFormat对象格式化需要输出的浮点数:System.out.println(f.format(12.1234));
DecimalFormat示例
import java.text.*;
public class decimalformat{
public static void main(String[] args){
DecimalFormat f = new DecimalFormat("#.00#");
DecimalFormat g = new DecimalFormat("0.000");
double a = 123.4509, b = 0.12;
System.out.println(f.format(a));
System.out.println(g.format(a));
System.out.println(f.format(b));
System.out.println(g.format(b));
}
}
运行结果:
123.451
123.451
.12
0.120
int a=10;
float b=2.35f;
System.out.printf("%d %10.5f\n", a, b);
字符串(String)
String类常用方法:
String s=“abcde”;
char[] chs={‘a’,’b’,’c’,’d’,’e’};
String s=new String(chs);
char ch=s.charAt(1); //ch=‘b’;
System.out.println(s.substring(0, 3)) // output “abc"
System.out.println(s.substring(1, 3)) // output “bc"
System.out.println(s.substring(1)) // output “bcde"
String s=“123:34:55”;
String[] ss = s.split(“:”);
for(int i=0;i<ss.length;i++) System.out.println(ss[i]);
运行结果:
123
34
55
String s=“2009-07-26”;
System.out.println( s.replace(‘-’,’//’) ); //输出2009/07/26
String s=“0.123456”;
System.out.println( s.replaceAll(“^0”,””) ); //输出.123456
String中的字符不能改变,如果需要改变可以使用StringBuffer
其他注意的事项
Java数组是对象,定义后必须初始化,如 int[] a = new int[100]; 数组长度由length成员得到,如System.out.println(a.length);
Arrays类提供的一些有用方法:
Arrays.fill()
Arrays.sort()
Arrays.binarySearch()
布尔类型为 boolean,只有true和false二值,在 if (...) / while (...) 等语句的条件中必须为boolean类 型。
在C/C++中的 if (n % 2) ... 在Java中无法编译通过。
Java也提供了类似STL的集合类:
Vector,ArrayList,Map,Queue,Stack,Hashtable
3.Java与高精度计算
Sample Input
95.123 12
0.4321 20
5.1234 15
6.7592 9
98.999 10
1.0100 12
Sample Output
548815620517731830194541.899025343415715973535967221869852721
. 00000005148554641076956121994511276767154838481760200726351203835
429763013462401
43992025569.928573701266488041146654993318703707511666295476720493
953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201
用C/C++解决的方法
C/C++的pow函数无法达到需要的精度
C/C++用数组来模拟乘法运算提高精度
import java.math.*;
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
while(in.hasNext()){
BigDecimal val=in.nextBigDecimal();
int n=in.nextInt();
BigDecimal ret=val.pow(n).stripTrailingZeros();
System.out.println( ret.toPlainString().replaceAll("^0", "") );
}
}
}
BigDecimal类
BigDecimal (int val)
BigDecimal (double val)
BigDecimal (String val)
BigDecimal d1=new BigDecimal(1);
BigDecimal d2=new BigDecimal(0.1);
BigDecimal d3=new BigDecimal("0.1"); System.out.println("d1="+d1);
System.out.println("d2="+d2);
System.out.println("d3="+d3);
BigDecimal add(BigDecimal augend) // “+”
BigDecimal subtract(BigDecimal subtrahend) // “-”
BigDecimal multiply(BigDecimal multiplicand) // “*”
BigDecimal divide(BigDecimal divisor) // “/”
BigDecimal remainder(BigDecimal divisor) // “%”
BigDecimal pow(int n) //“求幂”
String toPlainString() //返回不带指数的字符串表示
String toString() //返回带指数的字符串表示
1131、1205、1220、1405、1503、1604 1894、2084、2305、2325、2389、2413 3101、3199
Java in ACM/ICPC的更多相关文章
- hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Ot ...
- 【转】lonekight@xmu·ACM/ICPC 回忆录
转自:http://hi.baidu.com/ordeder/item/2a342a7fe7cb9e336dc37c89 2009年09月06日 星期日 21:55 初识ACM最早听说ACM/ICPC ...
- hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/O ...
- hduoj 4715 Difference Between Primes 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Time Limit: 2000/1000 MS (J ...
- hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...
- hduoj 4706 Herding 2013 ACM/ICPC Asia Regional Online —— Warmup
hduoj 4706 Children's Day 2013 ACM/ICPC Asia Regional Online —— Warmup Herding Time Limit: 2000/1000 ...
- hduoj 4707 Pet 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4707 Pet Time Limit: 4000/2000 MS (Java/Others) Memory ...
- hduoj 4706 Children's Day 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4706 Children's Day Time Limit: 2000/1000 MS (Java/Others) ...
- 2016 ACM/ICPC Asia Regional Shenyang Online 1003/HDU 5894 数学/组合数/逆元
hannnnah_j’s Biological Test Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
随机推荐
- 31. Next Permutation
题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...
- C++:类型转换
5.3 类型转换 5.3.1 系统预定义类型间的转换 1. 隐式转换: 例如: int x=5,y; y=3.5+x; //系统会自动先将int型的5转换为double型的5. ...
- Intellij Idea 创建EJB项目入门(一)
相关软件: 1.JBoss(jboss-as-7.1.1.Final):http://jbossas.jboss.org/downloads 2.Intellij IDEA 13.02 3.JDK 1 ...
- 通俗易懂的讲解iphone视图控制器的生命周期
IOS 视图控制器的生命周期非常非常重要,所以我有必要写个文章来和大家一起探讨问题. 今天在学习视图控制器的生命周期,也看了一下网上的一些资料,但总觉得不是那么好理解,首先我们来看一张图: 先粗略讲一 ...
- 设置图片Alpha
Drawable ico = getResources().getDrawable(R.drawable.search_box); mSearchLayou ...
- LA 3510 (置换 循环分解) Pixel Shuffle
思路挺简单的,题目中的每个命令(包括命令的逆)相当于一个置换. 用O(n2k)的时间复杂度从右往左求出这些置换的乘积A,然后求m使Am = I(I为全等置换) 还是先把A分解循环,m则等于所有循环节长 ...
- web交互方式
轮询:客户端定时向服务器发送Ajax请求,服务器接到请求后马上返回响应信息并关闭连接. 优点:后端程序编写比较容易. 缺点:请求中有大半是无用,浪费带宽和服务器资源. 实例:适于小型应用. 长轮询:客 ...
- 淘宝技术发展(Java时代:脱胎换骨)
我的师父黄裳@岳旭强曾经说过,“好的架构图充满美感”,一个架构好不好,从审美的角度就能看得出来.后来我看了很多系统的架构,发现这个言论基本成立.那么反观淘宝前面的两个版本的架构,你看哪个比较美? 显然 ...
- Tomcat 7.0配置SSL的问题及解决办法
http://dong-shuai22-126-com.iteye.com/blog/1830209 以前一直在用Tomcat 6.0.29版本,今下载了apache-tomcat-7.0.33- ...
- shell -Z- d等等代表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 [ -a FILE ] ...