ACMのjava入門
11500 ワード
一.入力
二.しゅつりょく
三.文字列処理
四.高精度
五.しんしんへんかん
六.配列ソート
import java.io.BufferedInputStream;
import java.util.Scanner;
/**
* 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()
* @author admin
*
*/
public class InputExample {
public static void main(String args[]){
//exampleOne();
//exampleTwo();
//exampleThree();
exampleFour();
}
/**
* 1:
* Input , , 。
* Sample Input
* 56
* 67
* 100
* 123
*/
private static void exampleOne(){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int score = sc.nextInt();
System.out.println(score);
}
}
/**
* 2:
* , 2 , N, N 。
* Sample Input
* 4
* 56.9 67.7 90.5 12.8
* 5
* 56.9 67.7 90.5 12.8
*/
public static void exampleTwo(){
Scanner sc = new Scanner(new BufferedInputStream(System.in));
while(sc.hasNext()){
int n = sc.nextInt();
for(int i = 0; i < n; i++){
double a = sc.nextDouble();
System.out.println(a);
}
}
}
/**
* 3: 【 2017 】
* , n, , n , 。
* Sample Input
* 2
* asdfasdf123123asdfasdf
* asdf111111111asdfasdfasdf
*/
public static void exampleThree(){
Scanner sc = new Scanner(new BufferedInputStream(System.in));
while(sc.hasNext()){
int n = sc.nextInt();
for(int i = 0; i < n; i++){
String s = sc.next();
System.out.println(s);
}
}
}
/**
* 3: 【 2005 ?】
* , 。
* Input , , YYYY/MM/DD
* 1985/1/20
* 2006/3/12
*/
public static void exampleFour(){
Scanner sc = new Scanner(new BufferedInputStream(System.in));
while(sc.hasNext()){
int md[] = {31, 27, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String str[] = sc.next().split("/");
int year = Integer.parseInt(str[0]);
int month = Integer.parseInt(str[1]);
int day = Integer.parseInt(str[2]);
int days = 0;
for(int i = 0; i < month - 1; i++) days += md[i];
days += day;
if( month>2 && ( year%400 == 0 || (year%100 != 0 && year%4 == 0))) days++;
System.out.println(days);
}
}
}
二.しゅつりょく
import java.io.BufferedInputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Scanner;
/**
* :
* System.out.print();
* System.out.println();
* System.out.format();
* System.out.printf();
* @author admin
*
*/
public class OutputExample {
public static void main(String[] args) {
//exampleOne();
exampleTwo();
}
/**
* 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
*/
private static void exampleOne(){
Scanner sc = new Scanner(new BufferedInputStream(System.in));
while(sc.hasNext()){
int t = sc.nextInt();
for(int times = 0; times < t; times++){
String c = sc.next();
int a = sc.nextInt();
int b = sc.nextInt();
if(c.equals("+")) System.out.println(a+b);
else if(c.equals("-")) System.out.println(a-b);
else if(c.equals("*")) System.out.println(a*b);
else System.out.format("%.2f", (a*1.0)/b).println();
}
}
}
/**
* :
* :
* 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));
*/
private static void exampleTwo(){
NumberFormat formatter = new DecimalFormat( "000000");
String s = formatter.format(-1234.567); // -001235
System.out.println(s);
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");
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");
s = formatter.format(-.567); // -0.57
System.out.println(s);
formatter = new DecimalFormat( "#.#");
s = formatter.format(-1234.567); // -1234.6
System.out.println(s);
formatter = new DecimalFormat( "#.######");
s = formatter.format(-1234.567); // -1234.567
System.out.println(s);
formatter = new DecimalFormat( ".######");
s = formatter.format(-1234.567); // -1234.567
System.out.println(s);
formatter = new DecimalFormat( "#.000000");
s = formatter.format(-1234.567); // -1234.567000
System.out.println(s);
formatter = new DecimalFormat( "#,###,###");
s = formatter.format(-1234.567); // -1,235
System.out.println(s);
s = formatter.format(-1234567.890); // -1,234,568
System.out.println(s);
// The ; symbol is used to specify an alternate pattern for negative values
formatter = new DecimalFormat( "#;(#) ");
s = formatter.format(-1234.567); // (1235)
System.out.println(s);
// The ' symbol is used to quote literal symbols
formatter = new DecimalFormat( " '# '# ");
s = formatter.format(-1234.567); // - # 1235
System.out.println(s);
formatter = new DecimalFormat( " 'abc '# ");
s = formatter.format(-1234.567); // - abc 1235
System.out.println(s);
formatter = new DecimalFormat( "#.##%");
s = formatter.format(-12.5678987);
System.out.println(s);
}
}
三.文字列処理
/**
* 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 。
* @author admin
*
*/
public class StringExample {
}
四.高精度
import java.io.BufferedInputStream;
import java.math.BigInteger;
import java.util.Scanner;
/**
* BigInteger BigDecimal acmer java 。
* :add, subtract, divide, mod, compareTo ,
* BigInteger(BigDecimal) BigInteger(BigDecimal) ,
* int(double) BigInteger(BigDecimal), BigInteger.valueOf().
* @author admin
*
*/
public class HighPrecision {
public static void main(String[] args) {
Scanner cin = new Scanner (new BufferedInputStream(System.in));
int a = 123, b = 456, c = 7890;
BigInteger x, y, z, ans;
x = BigInteger.valueOf(a);
y = BigInteger.valueOf(b);
z = BigInteger.valueOf(c);
ans = x.add(y); System.out.println(ans);
ans = z.divide(y); System.out.println(ans);
ans = x.mod(z); System.out.println(ans);
if (ans.compareTo(x) == 0) System.out.println("1");
}
}
五.しんしんへんかん
/**
* java 。
* :
* 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 .
* @author admin
*
*/
public class HexadecimalConversionExamp {
}
六.配列ソート
import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.Scanner;
/**
* :Arrays.sort();
* @author admin
*
*/
public class ArraysSortExample {
public static void main(String[] args) {
Scanner cin = new Scanner (new BufferedInputStream(System.in));
int n = cin.nextInt();
int a[] = new int [n];
for (int i = 0; i < n; i++) a[i] = cin.nextInt();
Arrays.sort(a);
for (int i = 0; i < n; i++) System.out.print(a[i] + " ");
for (int i = n - 1; i >= 0; i--) System.out.print(a[i] + " ");
}
}