(SPOJ) 2284 - Palavras primas - Solução
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
Main processando = new Main();
processando.processa();
System.exit(0);
}
void processa() throws NumberFormatException, IOException {
String line = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int[] vetor = new int[60];
vetor['a'-'A'] = 1;
vetor['b'-'A'] = 2;
vetor['c'-'A'] = 3;
vetor['d'-'A'] = 4;
vetor['e'-'A'] = 5;
vetor['f'-'A'] = 6;
vetor['g'-'A'] = 7;
vetor['h'-'A'] = 8;
vetor['i'-'A'] = 9;
vetor['j'-'A'] = 10;
vetor['k'-'A'] = 11;
vetor['l'-'A'] = 12;
vetor['m'-'A'] = 13;
vetor['n'-'A'] = 14;
vetor['o'-'A'] = 15;
vetor['p'-'A'] = 16;
vetor['q'-'A'] = 17;
vetor['r'-'A'] = 18;
vetor['s'-'A'] = 19;
vetor['t'-'A'] = 20;
vetor['u'-'A'] = 21;
vetor['v'-'A'] = 22;
vetor['w'-'A'] = 23;
vetor['x'-'A'] = 24;
vetor['y'-'A'] = 25;
vetor['z'-'A'] = 26;
vetor['A'-'A'] = 27;
vetor['B'-'A'] = 28;
vetor['C'-'A'] = 29;
vetor['D'-'A'] = 30;
vetor['E'-'A'] = 31;
vetor['F'-'A'] = 32;
vetor['G'-'A'] = 33;
vetor['H'-'A'] = 34;
vetor['I'-'A'] = 35;
vetor['J'-'A'] = 36;
vetor['K'-'A'] = 37;
vetor['L'-'A'] = 38;
vetor['M'-'A'] = 39;
vetor['N'-'A'] = 40;
vetor['O'-'A'] = 41;
vetor['P'-'A'] = 42;
vetor['Q'-'A'] = 43;
vetor['R'-'A'] = 44;
vetor['S'-'A'] = 45;
vetor['T'-'A'] = 46;
vetor['U'-'A'] = 47;
vetor['V'-'A'] = 48;
vetor['W'-'A'] = 49;
vetor['X'-'A'] = 50;
vetor['Y'-'A'] = 51;
vetor['Z'-'A'] = 52;
while((line = br.readLine()) != null) {
int soma = 0;
for (int i = 0; i < line.length(); i++) {
soma += vetor[line.charAt(i)-'A'];
}
boolean primo = true;
for (int i = 2; i*i <= soma; i++) {
if (soma%i == 0) {
primo = false;
break;
}
}
if (primo) {
bw.write("It is a prime word.\n");
}
else {
bw.write("It is not a prime word.\n");
}
}
bw.flush();
bw.close();
return;
}
}
import java.util.*;
class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
Main processando = new Main();
processando.processa();
System.exit(0);
}
void processa() throws NumberFormatException, IOException {
String line = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int[] vetor = new int[60];
vetor['a'-'A'] = 1;
vetor['b'-'A'] = 2;
vetor['c'-'A'] = 3;
vetor['d'-'A'] = 4;
vetor['e'-'A'] = 5;
vetor['f'-'A'] = 6;
vetor['g'-'A'] = 7;
vetor['h'-'A'] = 8;
vetor['i'-'A'] = 9;
vetor['j'-'A'] = 10;
vetor['k'-'A'] = 11;
vetor['l'-'A'] = 12;
vetor['m'-'A'] = 13;
vetor['n'-'A'] = 14;
vetor['o'-'A'] = 15;
vetor['p'-'A'] = 16;
vetor['q'-'A'] = 17;
vetor['r'-'A'] = 18;
vetor['s'-'A'] = 19;
vetor['t'-'A'] = 20;
vetor['u'-'A'] = 21;
vetor['v'-'A'] = 22;
vetor['w'-'A'] = 23;
vetor['x'-'A'] = 24;
vetor['y'-'A'] = 25;
vetor['z'-'A'] = 26;
vetor['A'-'A'] = 27;
vetor['B'-'A'] = 28;
vetor['C'-'A'] = 29;
vetor['D'-'A'] = 30;
vetor['E'-'A'] = 31;
vetor['F'-'A'] = 32;
vetor['G'-'A'] = 33;
vetor['H'-'A'] = 34;
vetor['I'-'A'] = 35;
vetor['J'-'A'] = 36;
vetor['K'-'A'] = 37;
vetor['L'-'A'] = 38;
vetor['M'-'A'] = 39;
vetor['N'-'A'] = 40;
vetor['O'-'A'] = 41;
vetor['P'-'A'] = 42;
vetor['Q'-'A'] = 43;
vetor['R'-'A'] = 44;
vetor['S'-'A'] = 45;
vetor['T'-'A'] = 46;
vetor['U'-'A'] = 47;
vetor['V'-'A'] = 48;
vetor['W'-'A'] = 49;
vetor['X'-'A'] = 50;
vetor['Y'-'A'] = 51;
vetor['Z'-'A'] = 52;
while((line = br.readLine()) != null) {
int soma = 0;
for (int i = 0; i < line.length(); i++) {
soma += vetor[line.charAt(i)-'A'];
}
boolean primo = true;
for (int i = 2; i*i <= soma; i++) {
if (soma%i == 0) {
primo = false;
break;
}
}
if (primo) {
bw.write("It is a prime word.\n");
}
else {
bw.write("It is not a prime word.\n");
}
}
bw.flush();
bw.close();
return;
}
}
Comments
Post a Comment