本文转自:ACM之Java输入输出


一、Java之ACM注意点

1. 类名称必须采用public class Main方式命名

2. 在有些OJ系统上,即便是输出的末尾多了一个“ ”,程序可能会输出错误,所以在我看来好多OJ系统做的是非常之垃圾

3. 有些OJ上的题目会直接将OI上的题目拷贝过来,所以即便是题目中有输入和输出文件,可能也不需要,因为在OJ系统中一般是采用标准输入输出,不需要文件

4. 在有多行数据输入的情况下,一般这样处理,

  1. static Scanner in = new Scanner(System.in);
  2. while(in.hasNextInt())
  3. 或者是
  4. while(in.hasNext())

5. 有关System.nanoTime()函数的使用,该函数用来返回最准确的可用系统计时器的当前值,以毫微秒为单位。

  1. long startTime = System.nanoTime();
  2. // ... the code being measured ...
  3. long estimatedTime = System.nanoTime() - startTime;

二、Java之输入输出处理

由于ACM竞赛题目的输入数据和输出数据一般有多组(不定),并且格式多种多样,所以,如何处理题目的输入输出是对大家的一项最基本的要求。这也是困扰初学者的一大问题。

1. 输入:

格式1:Scanner sc = new Scanner (new BufferedInputStream(System.in));

格式2:Scanner sc = new Scanner (System.in);

在读入数据量大的情况下,格式1的速度会快些。

读一个整数: int n = sc.nextInt(); 相当于 scanf("%d", &n); 或 cin >> n;

读一个字符串:String s = sc.next(); 相当于 scanf("%s", s); 或 cin >> s;

读一个浮点数:double t = sc.nextDouble(); 相当于 scanf("%lf", &t); 或 cin >> t;

读一整行: String s = sc.nextLine(); 相当于 gets(s); 或 cin.getline(...);

判断是否有下一个输入可以用sc.hasNext()或sc.hasNextInt()或sc.hasNextDouble()或sc.hasNextLine()

例1:读入整数

Input  输入数据有多组,每组占一行,由一个整数组成。 
Sample Input 
56
67
100
123

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. while (sc.hasNext()) { // 判断是否结束
  6. int score = sc.nextInt();// 读入整数
  7. <span style="white-space:pre">            </span>。。。
  8. }
  9. }
  10. }

例2:读入实数

输入数据有多组,每组占2行,第一行为一个整数N,指示第二行包含N个实数。

Sample Input

56.9  67.7  90.5  12.8 

56.9  67.7  90.5  12.8

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. while (sc.hasNext()) {
  6. int n = sc.nextInt();
  7. for (int i = 0; i < n; i++) {
  8. double a = sc.nextDouble();
  1. <span style="white-space:pre">                </span>。。。
  2. }
  3. }
  4. }
  5. }

例3:读入字符串【杭电2017 字符串统计】

输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。

Sample Input  
2
asdfasdf123123asdfasdf
asdf111111111asdfasdfasdf

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. int n = sc.nextInt();
  6. for (int i = 0; i < n; i++) {
  7. String str = sc.next();
  8. }
  9. }
  10. }
  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. int n = Integer.parseInt(sc.nextLine());
  6. for (int i = 0; i < n; i++) {
  7. String str = sc.nextLine();
  8. }
  9. }
  10. }

例3:读入字符串【杭电2005 第几天?】
给定一个日期,输出这个日期是该年的第几天。 
Input  输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成
1985/1/20
2006/3/12

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. int[] dd = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  6. while (sc.hasNext()) {
  7. int days = 0;
  8. String str = sc.nextLine();
  9. String[] date = str.split("/");
  10. int y = Integer.parseInt(date[0]);
  11. int m = Integer.parseInt(date[1]);
  12. int d = Integer.parseInt(date[2]);
  13. if ((y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)) && m > 2)
  14. days++;
  15. days += d;
  16. for (int i = 0; i < m; i++) {
  17. days += dd[i];
  18. }
  19. System.out.println(days);
  20. }
  21. }
  22. }

2. 输出

函数:

System.out.print();

System.out.println();

System.out.format();

System.out.printf();

例4 杭电1170Balloon Comes!

Give you an operator (+,-,*, / --denoting addition, subtraction, multiplication, division respectively) and two positive integers, your task is to output the result.

Input

Input contains multiple test cases. The first line of the input is a single integer T (0<T<1000) which is the number of test cases. T test cases follow. Each test case contains a char C (+,-,*, /) and two integers A and B(0<A,B<10000).Of course, we all know that A and B are operands and C is an operator.

Output

For each case, print the operation result. The result should be rounded to 2 decimal places If and only if it is not an integer.

Sample Input

4

+ 1 2

- 1 2

* 1 2

/ 1 2

Sample Output

3

-1

2

