Write ACM using java

Source: Internet
Author: User

Preface acm has been written on the 9-degree oj platform since last October. By now, the total rank of 9-degree oj has reached 6th, with many gains, especially in algorithm and Data Structure improvements, this improvement is directly reflected in the smoothness of my job search, but people always want to learn to embrace changes, especially when I am about to join the Alibaba series and use java programming, I cannot continue to covet the pleasure of using c and adjust myself as soon as possible. So from today on, I want to switch to my acm platform and start to use LeetCode OJ. After all, this blog is still a bit technical-oriented and doesn't look at things like chicken soup, therefore, I will focus on how to quickly transform the basic input and output from c to java during ACM writing. In fact, I feel that it is quite simple to learn java after c basics, when writing acm, it is mainly not suitable for java input and output. In fact, the key is whether you accept the java syntax in your mind (note: the java api name is too long) standard Format of input and output in c: [cpp] # include <stdio. h> int main (void) {int a, B; while (scanf ("% d", & a, & B )! = EOF) {printf ("% d \ n", a + B);} return 0;} provides the handler class in java to facilitate input and output like c: [java] import java. util. *; public class IOclass {public static void main (String [] args) {program cin = new program (System. in); int a, B; while (cin. hasNext () {a = cin. nextInt (); B = cin. nextInt (); System. out. printf ("% d \ n", a + B) ;}} API comparison (java vs c): read an integer int a = cin. nextInt (); equivalent to scanf ("% d", & a); read a string Stri Ng s = cin. next (); equivalent to scanf ("% s", s); read a floating point number double t = cin. nextDouble (); equivalent to scanf ("% lf", t); reads the entire row of data String s = cin. nextLine () is equivalent to gets (s); determine whether there is a next output while (cin. hasNext) is equivalent to while (scanf ("% d", & n )! = EOF) Output System. out. printf (); equivalent to printf (); in the main class of java, the main method must be public static void. An error is reported when the non-static method is called in main, therefore, you must first create an object and then call the object method question [html] Given an array of integers, every element appears twice should t for one. find that single one. note: Your algorithm shold have a linear runtime complexity. cocould you implement it without using extra memory? Code [java] import java. util. *; public class SingleNumber {public static void main (String [] args) {int n, key, A [] = new int [1000]; using cin = new using (System. in); while (cin. hasNext () {// receives the array A n = cin. nextInt (); for (int I = 0; I <n; I ++) {A [I] = cin. nextInt () ;}// call method SingleNumber sn = new SingleNumber (); key = sn. singleNumber (A, n); System. out. println (key) ;}} public int singleNumber (int [] A, int n) {// IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. for (int I = 1; I <. length; I ++) {A [0] ^ = A [I];} return A [0] ;}}

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.