一、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:读入整数

  1. Input  输入数据有多组,每组占一行,由一个整数组成。
  2. Sample Input
  3. 56
  4. 67
  5. 100
  6. 123
  7. import java.util.Scanner;
  8. public class Main {
  9. public static void main(String[] args) {
  10. Scanner sc =new Scanner(System.in);
  11. while(sc.hasNext()){  //判断是否结束
  12. int score = sc.nextInt();//读入整数
  13. 。。。。
  14. }
  15. }
  16. }

例2:读入实数

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

  1. Sample Input
  2. 4
  3. 56.9  67.7  90.5  12.8
  4. 5
  5. 56.9  67.7  90.5  12.8
  6. import java.util.Scanner;
  7. public class Main {
  8. public static void main(String[] args) {
  9. Scanner sc =new Scanner(System.in);
  10. while(sc.hasNext()){
  11. int n = sc.nextInt();
  12. for(int i=0;i<n;i++){
  13. double a = sc.nextDouble();
  14. 。。。。。。
  15. }
  16. }
  17. }
  18. }

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

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

  1. Sample Input
  2. 2
  3. asdfasdf123123asdfasdf
  4. asdf111111111asdfasdfasdf
  5. import java.util.Scanner;
  6. public class Main {
  7. public static void main(String[] args) {
  8. Scanner sc = new Scanner(System.in);
  9. int n = sc.nextInt();
  10. for(int i=0;i<n;i++){
  11. String str = sc.next();
  12. ......
  13. }
  14. }
  15. }
  16. import java.util.Scanner;
  17. public class Main {
  18. public static void main(String[] args) {
  19. Scanner sc = new Scanner(System.in);
  20. int n = Integer.parseInt(sc.nextLine());
  21. for(int i=0;i<n;i++){
  22. String str = sc.nextLine();
  23. ......
  24. }
  25. }
  26. }

例3:读入字符串【杭电2005 第几天?】

  1. 给定一个日期,输出这个日期是该年的第几天。
  2. Input  输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成
  3. 1985/1/20
  4. 2006/3/12
  5. import java.util.Scanner;
  6. public class Main {
  7. public static void main(String[] args) {
  8. Scanner sc = new Scanner(System.in);
  9. int[] dd = {0,31,28,31,30,31,30,31,31,30,31,30,31};
  10. while(sc.hasNext()){
  11. int days = 0;
  12. String str = sc.nextLine();
  13. String[] date = str.split("/");
  14. int y = Integer.parseInt(date[0]);
  15. int m = Integer.parseInt(date[1]);
  16. int d = Integer.parseInt(date[2]);
  17. if((y%400 == 0 || (y%4 == 0 && y%100 !=0)) && m>2) days ++;
  18. days += d;
  19. for(int i=0;i<m;i++){
  20. days += dd[i];
  21. }
  22. System.out.println(days);
  23. }
  24. }
  25. }

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) System.out.println(a / b);
  18. else System.out.format("%.2f", (a / (1.0*b))). Println();
  19. }
  20. }
  21. }
  22. }

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

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写法入门

    打2017icpc沈阳站的时候遇到了大数的运算,发现java与c++比起来真的很赖皮,竟然还有大数运算的函数,为了以后打比赛更快的写出大数的算法并且保证不错,特意在此写一篇博客, 记录java的大数运 ...

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

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

  3. ACM录 之 输入输出。

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

  4. Java基本输入输出

    Java基本输入输出 基本输入 基本输出 package com.ahabest.demo; public class Test { public static void main(String[] ...

  5. ACM中Java高效输入输出封装

    来自互联网 : 既高效又好用才是王道! import java.io.IOException; import java.io.FileInputStream; import java.io.Input ...

  6. acm java入门(转载)

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

  7. JAVA控制台输入输出方法总结

    java的控制台输入输出有很多方法,此文分别对其进行介绍. 1.控制台的输入 关于控制台的输入主要介绍三种方法,第一种方法使用BufferedReader获得控制台输入的数据,此方法是传统的输入方法, ...

  8. Java一点输入输出技巧

    输入: 格式1:Scanner sc = new Scanner(System.in); 格式2:Scanner sc = new Scanner(new BufferedInputStream(Sy ...

  9. java socket输入输出中文乱码问题

    http://hi.baidu.com/linjk03/item/e2028bfd990c14ea1a111feb 统一了输入输出的编码格式,是不会有乱码问题出现的.   构造Reader或Write ...

随机推荐

  1. footable动态载入数据

    footable_redraw事件 $('#scan').on('click',function(){ var html = '<tr><td>mayidudu</td& ...

  2. CSS小记

    1.元素居中 (1)水平居中:指定宽度,然后margin auto 即可 .middle{ max-width:400px; //width:400px;//当浏览器被缩小,宽度小于元素宽度时,元素会 ...

  3. UML(5)——协作图

    协作图中表示了角色之间的关系,通过协作图限定协作中的对象或链.协作指的是在一定的语境中一组对象以及实现某些行为的对象间的相互作用. 协 作图是表现对象协作关系的图,表示了协作中作为各种类元角色的对象所 ...

  4. lamp环境centos6.4

    http://www.centos.bz/2011/09/centos-compile-lamp-apache-mysql-php/comment-page-1/#comments 编译安装: 首先卸 ...

  5. Codeforces Round #270 D C B A

    谈论最激烈的莫过于D题了! 看过的两种做法不得不ORZ,特别第二种,简直神一样!!!!! 1th:构造最小生成树. 我们提取所有的边出来按边排序,因为每次我们知道边的权值>0, 之后每次把边加入 ...

  6. ExtJS学习之路第五步:认识最常见组件Panel

    文档中描述 Panel(面板)是一个容器,它具有特定的功能和结构部件,这使它成为面向应用用户界面的完美基石.面板,继承自Ext.container.Container,能够配置布局以及子组件(Chil ...

  7. java笔记--关于线程通信

    关于线程通信 使用多线程编程的一个重要原因就是线程间通信的代价比较小 --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3897773.h ...

  8. 关于showModalDialog()对话框点击按钮弹出新页面的问题

    页面a.aspx上,单击按钮a,走脚本,弹出showModalDialog("b.aspx",....) 在b.aspx上有个服务器控件按钮b,单击按钮,更新数据后,会弹出一个新的 ...

  9. codeforces 258div2 B Sort the Array

    题目链接:http://codeforces.com/contest/451/problem/B 解题报告:给出一个序列,要你判断这个序列能不能通过将其中某个子序列翻转使其成为升序的序列. 我的做法有 ...

  10. unity3d中资源文件从MAX或者MAYA中导出的注意事项

    原地址:http://blog.sina.com.cn/s/blog_6ad33d3501011ekx.html 之前在项目中,没有怎么接触过美术的软件(之前的美术团队很犀利,被他们宠坏了).在自己公 ...