-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP106.java
88 lines (79 loc) · 2.77 KB
/
P106.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package p100_199;
import java.util.Scanner;
//accepted
public class P106 {
static String [] numpais = {"0", "380", "50", "539", "560", "70", "759", "850", "890"};
static String [] nombrePais = {" EEUU", " Bulgaria", " Inglaterra", " Irlanda", " Portugal", " Noruega", " Venezuela", " Cuba", " India"};
public static void casoPrueba(String n, boolean trece){
int iteraciones = trece ? 12 : 7;
long x = Long.parseLong(n);
long digitoControl = x%10;
x = x/10;
long acumulador = 0;
for (int i = 0; i < iteraciones; i++) {
acumulador = acumulador + (i%2 != 0 ? (x%10) : 3L *(x%10));
x=x/10;
}
if ((acumulador+digitoControl)%10 != 0){
System.out.print("NO");
} else {
System.out.print("SI");
if (trece){
boolean found = false;
String codigo = n.substring(0, 3);
for (int i = 0; i < numpais.length; i++) {
if (codigo.equals(numpais[i])){
found = true;
System.out.print(nombrePais[i]);
break;
}
}
if (!found){
codigo = n.substring(0, 2);
for (int i = 0; i < numpais.length; i++) {
if (codigo.equals(numpais[i])){
found = true;
System.out.print(nombrePais[i]);
break;
}
}
}
if (!found){
if (n.substring(0,1).equals(numpais[0])){
System.out.print(nombrePais[0]);
found = true;
}
}
if (!found){
System.out.print(" Desconocido");
}
}
}
System.out.println();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String n = sc.nextLine();
while (!n.equals("0")){
boolean trece = n.length()>8;
if (trece && n.length()<13){
int faltantes = 13-n.length();
String anadir = "";
for (int i = 0; i < faltantes; i++) {
anadir+="0";
}
n = anadir+n;
}
if (!trece && n.length()<8){
int faltantes = 8-n.length();
String anadir = "";
for (int i = 0; i < faltantes; i++) {
anadir+="0";
}
n = anadir+n;
}
casoPrueba(n, trece);
n = sc.nextLine();
}
}
}