ACM Java entry and basic skills

Source: Internet
Author: User
Tags api manual binary to decimal decimal to binary

Extracted from the internet!

1. Basic Input and Output:
(1) JDK 1.5.0 new classification class provides a good foundation for input, it is set for the ACM-ICPC.
General Usage:

import java.io.* 
import java.util.*
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(new BufferedInputStream(System.in));
}
}

Of course, you can also directly convert CIN = new into (system. In); but adding a buffer may be faster.
(2)
Read an integer: int n = cin. nextint (); equivalent to scanf ("% d", & N); CIN> N;
Read a string: String S = cin. Next (); equivalent to scanf ("% s", S); or CIN> S;
Read a floating point number: Double T = cin. nextdouble (); equivalent to scanf ("% lf", & T); or CIN> T;
Read the entire row: String S = cin. nextline (); equivalent to gets (s); or CIN. Getline (...);
You can use cin. hasnext (), Cin. hasnextint (), or CIN. hasnextdouble () to determine whether there is a next input. For details, see the toJ 1001 routine.
(3)
Generally, system. Out. Print () and system. Out. println () can be used for output directly. The former does not output line breaks, while the latter does.
For example: system. Out. println (n); // n is int type
Multiple integers can be output in the same row.
System. Out. println (New INTEGER (N). tostring () + "" + new INTEGER (M). tostring ());
You can also redefine it:
Static printwriter cout = new printwriter (New bufferedoutputstream (system. Out ));
Cout. println (N );
(4)
You can use the decimalformat class to retain a few decimal places in the output floating point,

import java.text.*; 
DecimalFormat f = new DecimalFormat("#.00#");
DecimalFormat g = new DecimalFormat("0.000");
double a = 123.45678, b = 0.12;
System.out.println(f.format(a));
System.out.println(f.format(b));
System.out.println(g.format(b));

Here 0 refers to a digit, and # refers to a Number other than 0.
2. Large numbers
Biginteger and bigdecimal are existing classes in the Java. Math package. The former represents an integer, and the latter represents a floating point.
Usage: you cannot directly use symbols such as + or-to use large numbers. For example:
(Import java. Math. *) // you need to introduce the java. Math package.

BigInteger a = BigInteger.valueOf(100); 
BigInteger b = BigInteger.valueOf(50);
BigInteger c = a.add(b) // c = a + b;

You can use the following methods:

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)

Use System. Out. println (A) to output a large number.
3. String

