forked from MiYazJE/Acepta-el-reto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp106.java
85 lines (67 loc) · 2.5 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
import java.util.*;
/**
* @author retos_killer
* Problema 106 ACR Codigo de barras
*/
public class p106 {
public static String identificarPais(String codigo) {
if (codigo.equals("380")) return "Bulgaria";
else if (codigo.equals("539")) return "Irlanda";
else if (codigo.equals("560")) return "Portugal";
else if (codigo.equals("759")) return "Venezuela";
else if (codigo.equals("850")) return "Cuba";
else if (codigo.equals("890")) return "India";
else {
codigo = codigo.substring(0, 2);
if (codigo.equals("70")) return "Noruega";
else if (codigo.equals("50")) return "Inglaterra";
else if (codigo.charAt(0) == '0') return "EEUU";
}
return "Desconocido";
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String codigo, pais;
int calculo, codigoCntrol, flag, len, n;
boolean ean13;
while (true) {
codigo = s.nextLine();
if (codigo.equals("0")) break;
len = codigo.length();
ean13 = (len > 8 && len < 14);
if (len < 14) {
flag = 0;
calculo = 0;
for (int i = len-2; i >= 0; i--) {
n = Integer.parseInt(""+codigo.charAt(i));
if (flag == 0) {
calculo += n * 3;
flag = 1;
}
else {
calculo += n;
flag = 0;
}
}
codigoCntrol = Integer.parseInt(""+codigo.charAt(len-1));
if ((codigoCntrol + calculo) %10 == 0) {
if (ean13) {
pais = "";
if (len < 13) pais = "EEUU";
else pais = identificarPais(codigo.substring(0, 3));
System.out.println("SI " + pais);
}
else {
System.out.println("SI");
}
}
else {
System.out.println("NO");
}
}
else {
System.out.println("NO");
}
}
}
}