FSaraiva Posted June 9, 2009 at 03:15 PM Report #271034 Posted June 9, 2009 at 03:15 PM Boa tarde, gostava de saber como faço um metodo recursivo que dada um array elementos do tipo inteiro que descubra se a sequencia tem algum elemento repetido. boolean temRepetidos ( int [ ] v )... Agradecia umas luzes....
FSaraiva Posted June 13, 2009 at 02:41 PM Author Report #271938 Posted June 13, 2009 at 02:41 PM public class testes { private static boolean contains(int [] v, int target, int first){ if(first >= v.length) return false; else if(v[first] == target) return true; else return contains(v, target, (first + 1)); } public static boolean cont(int [] v){ return cont (v,0); } public static boolean cont(int [] v, int index){ if( index + 1 == v.length ){ return false; } else if (contains(v,v[index],index +1)) return true; return cont (v , index+1); } public static void main (String [] args){ int v [] = {1,2,3,4,5,8,7,8}; System.out.println(cont(v)); } }
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now