0.50

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. int n = sc.nextInt();
  6. for (int i = 0; i < n; i++) {
  7. String op = sc.next();
  8. int a = sc.nextInt();
  9. int b = sc.nextInt();
  10. if (op.charAt(0) == '+') {
  11. System.out.println(a + b);
  12. } else if (op.charAt(0) == '-') {
  13. System.out.println(a - b);
  14. } else if (op.charAt(0) == '*') {
  15. System.out.println(a * b);
  16. } else if (op.charAt(0) == '/') {
  17. if (a % b == 0)
  18. System.out.println(a / b);
  19. else
  20. System.out.format("%.2f", (a / (1.0 * b))).Println();
  21. }
  22. }
  23. }
  24. }

3. 规格化的输出:
函数:
// 这里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));

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. NumberFormat formatter = new DecimalFormat("000000");
  5. String s = formatter.format(-1234.567); // -001235
  6. System.out.println(s);
  7. formatter = new DecimalFormat("##");
  8. s = formatter.format(-1234.567); // -1235
  9. System.out.println(s);
  10. s = formatter.format(0); // 0
  11. System.out.println(s);
  12. formatter = new DecimalFormat("##00");
  13. s = formatter.format(0); // 00
  14. System.out.println(s);
  15. formatter = new DecimalFormat(".00");
  16. s = formatter.format(-.567); // -.57
  17. System.out.println(s);
  18. formatter = new DecimalFormat("0.00");
  19. s = formatter.format(-.567); // -0.57
  20. System.out.println(s);
  21. formatter = new DecimalFormat("#.#");
  22. s = formatter.format(-1234.567); // -1234.6
  23. System.out.println(s);
  24. formatter = new DecimalFormat("#.######");
  25. s = formatter.format(-1234.567); // -1234.567
  26. System.out.println(s);
  27. formatter = new DecimalFormat(".######");
  28. s = formatter.format(-1234.567); // -1234.567
  29. System.out.println(s);
  30. formatter = new DecimalFormat("#.000000");
  31. s = formatter.format(-1234.567); // -1234.567000
  32. System.out.println(s);
  33. formatter = new DecimalFormat("#,###,###");
  34. s = formatter.format(-1234.567); // -1,235
  35. System.out.println(s);
  36. s = formatter.format(-1234567.890); // -1,234,568
  37. System.out.println(s);
  38. // The ; symbol is used to specify an alternate pattern for negative
  39. // values
  40. formatter = new DecimalFormat("#;(#) ");
  41. s = formatter.format(-1234.567); // (1235)
  42. System.out.println(s);
  43. // The ' symbol is used to quote literal symbols
  44. formatter = new DecimalFormat(" '# '# ");
  45. s = formatter.format(-1234.567); // -#1235
  46. System.out.println(s);
  47. formatter = new DecimalFormat(" 'abc '# ");
  48. s = formatter.format(-1234.567); // - abc 1235
  49. System.out.println(s);
  50. formatter = new DecimalFormat("#.##%");
  51. s = formatter.format(-12.5678987);
  52. System.out.println(s);
  53. }
  54. }

4. 字符串处理 String

String 类用来存储字符串,可以用charAt方法来取出其中某一字节,计数从0开始:

String a = "Hello"; // a.charAt(1) = 'e'

用substring方法可得到子串,如上例

System.out.println(a.substring(0, 4)) // output "Hell"

注意第2个参数位置上的字符不包括进来。这样做使得 s.substring(a, b) 总是有 b-a个字符。

字符串连接可以直接用 + 号,如

String a = "Hello";

String b = "world";

System.out.println(a + ", " + b + "!"); // output "Hello, world!"

如想直接将字符串中的某字节改变,可以使用另外的StringBuffer类。

5. 高精度
BigInteger和BigDecimal可以说是acmer选择java的首要原因。
函数:add, subtract, divide, mod, compareTo等,其中加减乘除模都要求是BigInteger(BigDecimal)和BigInteger(BigDecimal)之间的运算,所以需要把int(double)类型转换为BigInteger(BigDecimal),用函数BigInteger.valueOf().

  1. import java.io.BufferedInputStream;
  2. import java.math.BigInteger;
  3. import java.util.Scanner;
  4. public class Main {
  5. public static void main(String[] args)   {
  6. Scanner cin = new Scanner (new BufferedInputStream(System.in));
  7. int a = 123, b = 456, c = 7890;
  8. BigInteger x, y, z, ans;
  9. x = BigInteger.valueOf(a);
  10. y = BigInteger.valueOf(b);
  11. z = BigInteger.valueOf(c);
  12. ans = x.add(y); System.out.println(ans);
  13. ans = z.divide(y); System.out.println(ans);
  14. ans = x.mod(z); System.out.println(ans);
  15. if (ans.compareTo(x) == 0) System.out.println("1");
  16. }
  17. }

6. 进制转换
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的进制.

7. 数组排序
函数:Arrays.sort();

    1. public class Main {
    2. public static void main(String[] args)    {
    3. Scanner cin = new Scanner (new BufferedInputStream(System.in));
    4. int n = cin.nextInt();
    5. int a[] = new int [n];
    6. for (int i = 0; i < n; i++) a[i] = cin.nextInt();
    7. Arrays.sort(a);
    8. for (int i = 0; i < n; i++) System.out.print(a[i] + " ");
    9. }
    10. }

