Sunday 15 April 2012

java - IsFibo not returning desired result -


This is a solution that I think of IsFibo HackerRank problem. (). I am thinking that this will not return "ISFIO" to 5. My argument is probably faulty and if someone tells me my mistake, then I appreciate it. Here's the code:

  import java.io. *; Import java.util. *; Import java.text *; Import java.math. *; Import java.util.regex *; Public class solutions {public stable arrelevith & lt; Bigenter & gt; Fiblelist = new Arrestists & lt; Bigenter & gt; (); Public static zero main (string [] args) {/ * Enter your code here. Read input from STDIN Print output to STDOUT; Your class name should be resolved * / scanner = new scanner (System.in); Int numcases = in.nextInt (); FibList.add (BigInteger.valueOf (0)); FibList.add (BigInteger.valueOf (1)); While (in.hasNextLine ()) {System.out.println (checkFibo (in.nextBigInteger ())); Println (fibList); }} Public Stabilized String Checkfibo (BigEnger i) {int lastIndex = fibList.size () - 1; Int Thalneris = i.compareTo (fibList.get (last index)); System.out.println ("Final Fib Number:" + Fiblist .get (Last Index)); System.out.println ("Compare Ridge:" + Compare); Switch (Compare) (Case 0: Return "ISFIO"; Case 1: BigInterizer newfib = Fibli.Get (Last Index-1) .add (Fibli.Get (Last Index)); fibList.add (newFib); checkFibo I) break; default: break;} return "isnotfibo";}}  

There are two problems with your code:

  1. Case 1: Return value is not returned from the recurring call
  2. Case -1 that falls on the default implementation is completely To unseen Is eaten (ie can not be small input after the large processed process that has already been processed)

Here is a working code:

  import java.math.BigInteger; import java.util.ArrayList; import java.util.Scanner; public class solution {public stable arrelist & lt; bigenter & gt; fibliaclest = new arrelist & lt; bigigner & gt; (); Public Fixed Zero Main (string [] Args) {/ * Enter your code here. Read the input from STDIN Switch to output must resolve the name of your class to print to STDOUT * / scanner = new Scanner (System.in); FibList.add (BigInteger.valueOf (0)); FibList.add (BigInteger.valueOf (1)); While (in.hasNextLine ()) {BigInteger nextBigDecimal = in.nextBigInteger (); System.out.println ("Input:" + NextBigDiskic); System.out.println ("isFibo:" + Checkfibo (Next Bigdesque)); Println (fibList); }} Public Stable Boolean Checkfibo (Bigenter i) {int lastIndex = fibList.size () - 1; Int Thalneris = i.compareTo (fibList.get (last index)); System.out.println ("Final Fib Number:" + Fiblist .get (Last Index)); System.out.println ("Compare Ridge:" + Compare); Boolean isFibo = false; Switch (compare) {case 0: isFibo = true; break; Case 1: Bigentiary newfib = FBList. Get (Last Index-1) .add (Fibli.Get (Last Index)); FibList.add (newFib); IsFibo = checkFibo (i); break; Case -1: isFibo = fibList.contains (i); break; } Returns is Phibo; }}  

No comments:

Post a Comment