Estimados alumnos les deseo muchos éxitos en sus parciales :)
Para todos los problemas
Se tiene:
int[] x = new int[20];
int indice;
PROBLEMA 01
Implemente un método que retorne el último número divisible por 5
-----------------------------------------------
| 7 | 8 | 1 | 15 | 5 | 10 | 4 | 4 | 2|...0|0|0|
-----------------------------------------------
metodo()->10
public int metodo(){
int salida = -1;
for(int i = indice-1; i>=0;i--){
if(x[i]%5==0){
salida = x[i];
break;
}
}
return salida;
}
PROBLEMA 02
Implemente un método que retorne el segundo número múltiplo de cinco
-----------------------------------------------
| 7 | 8 | 1 | 15 | 5 | 10 | 4 | 4 | 2|...0|0|0
----------------------------------------------
0 1 2 3 4 5 6 7 8
metodo()->5
//Primera forma de solución
public int metodo(){
int salida = -1, c = 0 ;
for(int i = 0; i<indice; i++){
if(x[i]%5==0){
c++;
if(c == 2){
salida = x[i];
break;
}
}
}
return salida;
}
//Segunda forma de solución
public int metodo(){
int c = 0 ;
for(int i = 0; i<indice; i++){
if(x[i]%5==0){
c++;
if(c == 2){
return x[i];
}
}
}
return -1;
}
//Tercera forma de solucion
public int metodo(){
int c = 0 ;
for(int i = 0; i<indice; i++){
if(x[i]%5==0 && ++c == 2){
return x[i];
}
}
return -1;
}
PROBLEMA 03
Implemente un método que retorne el segundo número que termine en tres
-----------------------------------------------
| 7 | 13| 1 | 15 | 23 | 10 | 4 | 4 | 2|...0|0|0
----------------------------------------------
0 1 2 3 4 5 6 7 8
metodo()->23
// Primera forma de solución
public int metodo(){
int salida = -1, c = 0;
for(int i =0 ; i<indice; i++){
if(x[i]%10 == 3){
c++;
if(c ==2){
salida = x[i];
break;
}
}
}
return salida;
}
// Segunda forma de solución
public int metodo(){
int c = 0;
for(int i =0 ; i<indice; i++){
if(x[i]%10 == 3){
c++;
if(c ==2){
return x[i];
}
}
}
return -1;
}
// Tercera forma de solución
public int metodo(){
int c = 0;
for(int i =0 ; i<indice; i++){
if(x[i]%10 == 3 && ++c ==2){
return x[i];
}
return -1;
}
}
PROBLEMA 04
Implemente un método que retorne antepenúltimo número divisible por tres
-----------------------------------------------
| 7 | 13| 1 | 15 | 21 | 10 | 24 | 4 | 12|...0|0|0
----------------------------------------------
0 1 2 3 4 5 6 7 8
metodo()->21
public int metodo(){
int salida = -1, c = 0 ;
for(int i = indice-1; i>=0;i--){
if(x[i]%3==0){
c++;
if(c == 3){
salida = x[i];
break;
}
}
}
return salida;
}
PROBLEMA 05
Implemente un método que remplace el primer par por el último número múltiplo de tres
Antes
-----------------------------------------------
| 7 | 8 | 1 | 15 | 5 | 10 | 4 | 12 | 2|...0|0|0|
-----------------------------------------------
0 1 2 3 4 5 6 7 8
Después
-----------------------------------------------
| 7 | 12| 1 | 15 | 5 | 10 | 4 | 12| 2|...0|0|0|
-----------------------------------------------
0 1 2 3 4 5 6 7 8
public void metodo(){
//1 posicion del primer par
int posPar = -1;
for(int i = 0; i<indice; i++){
if(x[i]%2==0){
posPar = i;
break;
}
}
//2 el último numero múltiplo de tres
int ultNum =-1;
for(int i = indice-1; i>=0;i--){
if(x[i]%3==0){
ultNum = x[i];
break;
}
}
//3 el remplazo
if(posPar != -1 && ultNum != -1){
x[posPar] = ultNum;
}
}
PROBLEMA 06
Implemente un método que remplace el mayor par por segundo número par
Antes
-----------------------------------------------
| 7 | 8 | 1 | 15 | 5 | 10 | 4 | 12 | 2|...0|0|0|
-----------------------------------------------
0 1 2 3 4 5 6 7 8
Después
-----------------------------------------------
| 7 | 8| 1 | 15 | 5 | 10 | 4 | 10 | 2|...0|0|0|
-----------------------------------------------
0 1 2 3 4 5 6 7 8
public void metodo(){
//1 posicion del mayor par
int posMayPar = -1, may = 0;
for(int i = 0; i<indice; i++){
if(x[i]%2==0 && x[i]>may){
may = x[i]
posMayPar = i;
}
}
//2 el segundo número par
int segPar =-1, c = 0 ;
for(int i = 0; i<indice; i++){
if(x[i]%2==0 && ++c == 2){
segPar = x[i]
break;
}
}
//3 el remplazo
if(c>=2 ){
x[posMaPar] = segPar;
}
}
PROBLEMA 07
Implemente un método que intercambie el primer número que termine en 3 por el último que termine en 4
Antes
-----------------------------------------------
| 11 | 13| 1 | 23 | 5 | 24 | 14 | 11 | 12|...0|0|0|
-----------------------------------------------
0 1 2 3 4 5 6 7 8
Después
-----------------------------------------------
| 11 | 14 | 1 | 23 | 5 | 24 | 13| 11 | 12|...0|0|0|
-----------------------------------------------
0 1 2 3 4 5 6 7 8
public void metodo(){
//1 el primer numero que termine en 3
int posTerTres = -1, numTerTres = 0;
for(int i = 0; i<indice; i++){
if(x[i]%10 == 3){
posTerTres = i;
numTerTres = x[i];
break;
}
}
//2 el ultimo que termine en 4
int posUltCua =-1, numUltCua = 0 ;
for(int i = indice-1; i>=0; i++){
if(x[i]%10 == 4){
posUltCua = i;
numUltCua = x[i];
break;
}
}
//3 validaciones y el intercambio
If(posTerTres == -1){
Imprime(No existe numero q termine en tres);
}
If(posUltCua == -1){
Imprime(No existe numero q termine en cuatro);
}
If(posTerTres != -1 && posUltCua != -1){
x[posTerTres] = numUltCua;
x[posUltCua] = numTerTres;
}
}
PROBLEMA 08
Intercambie el primer número múltiplo de 3 por el último número múltiplo de 7.
Antes
-----------------------------------------------
| 11 | 12| 1 | 23 | 5 | 24 | 77 | 11 | 12|...0|0|0|
-----------------------------------------------
0 1 2 3 4 5 6 7 8
Después
-----------------------------------------------
| 11 | 77 | 1 | 23 | 5 | 24 | 12| 11 | 12|...0|0|0|
-----------------------------------------------
0 1 2 3 4 5 6 7 8
public void metodo(){
//1 el primer número múltiplo de 3
int posMulTres = -1, numMulTres = 0;
for(int i = 0; i<indice; i++){
if(x[i]%3==0){
numMulTres = x[i];
posMulTres = i;
break;
}
}
//2 el último número múltiplo de 7.
int posMulSie =-1, numMulSie = 0 ;
for(int i = indice-1; i>=0;i--){
if(x[i]%7==0){
posMulSie = i;
numMulSie = x[i];
break;
}
}
//3 validaciones y el intercambio
if(posMulTres == -1){
imprime("No existe número múltiplo de 3");
}
if(posMulSie == -1){
imprime("No existe número múltiplo de 7");
}
if(posMulTres != -1 && posMulSie!= -1){
x[posMulTres] = numMulSie;
x[posMulSie] = numMulTres;
}
}
PROBLEMA 09
Intercambie el segundo mayor par por el termino central de arreglo
Antes
---------------------------------------------------
| 11 | 13| 1 | 23 | 5 | 24 | 14 | 11 | 12|...0|0|0|
---------------------------------------------------
0 1 2 3 4 5 6 7 8
Después
---------------------------------------------------
| 11 | 13| 1 | 23 | 14 | 24 | 5 | 11 | 12|...0|0|0|
---------------------------------------------------
0 1 2 3 4 5 6 7 8
public void metodo(){
//1 termino central
int posCen = 0, numCen =0;
if(posCen %2 !=0){
posCen = indice/2;
numCen = x[posCen];
}else{
posCen = -1;
}
//2 el segundo mayor par
int posPar , numPar = 0, segPar =0;
for(int i = 0; i<indice; i++){
if(x[i]%2==0 && x[i]>numPar){
numPar = x[i];
}
}
for(int i = 0; i<indice; i++){
if(x[i]%2==0 && x[i]!=numPar && x[i]>segPar){
segPar = x[i];
}
}
//3 el intercambio
x[posCen] = segPar;
x[posPar] = numCen;
}
No hay comentarios:
Publicar un comentario