Algorithm
Problem Name: 2 AD-HOC - beecrowd | 2508
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2508
Fortune Teller
By Hamilton José Brumatto, UESC Brazil
Timelimit: 1
Clodovildo Procariontes Urus is a Fortune Teller, he uses numerology as a science that studies luck from numbers. An example of numerology application is in the calculation of luck from the person's name. Many people worried about their own fortune end up changing their name to ensure a better luck. The calculation of the lucky number is based on the Pythagorean table, where each letter is translated to a number:
Once the letter has been translated by numbers, we add the sum and add the result digits until there is a value between 1 and 9. See, for example, the name "Harry Potter": 8 + 1 + 9 + 9 + 7 + 7 + 6 + 2 + 2 + 5 + 9 = 65 → 6 + 5 = 11 → 1 + 1 = 2. The lucky number for Harry Potter is 2. Clodovildo has asked you for a program that calculates the lucky number from given names, with the number he can declare the luck of the person.
Input
The input contains several test cases. Each test case has a name N on a single line, the name is composed of unaccented letters, and the cedilla is represented as the letter 'C', each name has at most 100 characteres.
Output
For each input test case your program should generate for input name, a line with the Lucky Number.
Input Sample | Output Sample |
Harry Potter |
2 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <ctype.h>
unsigned short tabela(char);
unsigned short numBruto(const char *nome);
unsigned short numSorte(unsigned short numero);
int main (void)
{
unsigned short numeroSorte;
char nome[110];
while (scanf(" %[^\n]", nome) != EOF)
printf("%hu\n", numSorte(numBruto(nome)));
}
// Função converte o caractere para um numero;
unsigned short tabela(char caractere)
{
switch (caractere) {
case 'A': case 'J': case 'S':
return 1;
case 'B': case 'K': case 'T':
return 2;
case 'C': case 'L': case 'U':
return 3;
case 'D': case 'M': case 'V':
return 4;
case 'E': case 'N': case 'W':
return 5;
case 'F': case 'O': case 'X':
return 6;
case 'G': case 'P': case 'Y':
return 7;
case 'H': case 'Q': case 'Z':
return 8;
case 'I': case 'R':
return 9;
}
}
// Função calcula a soma bruta do número gerado pela função 'tabela';
unsigned short numBruto(const char *nome)
{
short soma = 0;
while (*nome)
{
if (*nome == ' ')
nome++;
soma += tabela(toupper(*(nome++)));
}
return soma;
}
// Função calcula a soma dos digitos do número bruto gerado pela função 'numBruto';
unsigned short numSorte(unsigned short numero)
{
short soma = 0;
if (numero < 10)
return numero;
else
{
do
{
soma += numero % 10;
numero /= 10;
} while (numero > 9);
soma += numero;
return numSorte(soma);
}
}
Copy The Code &
Try With Live Editor