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 ...
随机推荐
- 77. Combinations
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...
- redhat 7.2 配置yum源
http://blog.csdn.net/wylfengyujiancheng/article/details/50418930
- 1934. Black Spot(spfa)
1934 水题 RE了N久 后来发现是无向图 #include <iostream> #include<cstring> #include<algorithm> # ...
- iOS开发:本地数据存储-NSUserDefaults
Getting Default Values arrayForKey(_:) boolForKey(_:) dataForKey(_:) dictionaryForKey(_:) floatForKe ...
- ASP.NET MVC ActionResult的实现
1.11种ActionResult 在System.Web.Mvc命名空间下: ActionResult是一个抽象类, 在Action中返回的都是其派生类.下面是我整理的ASP.NET MVC 1.0 ...
- Web网站的性能测试工具
随着Web 2.0技术的迅速发展,许多公司都开发了一些基于Web的网站服务,通常在设计开发Web应用系统的时候很难模拟出大量用户同时访问系统的实际情况,因此,当Web网站遇到访问高峰时,容易发生服务器 ...
- 让你的 Node.js 应用跑得更快的 10 个技巧(转)
Node.js 受益于它的事件驱动和异步的特征,已经很快了.但是,在现代网络中只是快是不行的.如果你打算用 Node.js 开发你的下一个Web 应用的话,那么你就应该无所不用其极,让你的应用更快,异 ...
- javascript实现继承的一种方式
function extend(Child, Parent) { var F = function(){}; F.prototype = Parent.prototype; Child.prototy ...
- echo二次开发 ecshop 函数列表
lib_time.php (时间函数) gmtime() P: 获得当前格林威治时间的时间戳 /$0 server_timezone() P: 获得服务器的时区 /$0 local_mktime($h ...
- 【转】traits技术及模板偏特化
#include <iostream> using namespace std; struct __xtrue_type { }; // define two mark-type stru ...