1.类名称必须采用public class Main方式命名
2.多组输入,读取到文件尾
Scanner scan=new Scanner(System.in);
while(scan.hasNext())
或scan.hasNextInt()或scan.hasNextDouble()或scan.hasNextLine()
3.输入输出
语法1:Scanner scan = new Scanner (new BufferedInputStream(System.in));//import java.io.BufferedInputStream;
语法2:Scanner scan=new Scanner(System.in);
读入数据多的时候,用语法1比较快
读整数:int n = scan.nextInt(); = scanf("%d",&n); = cin>>n;
读小数:double f = scan.nextDouble(); = scanf("%lf",&f); = cin>>f;
读字符串:String s = scan.next(); = cin >> s;
读一整行:String s = sc.next(); 相当于 scanf("%s", s); 或 cin >> s;
有一些题目是有读入字符的,hdu2005-第几天?(多组输入,YYYY/MM/DD格式)
用字符串读入再分割。
import java.math.BigInteger;//操作大整数
import java.math.BigDecimal;//操作大小数
import java.io.BufferedInputStream;
import java.util.Scanner;
import java.util.Arrays;
public class Main//hdu2005
{
public static void main(String []args)
{
Scanner scan = new Scanner(System.in); while( scan.hasNext())
{
int a[]={0,31,59,90,120,151,181,212,243,273,304,334};//java不能int a[12]
String str = scan.nextLine();
String date[] = str.split("/");
int year = Integer.parseInt(date[0]);
int month = Integer.parseInt(date[1]);
int day = Integer.parseInt(date[2]);
int sum=0;
if(year%400==0 || ( year%4==0 && year%100!=0))
a[2]++;
sum = a[month-1]+day;
System.out.println(sum);
}
}
}
4.大数类(java杀招)
import java.math.BigInteger;//操作大整数
import java.math.BigDecimal;//操作大小数
大数不能用简单的加减乘除符号,需要调用方法。
import java.math.BigInteger;//操作大整数
import java.math.BigDecimal;//操作大小数
import java.io.BufferedInputStream;
import java.util.Scanner;
import java.util.Arrays;
public class Main
{
public static void main(String []args)
{
BigInteger a,b,ans;
Scanner scan=new Scanner(System.in);
a=scan.nextBigInteger();
b=scan.nextBigInteger();
ans=a.add(b);//ans=a+b;
ans=a.subtract(b);//ans=a-b
ans=a.mod(b);//ans=a%b
ans=a.divide(b);//ans=a/b
ans=a.max(b);
ans=a.min(b);
ans=a.multiply(b);
}
}