The string class is used to store strings. You can use the charat method to retrieve a certain byte. The count starts from 0:
String A = "hello"; // A. charat (1) = 'E'
Use the substring method to obtain the substring.
System. Out. println (A. substring (0, 4) // output "hell"
Note that the characters at the 2nd parameter locations are not included. In this way, S. substring (a, B) always contains B-A characters. You can use the plus sign (+) for string connection, as shown in figure
String A = "hello ";
String B = "world ";
System. Out. println (a + "," + B + "! "); // Output" Hello, world! "
If you want to directly change a byte in the string, you can use another stringbuffer class.

4. Call recursion (or other dynamic methods)
In the main class, the main method must be public static void. When a non-static class is called in main, a warning message is displayed. You can create an object and then call the method through the object:

public class Main {       void dfs(int a){           if (...) return;           dfs(a+1);       }       public static void main(String args[]) {           ...           Main e = new Main();           e.dfs(0);       } } 

5. Other Precautions
(1) Java is an object-oriented language, and the thinking methods need to be transformed. The functions in it are collectively referred to as methods. Do not make mistakes.
(2) there are some changes to the array in Java. In fact, the internal structure of multi-dimensional arrays is pointers. Therefore, Java does not support fill multi-dimensional arrays. The array must be initialized after definition, for example, int [] A = new int [100];
(3) The boolean type is Boolean and only has the values true and false. It must be boolean in the conditions of IF (...)/while (...) statements. In C/C ++, if (N % 2)... cannot be compiled in Java.
(4) The following methods of the arrays class in the Java. util package can replace memset, qsort/sort, and bsearch in C/C ++:
Arrays. Fill () arrays. Sort () arrays. binarysearch ()

6. Java hexadecimal conversion ~ Highlights

Unicode is compatible with ASCII (0 ~ 255). Therefore, the Unicode obtained above is ASCII.

Perform binary, octal, hexadecimal, and decimal conversion in Java

Integer. tohexstring (int I) decimal to hexadecimal

Integer. tooctalstring (int I) decimal to octal

Integer. tobinarystring (int I) decimal to binary

Integer. valueof ("ffff", 16). tostring () hexadecimal to decimal

Integer. valueof ("876", 8). tostring () octal decimal

Integer. valueof ("0101", 2). tostring () binary to decimal

As for conversion to binary or other binary, Java API provides convenient functions. You can check the Java API manual.

Take the ASCII character a as an example:

Int I = 'a ';
String ibin = integer. tobinarystring (I); // binary
String ihex = integer. tohexstring (I); // hexadecimal
String ioct = integer. tooctalstring (I); // octal
String iwokao = integer. tostring (I, 3); // The hexadecimal DEC of any of the following 35 hexadecimal values

Is there any way to directly convert 2, 8, and 16 to 10?

Java. Lang. Integer class

Parseint (string S, int Radix)

Use the base number specified by the second parameter to resolve the string parameter to a signed integer.

Examples from JDK:
Parseint ("0", 10) returns 0
Parseint ("473", 10) returns 473
Parseint ("-0", 10) returns 0
Parseint ("-FF", 16) returns-255
Parseint ("1100110", 2) returns 102
Parseint ("2147483647", 10) returns 2147483647
Parseint ("-2147483648", 10) returns-2147483648
Parseint ("2147483648", 10) throws a numberformatexception
Parseint ("99", 8) throws a numberformatexception
Parseint ("Kona", 10) throws a numberformatexception
Parseint ("Kona", 27) returns 411787.
How to Write (2, 8, 16) in hexadecimal conversion without Algorithms
Integer. tobinarystring
Integer. tooctalstring
Integer. tohexstring
Example 1:
Public class test {
Public static void main (string ARGs []) {
Int I = 100;
String binstr = integer. tobinarystring (I );
String otcstr = integer. tooctalstring (I );
String hexstr = integer. tohexstring (I );
System. Out. println (binstr );
Example 2:
Public class teststringformat {
Public static void main (string [] ARGs ){
If (ARGs. Length = 0 ){
System. Out. println ("Usage: Java teststringformat <a number> ");
System. Exit (0 );
}

Integer factor = integer. valueof (ARGs [0]);
String S;
S = string. Format ("% d", factor );
System. Out. println (s );
S = string. Format ("% x", factor );
System. Out. println (s );
S = string. Format ("% O", factor );
System. Out. println (s );
}

}

Convert various numeric types to string types:
String S = string. valueof (value); // The value is of any numeric type.

String type conversion to various numeric types:
String S = "169 ";
Byte B = byte. parsebyte (s );
Short T = short. parseshort (s );
Int I = integer. parseint (s );
Long L = long. parselong (s );
Float F = float. parsefloat (s );
Double D = double. parsedouble (s );

Conversions between numeric types and numeric objects:
Bytes B = 169;
Byte BO = new byte (B );
B = bo. bytevalue ();
Short T = 169;
Short to = new short (t );
T = to. aggregate value ();
Int I = 169;
B = bo. bytevalue ();
Short T = 169;
Short to = new short (t );
T = to. aggregate value ();

Int I = 169;
Integer IO = new INTEGER (I );
I = Io. intvalue ();

Long L = 169;
Long Lo = new long (L );
L = Lo. longvalue ();

Float F = 169f;
Float fo = new float (f );
F = fo. floatvalue ();

Double D = 169f;
Double dobj = new double (d );
D = dobj. doublevalue ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.