Algorithm


Problem Name: 2 AD-HOC - beecrowd | 1769

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1769

SSN 1

 

By Alexandre Campos, UNIUBE BR Brazil

Timelimit: 1

In Brazil, the equivalent to Social Security Number is usually called CPF. It is composed by 11 digits and the two lasts are function of the nine previous. In this way, if a person informs a CPF, by mistake or on purpose, it is possible to find out. Let us introduce some notation. Let a CPF be

a1a2a3.a4a5a6.a7a8a9-b1b2

To get b1, one can multiply aby 1, a2 by 2, a3 by 3, so on, up to a9 by 9 and sum these results. Then, b1 is the remaining of this number when divided by 11, or 0 in case the remaining is 10.

Analogous, to get b2, one can multiply aby 9, a2 by 8, a3 by 7, so on, up to a9 by 1 and sum these results. Then, b2 is the remaining of this number when divided by 11, or 0 in case the remaining is 10.

Given a CPF number, you have to tell whether it is valid or not.

 

Input

 

The input is composed by an unknown number of CPF numbers, not more than 10000 cases. Each line has a CPF in the form

d1d2d3.d4d5d6.d7d8d9-d1d2

 

Output

 

If the given CPF is valid, print "CPF valido". Otherwise, print "CPF invalido".

 

 

 

Input Sample Output Sample

048.856.829-63
733.184.680-96
227.518.471-08
092.844.842-86
098.447.895-55

CPF invalido
CPF valido
CPF invalido
CPF valido
CPF invalido

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>
#include <ctype.h>

void transformaInt(char *, short vet[]);
void retiraPonto(char *, char *);

int main (void)
{

	char cpf[15], cpfCpy[15];
	short numCPF[15], numB1 = 0, numB2 = 0;
	unsigned char numero, i = 0, j , k;

	while ((scanf(" %s", cpf) != EOF ))
	{
		//Retirar caracteres especiais;
		retiraPonto(cpf, cpfCpy);

		//Converter caractere para inteiro;
		transformaInt(cpfCpy, numCPF);

		for (i = 0, j = 1, k = 9; i  <  9; i++, j++, k--)
		{	
			numB1 += numCPF[i] * j;
			numB2 += numCPF[i] * k;
		}

		numB1 = numB1 % 11;
		if (numB1 % 11 == 10)
			numB1 = 0;
		numB2 = numB2 % 11;
		if (numB2 % 11 == 10)
			numB2 = 0;

		if (numB1 == numCPF[9] && numB2 == numCPF[10])
			printf("CPF valido\n");
		else
			printf("CPF invalido\n");

		numB1 = 0;
		numB2 = 0;
	}
}

void transformaInt(char *str, short vet[])
{

	unsigned short i;

	for (i = 0; i  <  11; i++)
		vet[i] = (int)str[i] - 48;

}

void retiraPonto(char *str, char *strCpy)
{

	short i = 0, j = 0;

	while (i  <  14)
	{

		if (isdigit(str[i]))
			strCpy[j++] = str[i];

		i++;
	}

	strCpy[j] = '\0';
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
048.856.829-63
733.184.680-96
227.518.471-08
092.844.842-86
098.447.895-55

Output

x
+
cmd
CPF invalido
CPF valido
CPF invalido
CPF valido
CPF invalido

#2 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>

using namespace std;

void cpf(char *a, char *b) {
   while (*a) {
      if ('0'  < = *a and *a <= '9')
         *b++ = *a;
      ++a;
   }
   *b = '\0';
}

short cti(char c) {
   return c - 48;
}

int main(void) {
   int s, i, b1, b2;
   char e[15], c[12];

   while (cin >> e) {
      cpf(e, c);

      s = 0;
      for (i = 0; i  <  9; ++i)
         s += cti(c[i]) * (i+1);
      b1 = s % 11;
      b1 = (b1 != 10) ? b1 : 0;

      s = 0;
      for (i = 0; i  <  9; ++i)
         s += cti(c[i]) * (9-i);
      b2 = s % 11;
      b2 = (b2 != 10) ? b2 : 0;

      if (b1 == cti(c[9]) and b2 == cti(c[10]))
         cout << "CPF valido";
      else
         cout << "CPF invalido";
      cout << endl;
   }

   return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
048.856.829-63
733.184.680-96
227.518.471-08
092.844.842-86
098.447.895-55

Output

x
+
cmd
CPF invalido
CPF valido
CPF invalido
CPF valido
CPF invalido

#3 Code Example with Javascript Programming

Code - Javascript Programming


const { readFileSync } = require("fs")
const input = readFileSync("/dev/stdin", "utf8").split("\n")

class CPF {
	/** @param {string} cpf */
	static validate(cpf) {
		const arr = cpf
			.match(/\d/g)
			.slice(0, 11)
			.map((code) => Number.parseInt(code, 10))

		let res01 = 0
		let res02 = 0

		for (let index = 0; index  <  9; index++) {
			res01 += (index + 1) * arr[index]
			res02 += (9 - index) * arr[index]
		}

		return (res01 % 11) % 10 == arr[9] && (res02 % 11) % 10 == arr[10]
	}
}

function main() {
	const responses = []

	for (const line of input)
		if (line == "") break
		else responses.push(CPF.validate(line) ? "CPF valido" : "CPF invalido")

	console.log(responses.join("\n"))
}

main()
Copy The Code & Try With Live Editor

Input

x
+
cmd
048.856.829-63
733.184.680-96
227.518.471-08
092.844.842-86
098.447.895-55

Output

x
+
cmd
CPF invalido
CPF valido
CPF invalido
CPF valido
CPF invalido

#4 Code Example with Python Programming

Code - Python Programming


while True:
    try:
        e = str(input()).strip()
        c = e.replace('.', '').replace('-', '')
        s = 0
        for i in range(9):
            s += int(c[i]) * (i + 1)
        b1 = s % 11
        b1 = 0 if b1 == 10 else b1

        s = 0
        if b1 == int(e[-2]):
            for i in range(9):
                s += int(c[i]) * (9 - i)
            b2 = s % 11
            b2 = 0 if b2 == 10 else b2
            if b2 == int(e[-1]):
                print('CPF valido')
            else: print('CPF invalido')
        else: print('CPF invalido')
    except EOFError: break
Copy The Code & Try With Live Editor

Input

x
+
cmd
048.856.829-63
733.184.680-96
227.518.471-08
092.844.842-86
098.447.895-55

Output

x
+
cmd
CPF invalido
CPF valido
CPF invalido
CPF valido
CPF invalido
Advertisements

Demonstration


Previous
#1765 Beecrowd Online Judge Solution 1765 Christmas Trapeziums Solution in C, C++, Java, Js and Python
Next
#1770 Beecrowd Online Judge Solution 1717 Shuffle Solution in C, C++, Java, Js and Python