Huawei School recruit on-machine written test (volume a)--java realization

Source: Internet
Author: User

Huawei School Recruit on-machine written test (volume a)--java realization

Programming Questions: A1. Min. Rectangle, A2. Determines whether a number contains two identical substrings, A3. Take a stone game.

A1. Minimum rectangle

Java code:
Import Java.util.arraylist;import Java.util.list;import Java.util.Scanner; Public classMinrectangle { Public Static void Main(String arg[]) {list<integer> Ldata=NewArraylist<integer> (); Boolean flag=false;//(0,0) consecutive occurrences, two consecutive occurrences of the end of the input        inttestnum=0;//Number of test casesScanner cin=NewScanner (System.inch); while(Cin.hasnext ()) {intX=cin.nextint ();intY=cin.nextint ();if(Math.Abs (x) > +|| Math.Abs (y) > +)return;if(x==0&&y==0){if(flag) { Break; }Else{flag=true;//A test case endedtestnum++; }            }Elseflag=false;            Ldata.add (x);        Ldata.add (y); } cin.close ();intSize=ldata.size ();intk=0; for(intI=0; i<testnum;i++) {intMinx=integer.max_value;intMiny=integer.max_value;intMaxx=integer.min_value;intMaxy=integer.min_value; for(; k<size/2; k++) {intX=ldata.Get(2*K);intY=ldata.Get(2*k+1);if(x==0&&y==0) {k++; Break;//A test case ended} minx=math.min (X,minx);                Miny=math.min (Y,miny);                Maxx=math.max (x, MaxX);            Maxy=math.max (Y,maxy); }if(minx!=maxx&&miny!=maxy&&minx!=integer.max_value&& miny!=integer.max_value& &maxx!=integer.min_value&&maxy!=integer.min_value) {System. out. println (minx+" "+miny+" "+maxx+" "+maxy); }        }    }}
A2. Determine if a number contains two identical substrings

Java code:
Import Java.util.Scanner; Public classCHILDSTR {PrivateString str;Private intLen Public void input() {Scanner cin=NewScanner (System.inch);        Str=cin.nextline (); Len=str.length ();if(str!=NULL&&len>9) {str=NULL;return; } for(intI=0; i<len;i++) {CharC=str.charat (i);if(c<' 1 '|| C>' 9 ') {str=NULL;return; }        }    } Public void Search(){if(str==NULL){return; } String s=NULL; for(intI=0; i<len-1; i++) {s=str.substring (i,i+1);//intercept sub-strings            if(Str.lastindexof (s)!=i) {//Retrieves from the tail whether it contains S and returns its positionSystem. out. println (1);return; }} System. out. println (0); } Public Static void Main(String arg[]) {Childstr cs=NewChildstr ();        Cs.input ();    Cs.search (); }}
A3. Take a stone game

Java code:
Import Java.util.Scanner; Public classFibgame {//Why is the declared sequence length 48? Because the calculation to f[48] is nearing the data range beyond int    Private int[] fib=New int[ -]; Public void F() {fib[0]=0; fib[1]=1; for(intI=2;i< -; i++) {fib[i]=fib[i-1]+fib[i-2]; }    } Public void input() {Scanner cin=NewScanner (System.inch);intN while(Cin.hasnext ()) {N=cin.nextint ();if(n<=1|| N> -) {System. out. println ("Please enter a number greater than 2 less than 48");return; } Boolean flag=false; for(intI=3;i< -; i++) {if(N==fib[i]) {flag=true; Break; }            }if(flag) {System. out. println ("Second win"); }Else{System. out. println ("first win"); }        }    } Public Static void Main(String arg[]) {Fibgame fg=NewFibgame ();        FG.F ();    Fg.input (); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Huawei School recruit on-machine written test (volume a)--java realization

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.