ACM之Java输入输出的更多相关文章

  1. ACM之Java速成(2)

    acm中Java的应用 Chapter I. Java的优缺点各种书上都有,这里只说说用Java做ACM-ICPC的特点: (1) 最明显的好处是,学会Java,可以参加Java Challenge ...

  2. ACM中java的使用

    ACM中java的使用 转载自http://www.cnblogs.com/XBWer/archive/2012/06/24/2560532.html 这里指的java速成,只限于java语法,包括输 ...

  3. ACM中java的使用 (转)

    ACM中java的使用 这里指的java速成,只限于java语法,包括输入输出,运算处理,字符串和高精度的处理,进制之间的转换等,能解决OJ上的一些高精度题目. 1. 输入: 格式为:Scanner ...

  4. [ACM训练] ACM中巧用文件的输入输出来改写acm程序的输入输出 + ACM中八大输入输出格式

    ACM中巧用文件的输入输出来改写acm程序的输入输出 经常有见大神们使用文件来代替ACM程序中的IO,尤其是当程序IO比较复杂时,可以使自己能够更专注于代码的测试,而不是怎样敲输入. C/C++代码中 ...

  5. ACM之Java速成(4)

    ACM中Java.进制转换 Java进制转换: 由于Unicode兼容ASCII(0-255),因此,上面得到的Unicode就是ASCII. java中进行二进制,八进制,十六进制,十进制间进行相互 ...

  6. ACM之Java速成(3)

    ACM中Java.大数处理 先上个代码: import java.math.*; import java.util.*; class Main{ public static void main(Str ...

  7. java 输入输出 io

    学习JAVA  输入输出篇 java不像C中拥有scanf这样功能强大的函数,大多是通过定义输入输出流对象.常用的类有BufferedReader,Scanner.实例程序:一,利用 Scanner ...

  8. ACM录 之 输入输出。

    —— 简单介绍一下ACM里面的输入输出... —— 主要说C++的输入输出(其实其他的我不会...). —— C++里面有输入输出流,也就是cin和cout,用起来也算是比较方便吧... —— 但是, ...

  9. Java输入输出小结

    无论使用哪一种编程语言,输入输出都是我们首当其冲的,因此简单整理了 一下关于Java输入输出知识点,还有些内容摘自其它博客,忘见谅. 第一部分,让我们看一下Java的输出 public class M ...

随机推荐

  1. Echarts 学习系列(1)-5分钟上手ECharts

    目录 写在前面 下载Echarts和主题 绘制一个简单的图表 写在前面 最近,在做某个项目的时候.需要使用的可视化的图表显数据.最后,选择了百度的Echarts. 下载Echarts和主题 1.获取E ...

  2. 完全图解 HTTPS

    安全基础 我们先来看下数据在互联网上数据传递可能会出现的三个比较有代表性的问题,其实后面提到的所有方法,都是围绕解决这三个问题而提出来的. 窃听 伪造 否认 对称密钥加密 假设 A 正在通过互联网向  ...

  3. Nginx优化配置,轻松应对高并发

    Nginx现在已经是最火的web服务器之一,尤其在静态分离和负载均衡方面,性能十分优越.接下来我们主要看下Nginx在高并发环境下的优化配置,主要是针对 nginx.conf 文件的属性设置.我们打开 ...

  4. 记一次Spring boot集成mybatis错误修复过程 Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

    最近自己写了一份代码签入到github,然后拉下来运行报下面的错误 Error starting ApplicationContext. To display the conditions repor ...

  5. iOS - 架构的认识过程,悬崖勒马。

    16年的时候写过一篇代码讲解的,依旧是这三种架构,现在20年将近了,看到好的文章,是否增加新的认识. 16年链接 iOS - 架构模式 - 解密 MVC.MVP.MVVM.VIPER架构 新项目选择架 ...

  6. Django:RestFramework之-------路由

    11.路由 路由设置: url(r'^(?P<version>[v1|v2]+)/vview\.(?P<format>\w+)$', views.VView.as_view({ ...

  7. php 根据URL下载远程图片、压缩包、pdf等文件到本地

    1.此方法可以下载图片.压缩包.pdf(亲测),应该所有类型的文件都可以下载到本地,可以试一下 //远程路径,名称,文件后缀 function downImgRar($url,$rename,$ext ...

  8. Ubuntu中wine程序安装windows软件中文乱码如何解决

    1.安装wine sudo apt install wine 2.安装中文程序方法 下载exe文件 在命令行执行 wine 文件名.exe 3.中文乱码原因分析 查看/home/用户名/.wine/d ...

  9. go build -tags 的使用

    go build 使用tag来实现编译不同的文件 go-tooling-workshop 中关于go build的讲解可以了解到go bulid的一些用法,这篇文章最后要求实现一个根据go bulid ...

  10. Django之form主键

    Form介绍 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是否 ...