ACM之JAVA入门
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i=0;i<n;i++){ String str = sc.next(); ......
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 +12 -12 *12 /12 Sample Output 3 -1 2 0.50 import java.util.Scanner; public class Main {
} } 例 3:读入字符串【杭电 2017 字符串统计】 输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字 母和数字组成的字符串。 Sample Input 2 asdfasdf123123asdfasdf asdf111111111asdfasdfasdf
import java.util.Scanner; public class Main {
while(sc.hasNext()){ //判断是否结束 int score = sc.nextInt(); //读入整数 。。。。
} } } 例 2:读入实数
输入数据有多组,每组占 2 行,第一行为一个整数 N,指示第二行包含 N 个实数。 Sample Input 4 56.9 67.7 90.5 12.8 5 56.9 67.7 90.5 12.8
import java.util.Scanner; public class Main {
public static void main(String[] args) { Scanner sc =new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); for(int i=0;i<n;i++){ do); 。。。。。。 } }
formatter = new DecimalFormat( "##");
s = formatter.format(-1234.567);
// -1235
System.out.println(s);
s = formatter.format(0);
// 0
System.out.println(s);
formatter = new DecimalFormat( "##00");
public static void main(String[] args) { Scanner sc =new Scanner(System.in); int n = sc.nextInt(); for(int i=0;i<n;i++){ String op = sc.next(); int a = sc.nextInt(); int b = sc.nextInt(); if(op.charAt(0)=='+'){ System.out.println(a+b);
读一个整数: 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
import java.util.Scanner; public class Main {
public static void main(String[] args) { Scanner sc =new Scanner(System.in);
}else if(op.charAt(0)=='-'){ System.out.println(a-b);
}else if(op.charAt(0)=='*'){ System.out.println(a*b);
}else if(op.charAt(0)=='/'){ if(a % b == 0) System.out.println(a / b); else System.out.format("%.2f", (a / (1.0*b))). Println();
s = formatter.format(0);
// 00
System.out.println(s);
formatter = new DecimalFormat( ".00");
s = formatter.format(-.567);
// -.57
System.out.println(s);
formatter = new DecimalFormat( "0.00");
} } } }
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));
public static void main(String[] args) {
NumberFormat formatter = new DecimalFormat( "000000");
String s = formatter.format(-1234.567); // -001235
System.out.println(s);
/showproblem.php?pid=1092 /showproblem.php?pid=1093 /showproblem.php?pid=1094
2. 输出 函数:
} } } import java.util.Scanner; public class Main { public static void main(String[] args) {
Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); for(int i=0;i<n;i++){
String str = sc.nextLine(); ...... } } } 例3:读入字符串【杭电2005 第几天?】 给定一个日期,输出这个日期是该年的第几天。 Input 输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成 1985/1/20 2006/3/12 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] dd = {0,31,28,31,30,31,30,31,31,30,31,30,31}; while(sc.hasNext()){ int days = 0; String str = sc.nextLine(); String[] date = str.split("/"); int y = Integer.parseInt(date[0]); int m = Integer.parseInt(date[1]); int d = Integer.parseInt(date[2]); if((y%400 == 0 || (y%4 == 0 && y%100 !=0)) && m>2) days ++; days += d; for(int i=0;i<m;i++){