ACM中使用JAVA的介绍
看到下面的文章,转载来~
以搞不清原作者是谁,若有幸遇到请与我联系
Chapter I.
这里指的java速成,只限于java语法,包括输入输出,运算处理,字符串和高精度的处理,进制之间的转换等,能解决OJ上的一些高精度题目。
1. 输入: 例程: public class Main 例程: public class Main 规格化的输出: 例程: public class Main 例程: public class Main 例程: public class Main
格式为:Scanner cin = new Scanner (new BufferedInputStream(System.in));
import java.io.*;
import java.math.*;
import java.util.*;
import java.text.*;
{
public static void main(String[] args)
{
Scanner cin = new Scanner (new BufferedInputStream(System.in));
int a; double b; BigInteger c; String st;
a = cin.nextInt(); b = cin.nextDouble(); c = cin.nextBigInteger(); d = cin.nextLine(); // 每种类型都有相应的输入函数.
}
}
2. 输出
函数:System.out.print(); System.out.println(); System.out.printf();
System.out.print(); // cout << …;
System.out.println(); // cout << … << endl;
System.out.printf(); // 与C中的printf用法类似.
import java.io.*;
import java.math.*;
import java.util.*;
import java.text.*;
{
public static void main(String[] args)
{
Scanner cin = new Scanner (new BufferedInputStream(System.in));
int a; double b;
a = 12345; b = 1.234567;
System.out.println(a + " " + b);
System.out.printf("%d %10.5f\n", a, b); // 输入b为字宽为10,右对齐,保留小数点后5位,四舍五入.
}
}
函数:
// 这里0指一位数字,#指除0以外的数字(如果是0,则不显示),四舍五入.
DecimalFormat fd = new DecimalFormat("#.00#");
DecimalFormat gd = new DecimalFormat("0.000");
System.out.println("x =" + fd.format(x));
System.out.println("x =" + gd.format(x));
3. 字符串处理
java中字符串String是不可以修改的,要修改只能转换为字符数组.
import java.io.*;
import java.math.*;
import java.util.*;
import java.text.*;
{
public static void main(String[] args)
{
int i;
Scanner cin = new Scanner (new BufferedInputStream(System.in));
String st = "abcdefg";
System.out.println(st.charAt(0)); // st.charAt(i)就相当于st[i].
char [] ch;
ch = st.toCharArray(); // 字符串转换为字符数组.
for (i = 0; i < ch.length; i++) ch[i] += 1;
System.out.println(ch); // 输入为“bcdefgh”.
if (st.startsWith("a")) // 如果字符串以'0'开头.
{
st = st.substring(1); // 则从第1位开始copy(开头为第0位).
}
}
}
4. 高精度
BigInteger和BigDecimal可以说是acmer选择java的首要原因。
函数:add, subtract, divide, mod, compareTo等,其中加减乘除模都要求是BigInteger(BigDecimal)和BigInteger(BigDecimal)之间的运算,所以需要把int(double)类型转换为BigInteger(BigDecimal),用函数BigInteger.valueOf().
import java.io.*;
import java.math.*;
import java.util.*;
import java.text.*;
{
public static void main(String[] args)
{
Scanner cin = new Scanner (new BufferedInputStream(System.in));
int a = 123, b = 456, c = 7890;
BigInteger x, y, z, ans;
x = BigInteger.valueOf(a); y = BigInteger.valueOf(b); z = BigInteger.valueOf(c);
ans = x.add(y); System.out.println(ans);
ans = z.divide(y); System.out.println(ans);
ans = x.mod(z); System.out.println(ans);
if (ans.compareTo(x) == 0) System.out.println("1");
}
}
5. 进制转换
java很强大的一个功能。
函数:
String st = Integer.toString(num, base); // 把num当做10进制的数转成base进制的st(base <= 35).
int num = Integer.parseInt(st, base); // 把st当做base进制,转成10进制的int(parseInt有两个参数,第一个为要转的字符串,第二个为说明是什么进制).
BigInter m = new BigInteger(st, base); // st是字符串,base是st的进制.
6. 排序
函数:Arrays.sort();至于怎么排序结构体,像C++里写个cmp的方法,在java还不太清楚,希望有人指点下~~
import java.io.*;
import java.math.*;
import java.util.*;
import java.text.*;
{
public static void main(String[] args)
{
Scanner cin = new Scanner (new BufferedInputStream(System.in));
int n = cin.nextInt();
int a[] = new int [n];
for (int i = 0; i < n; i++) a[i] = cin.nextInt();
Arrays.sort(a);
for (int i = 0; i < n; i++) System.out.print(a[i] + " ");
}
}
7. POJ高精度题目汇总:
POJ 1131 1205 1220 1405 1503 1604 1894 2084 2305 2325 2389 2413 3101 3199
2022年9月28日 00:59
Subject expert of your class teacher is provided with the model sample question bank for all chapters with practice study material for Paper-1 and Paper-2 examination tests as per Part-1 and Part-2 syllabus books, Assam HSLC Mathematics Model Paper all SEBA board HSLC and AHM students can download SEBA Mathematics Model Paper 2023 Pdf to guessing important questions for all Unit Tests, Three Months Exams (Quarterly), Six Months Exams (Half Yearly), Pre-Final and annual final public examination tests.
2023年7月22日 23:01
CG Board Class Teachers need to have a Complete and Deeper Understanding about the Children they are Expected to Teach, This Page Available Links From Where you can Download the Chhattisgarh Class Syllabus 2024 of Maths and Environmental Studies in English and Hindi Medium in Pdf File Format Provided in the Table with the Article are useful to Understand the Concepts CGBSE 10th Class Syllabus 2024 well and also to Clear any Doubts in These Subjects, Class Exam Date Sheet 2024 Available at Official Website, Every Year This High School Final Exam Conducted Month of April, so Students Download CG Board 10th class Syllabus 2024 Regular Reading and Fallow for Final Exam Better Performance
2024年1月18日 01:29
펀초이스 - The secret to choosing the perfect massage business, tailored to your preferences.
2024年1月19日 21:49
Pavzi.com is a startup by passionate webmasters and bloggers who have a passion for providing engaging content that is accurate, interesting, and worthy to read. pavzi.com We are more like a web community where you can find different information, resources, and topics on day-to-day incidents or news. We provide you with the finest web content on every topic possible with the help of the editorial and content team.