Jump to content

public static int[] lastTwo( int val )


shaqm
 Share

Recommended Posts

Boas, tenho um método recursivo para fazer para a faculdade, já tenho o código e o método funciona para arrays ordenados, mas não para desordenados, eu a programar recusrsivo é sempre à "martelada" e depois vejo no debug o que está mal, mas com o windows7 dá um bug no eclipse e não consigo correr o debug, aqui vai o problema:

public static int[] lastTwo( int val )

que retorna um array de dois inteiros contendo os dois algarismos de maior peso na base decimal do valor val. Caso o valor absoluto de |val| seja menor que 10 retorna null.

o código que eu fiz:

package grupo1;

public class LastTwo {

public static int[] lastTwo( int val ){
	int []a=new int[2];
	if(Math.abs(val) < 10)return null;
	if(val < 100){
		if(val%10 < val/10){
			a[0]=val%10;
			a[1]=val/10;
			return a;
		}else {
			a[1]=val%10;
			a[0]=val/10;
			return a;
		}	
	}
	if(val%10 > lastTwo(val/10)[0]){
		if(val%10 > lastTwo(val/10)[1]){
			a[1]=val%10;
			a[0]=lastTwo(val/10)[1];
			return a;
		}else{
			a[0]=val%10;
			return a;
		}
	}else
		return a;
}
}

alguém me csg dar uma mãozinha?tks in advance 😁

Link to comment
Share on other sites

Já csg fazer o debug e encontrei o problema, mas não o csg resolver:

public static int[] lastTwo( int val ){
	int []a=new int[2];
	if(Math.abs(val) < 10)return null;
	if(val < 100){
		if(val%10 < val/10){//quando retorna a os valores do array ficam a zero!
			a[0]=val%10;
			a[1]=val/10;
			return a;
		}else {
			a[1]=val%10;
			a[0]=val/10;
			return a;
		}	
	}
	if(val%10 > lastTwo(val/10)[0]){
		if(val%10 > lastTwo(val/10)[1]){
			a[1]=val%10;
			a[0]=lastTwo(val/10)[1];
			return a;
		}else{
			a[0]=val%10;
			return a;
		}
	}else
		return a;
}

Sugestões?

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.