Comparison of variables from a string (Java) -
assume, have variable y = 6. have array of variables x: 12 22 32 11 56. need write program, match variable y among array variables until success or end. matching algorithm checkes whether variable x greater or equal 2y, starting smallest value. values not ordered. values (both x , y) putting through console; values in x divided space. once such value in x found, becomes futher checking, replaces y first succesfull x etc.
in example above. 11 - no. 12 - yes. 22 - no (related 12). 32 - yes. 56 - no.
as result want have sum of successful matches, in case - 2.
to values array use code:
bufferedreader br=new bufferedreader (new inputstreamreader(system.in)); string vvod = br.readline(); string[] tok=vvod.split(" "); (int i=0; i<tok.length;i++) { int x=integer.parseint(tok[i]);
i find difficulties matching algorithm, once have success, have change number matching with. have problems ordering values string.
p.s. learning how code.
i think looking this:
int y = 6; // replace starting y wherever generating int[] x = {12, 22, 32, 11, 56}; // replace own generated array arrays.sort(x); // remember import java.util.arrays; @ top of file int matches = 0; for(int = 0; < x.length; i++) { if(x[i] >= 2*y) { matches++; y = x[i]; } } system.out.println(matches); // print or return depending on needs here
Comments
Post a Comment