Buenas,
Estaba haciendo un ejercicio en java sobre el numero kaprekar(seguro que os suena), y me sale "Run-time error" cunado entrego. Os dejo el codigo. Porque puede ser?
Gracias
package kaprekar;
import java.util.Scanner;
public class Kaprekar {
public static void toArray(int n, int a[]) {
int d = 1000;
for (int i = 0; i < 4; i++) {
a = n / d;
n = n % d;
d = d / 10;
}
}
public static void Clasificarmayor(int a[]) {
for (int i = 0; i <= 2; i++) {
for (int j = i + 1; j <= 3; j++) {
if (a < a[j]) {
int t;
t = a;
a = a[j];
a[j] = t;
}
}
}
}
public static void Clasificarmenor(int a[], int b[]) {
for (int i = 3; i >= 0; i--) {
b[3 - i] = a;
}
}
public static void Pasaranumero(int Menor, int a[], int d, int Mayor, int b[], int ret[]) {
d = 1000;
for (int i = 0; i <= 3; i++) {
Mayor = Mayor + a * d;
d = d / 10;
}
d = 1000;
for (int i = 0; i <= 3; i++) {
Menor = Menor + b * d;
d = d / 10;
}
ret[0] = Mayor;
ret[1] = Menor;
}
public static void Resta(int Mayor, int Menor, int ret[]) {
ret[2] = ret[0] - ret[1];
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int ret[] = new int[3];
int n, num;
num = sc.nextInt();
int con;
int a[] = new int[4];
int b[] = new int[4];
int acon[] = new int[num];
int Menor = 0;
int Mayor = 0;
int d = 1000;
int q = 0;
int x = num;
do {
con = 0;
num--;
n = sc.nextInt();
while (n != 6174){
toArray(n, a);
Clasificarmayor(a);
Clasificarmenor(a, b);
Pasaranumero(Menor, a, d, Mayor, b, ret);
Resta(Mayor, Menor, ret);
n = ret[2];
con++;
}
acon[q] = con;
q++;
} while (num != 0);
int y = 0;
while(x != 0) {
System.out.println(acon[y]);
x--;
y++;
}
}
}
Estaba haciendo un ejercicio en java sobre el numero kaprekar(seguro que os suena), y me sale "Run-time error" cunado entrego. Os dejo el codigo. Porque puede ser?
Gracias
package kaprekar;
import java.util.Scanner;
public class Kaprekar {
public static void toArray(int n, int a[]) {
int d = 1000;
for (int i = 0; i < 4; i++) {
a = n / d;
n = n % d;
d = d / 10;
}
}
public static void Clasificarmayor(int a[]) {
for (int i = 0; i <= 2; i++) {
for (int j = i + 1; j <= 3; j++) {
if (a < a[j]) {
int t;
t = a;
a = a[j];
a[j] = t;
}
}
}
}
public static void Clasificarmenor(int a[], int b[]) {
for (int i = 3; i >= 0; i--) {
b[3 - i] = a;
}
}
public static void Pasaranumero(int Menor, int a[], int d, int Mayor, int b[], int ret[]) {
d = 1000;
for (int i = 0; i <= 3; i++) {
Mayor = Mayor + a * d;
d = d / 10;
}
d = 1000;
for (int i = 0; i <= 3; i++) {
Menor = Menor + b * d;
d = d / 10;
}
ret[0] = Mayor;
ret[1] = Menor;
}
public static void Resta(int Mayor, int Menor, int ret[]) {
ret[2] = ret[0] - ret[1];
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int ret[] = new int[3];
int n, num;
num = sc.nextInt();
int con;
int a[] = new int[4];
int b[] = new int[4];
int acon[] = new int[num];
int Menor = 0;
int Mayor = 0;
int d = 1000;
int q = 0;
int x = num;
do {
con = 0;
num--;
n = sc.nextInt();
while (n != 6174){
toArray(n, a);
Clasificarmayor(a);
Clasificarmenor(a, b);
Pasaranumero(Menor, a, d, Mayor, b, ret);
Resta(Mayor, Menor, ret);
n = ret[2];
con++;
}
acon[q] = con;
q++;
} while (num != 0);
int y = 0;
while(x != 0) {
System.out.println(acon[y]);
x--;
y++;
}
}
}