package programame1;
public class UVA138_numerosCalles_PRINT {
public static void main(String[] args) {
//%10d imprime o numero en un margen de 10 espacios á izquierda (como pedia o ejercicio).
System.out.printf("%10d%10d%n", 6, 8);
System.out.printf("%10d%10d%n", 35, 49);
System.out.printf("%10d%10d%n", 204, 288);
System.out.printf("%10d%10d%n", 1189, 1681);
System.out.printf("%10d%10d%n", 6930, 9800);
System.out.printf("%10d%10d%n", 40391, 57121);
System.out.printf("%10d%10d%n", 235416, 332928);
System.out.printf("%10d%10d%n", 1372105, 1940449);
System.out.printf("%10d%10d%n", 7997214, 11309768);
System.out.printf("%10d%10d%n", 46611179, 65918161);
}
}
FORMA DE CALCULALO
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package UVA;
public class UVA138_numerosCalles {
public static void main(String[] args) {
int cant = 0, casasCalle = 1, sumaIzq, sumaDer;
boolean correcto;
while (cant <= 10) {
correcto = false;
while (!correcto) {
for (int i = (casasCalle*70/100); i < (casasCalle*75/100); i++) {
sumaIzq = sumaNumeros(i - 1, 1);
sumaDer = sumaNumeros(i + 1, casasCalle);
if (sumaIzq == sumaDer) {
correcto = true;
cant++;
enseñaNumeros(i, casasCalle);
}
}
casasCalle++;
}
}
}
public static int sumaNumeros(int inicio, int fin) {
int suma = 0, mayor, menor;
mayor = Math.max(inicio, fin);
menor = Math.min(inicio, fin);
for (int i = menor; i <= mayor; i++) {
suma += i;
}
return suma;
}
public static void enseñaNumeros(int casa, int calle) {
System.out.printf("%10d%10d%n", casa, calle);
}
}
No hay comentarios:
Publicar un comentario