Application of Java in ACM

Source: Internet
Author: User
Tags sorted by name

 

Input: It can be input from the file or the console.

Import Java. io. bufferedinputstream; import Java. io. file; import Java. util. summary; public class demo {public static void main (string [] ARGs) throws exception {summary CIN = NULL; CIN = new example (new file ("data. in "); // read from the file // CIN = new bytes (New bufferedinputstream (system. in); // enter int A =-1 from the console; while (CIN. hasnext () {A = cin. nextint (); system. out. println (a);} cin. close ();}}

Cin. nexttype (); // The number of Certain types, where type = double, float, byte, short, Int, long, Boolean, biginteger, bigdecimal
Cin. Next (); // If a space is returned, a string is obtained.
Cin. nextline (); // returns a line break.

The largest number of applications should be in Java:

Biginteger

// The following are the addition, subtraction, multiplication, division, and remainder public static biginteger add (biginteger A, biginteger B) {return. add (B);} public static biginteger subtract (biginteger A, biginteger B) {return. subtract (B);} public static biginteger multiply (biginteger A, biginteger B) {return. multiply (B);} public static biginteger divide (biginteger A, biginteger B) {return. divide (B);} public static biginteger Mod (biginteger A, biginteger B) {return. moD (B );}

Bigdecimal

// Add, subtract, multiply, and except public static bigdecimal add (bigdecimal A, bigdecimal B) {return. add (B);} public static bigdecimal subtract (bigdecimal A, bigdecimal B) {return. subtract (B);} public static bigdecimal multiply (bigdecimal A, bigdecimal B) {return. multiply (B);} public static bigdecimal divide (bigdecimal A, bigdecimal B) {return. divide (B );}

Applications in the arrays class: Sorting (SORT), filling (fill), binary search (binarysearch)

Sort:

Int [] arr = new int [] {12,-2, 3,-1, 22, 7}; For (int I: ARR) {system. out. println (I);} arrays. sort (ARR); // system from small to large. out. println ("--- sort ---"); For (int I: ARR) {system. out. println (I);} // result: // 12 //-2 // 3 //-1 // 22 // 7 // --- sort --- //-2 ///-1 // 3 // 7/ /12 // 22

 

Sort reference types:

Import Java. io. bufferedinputstream; import Java. util. iterator; import Java. util. imports; import Java. util. set; import Java. util. treeset; public class demo {public static void main (string [] ARGs) throws exception {partition CIN = new partition (New bufferedinputstream (system. in); set <student> set = NULL; iterator <student> ite = NULL; Student Stu = NULL; string STR = NULL; string [] strstu = NULL; int num =-1; while (CIN. hasnext () {STR = cin. nextline (); num = integer. parseint (STR); Set = new treeset <student> (); For (INT I = 0; I <num; I ++) {STR = cin. nextline (); strstu = Str. split (""); Stu = new student (integer. parseint (strstu [0]), strstu [1], double. parsedouble (strstu [2]); set. add (Stu);} ite = set. iterator (); While (ITE. hasnext () {Stu = ite. next (); system. out. println (Stu) ;}} cin. close ();} // result: // 10 // 22 Limin 90 // 12 Limin 99 // 33 Limin 78 // 9 Zhaolin 100 // 0 Zhaolin 90 // 2 Zhaolin 99 // 88 Wanzai 100 // 7 Zhaolin 100 // 3 linmin 90 // 10 Wanzai 99 // student [Sid = 33, sname = Limin, sscore = 78.0] // student [Sid = 22, sname = Limin, sscore = 90.0] // student [Sid = 3, sname = linmin, sscore = 90.0] // student [Sid = 0, sname = Zhaolin, sscore = 90.0] // student [Sid = 12, sname = Limin, sscore = 99.0] // student [Sid = 10, sname = Wanzai, sscore = 99.0] // student [Sid = 2, sname = Zhaolin, sscore = 99.0] // student [Sid = 88, sname = Wanzai, sscore = 100.0] // student [Sid = 7, sname = Zhaolin, sscore = 100.0] // student [Sid = 9, sname = Zhaolin, sscore = 100.0]} class student implements comparable <student> {private int Sid; private string sname; private double sscore; Public int getsid () {return Sid;} public void setsid (INT Sid) {This. SID = Sid;} Public String getsname () {return sname;} public void setsname (string sname) {This. sname = sname;} public double getsscore () {return sscore;} public void setsscore (double sscore) {This. sscore = sscore ;}@ override Public String tostring () {return "student [Sid =" + Sid + ", sname =" + sname + ", sscore = "+ sscore +"] ";} public student (INT Sid, string sname, double sscore) {super (); this. SID = Sid; this. sname = sname; this. sscore = sscore;} // first sort by score from low to high, then the scores are the same, sorted by name. If the names are the same, sort by student ID from small to large @ override public int compareto (student Stu) {int temp1 = double. valueof (this. sscore ). compareto (double. valueof (Stu. sscore); If (0 = temp1) {int temp2 = This. sname. compareto (Stu. sname); If (0 = temp2) {return integer. valueof (this. sid ). compareto (integer. valueof (Stu. sid);} return temp2;} return temp1 ;}}

 

 

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.