Java在ACM中的应用与注意事项

Java在ACM中的应用与注意事项

原文链接:https://www.xuebuyuan.com/3257862.html

0、基本框架

import java.oi.*;

import java.util.* 

public class Main 

{
    
     

public static void main(String args[]) 

{
    
     

Scanner cin = new Scanner(System.in));

} 

} 

1、读入数据直至文件结束

while (in.hasNext()) {
    
    
	int n = in.nextInt();
	System.out.println(n);
}

2、Java的输入

读一个整数: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(…);

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

3、Java的输出

输出一般可以直接用 System.out.print() 和 System.out.println(),前者不输出换行,而后者输出。

String name = s.nextLine();  
int age = s.nextInt();  
System.out.println("姓名:" + name + "  年龄:" + age );  
System.out.println();   //换行

4、浮点数输出

import java.text.*; 

public class Main

{
    
    

public static void main(String[] args)

{
    
    

DecimalFormat g = new DecimalFormat("0.00"); 

double a = 123.45678;

System.out.println(g.format(a)); 

}

}

//输出为123.46

5、Java的高精度

import java.math. // 需要引入 java.math 包
BigInteger a = BigInteger.valueOf(100);
BigInteger b = BigInteger.valueOf(50);
BigInteger c = a.add(b) // c = a + b;
//主要有以下方法可以使用:
BigInteger add(BigInteger other)
BigInteger subtract(BigInteger other)
BigInteger multiply(BigInteger other)
BigInteger divide(BigInteger other)
BigInteger mod(BigInteger other)
int compareTo(BigInteger other)
static BigInteger valueOf(long x)
*

切记:

BigInteger类不可进行±这样的运算,要调用其成员函数进行运算。

另外BigInteger类不是int 的扩展类型,所以不能把int类型值直接赋值给BigInteger。

BigInteger类型有自己的常量:

BigInteger.zero

BigInteger.one

6、Java的字符串

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!“

不可以通过charAt方法改变某一单个字符

7、Java的数组

数组的定义:

int[] a = new int[100];

数组的初始化

Arrays.fill(a,0);

相当于C语言中的 memset(a,0,sizeof(a));

8、一些注意事项

使用Netbeans写Java程序的时候用自动添加package main;交题的时候要去掉这句话。

主类必须命名为 Main

虽然Java功能很强大,但不能完全依赖他,毕竟C/C++还是ACM/ICPC的主流语言。有些题目无论Java怎么写都会超时,可以用Java计算出结果然后用C/C++打表提交。

交题时,你的代码应该是如下框架

import ………//相当于c++的include

public class Main {
    
    

    public static…….//一些自己定义的函数

    public static void main(String[] args) {
    
    

        Scanner cin = new Scanner(System.in);

    }

}

Java的语法与c/c++还是很相似的,所以。。放心写吧。。

/*

Java输入加速

用BufferedReader和StringTokenizer代替Scanner
   */
    
    import java.io.*;
    
    import java.util.*;
    
    public class Main {
    
    
    
        public static void main(String[] args) throws IOException {
    
    
    
         Reader Reader =new Reader(System.in);
    
         double x = Reader.nextDouble();
    
         int n = Reader.nextInt();
    
         String str = Reader.next();
    
        }
    
    }
    
    class Reader {
    
    
    
        final BufferedReader reader;
    
        StringTokenizer tokenizer;
    
        public Reader(InputStream input) {
    
    
    
            reader = new BufferedReader(new InputStreamReader(input));
    
            tokenizer = new StringTokenizer("");
    
        }
    
        public String next() throws IOException {
    
    
    
            while (!tokenizer.hasMoreTokens())
    
                tokenizer = new StringTokenizer(reader.readLine());
    
            return tokenizer.nextToken();
    
        }
    
        public int nextInt() throws IOException {
    
    
    
            return Integer.parseInt(next());
    
        }
    
        public double nextDouble() throws IOException {
    
    
    
            return Double.parseDouble(next());
    
        }
    
    }

猜你喜欢

转载自blog.csdn.net/qq_40534166/article/details/99346591