Algorithm


Problem Name: 2 AD-HOC - beecrowd | 1786

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

SSN 2

 

By Alexandre Campos, UNIUBE BR Brazil

Timelimit: 1

You are about to write a program to predict a CPF, which, in Brazil, is equivalent to Social Security Number. It is composed by 11 digits and the lasts two (verification digits) 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

a1 a2 a3 . a4 a5 a6 . a7 a8 a9 - b1 b2

To get b1, one can multiply a1 by 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.

Analogously, to get b2, one can multiply a1 by 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.

 

Input

 

The input is composed by an unknown number of sequences in the form:

a1a2a3a4a5a6a7a8a9

Each sequence represents the 9 firsts digits of a CPF.

 

Output

 

For each sequence, you have to print the input sequence and the verification digits formated as

a1a2a3.a4a5a6.a7a8a9-b1b2

 

 

 

Input Sample Output Sample

000000000
111111111
354122447
569961340
169992467

000.000.000-00
111.111.111-11
354.122.447-93
569.961.340-48
169.992.467-85

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>
#include <string.h>

void makeInt(char *str, short vet[]);

int main (void)
{

	char cpf[10];
	short numCPF[9];
	unsigned short digitoA, digitoB;
	unsigned i, j, k;

	while (scanf(" %s", cpf) != EOF)
	{

		makeInt(cpf, numCPF);

		digitoA = 0;
		digitoB = 0;

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

			digitoA = digitoA % 11;
			if (digitoA == 10)
				digitoA = 0;

			digitoB = digitoB % 11;
			if (digitoB  == 10)
				digitoB = 0;

			for (i = 0; i  <  3; i++)
				printf("%hu", cpf[i] - 48);

			printf(".");

			for (i = 3; i  <  6; i++)
				printf("%hu", cpf[i] - 48);

			printf(".");

			for (i = 6; i  <  9; i++)
				printf("%hu", cpf[i] - 48);

			printf("-");

			printf("%hu%hu", digitoA, digitoB);

			printf("\n");

	}
}

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

	unsigned short i;

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

}
Copy The Code & Try With Live Editor

Input

x
+
cmd
000000000
111111111
354122447
569961340
169992467

Output

x
+
cmd
000.000.000-00
111.111.111-11
354.122.447-93
569.961.340-48
169.992.467-85

#2 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>

using namespace std;

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

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

   while (cin >> 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;

      cout << c[0] << c[1] << c[2] << ".";
      cout << c[3] << c[4] << c[5] << ".";
      cout << c[6] << c[7] << c[8] << "-";
      cout << b1 << b2 << endl;
   }

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

Input

x
+
cmd
000000000
111111111
354122447
569961340
169992467

Output

x
+
cmd
000.000.000-00
111.111.111-11
354.122.447-93
569.961.340-48
169.992.467-85

#3 Code Example with Javascript Programming

Code - Javascript Programming


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


class CPF {
	/** @param {number[]} ints */
	static generateFromIntegers(ints) {
		const digits = new Uint8Array(11)

		let res01 = 0
		let res02 = 0

		for (let index = 0; index  <  9; index++) {
			digits.set([ints[index] || 0], index)

			res01 += (index + 1) * digits[index]
			res02 += (9 - index) * digits[index]
		}

		digits[9] = (res01 % 11) % 10
		digits[10] = (res02 % 11) % 10

		return digits.join("")
	}

	/** @param {string} cpf*/
	static format(cpf) {
		return cpf.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, "$1.$2.$3-$4")
	}
}


function main() {
	const responses = []

	for (const line of input) {
		if (line === "") break // EOFile
		const generated = CPF.generateFromIntegers([...line].map(Number))
		const formatted = CPF.format(generated)

		responses.push(formatted)
	}

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

main()
Copy The Code & Try With Live Editor

Input

x
+
cmd
000000000
111111111
354122447
569961340
169992467

Output

x
+
cmd
000.000.000-00
111.111.111-11
354.122.447-93
569.961.340-48
169.992.467-85

#4 Code Example with Python Programming

Code - Python Programming


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

        s = 0
        for i in range(9):
            s += int(e[i]) * (9 - i)
        b2 = s % 11
        b2 = 0 if b2 == 10 else b2

        print('{}{}{}.{}{}{}.{}{}{}-{}{}'.format(e[0], e[1], e[2], e[3], e[4], e[5], e[6], e[7], e[8], b1, b2))
        
    except EOFError: break
Copy The Code & Try With Live Editor

Input

x
+
cmd
000000000
111111111
354122447
569961340
169992467

Output

x
+
cmd
000.000.000-00
111.111.111-11
354.122.447-93
569.961.340-48
169.992.467-85
Advertisements

Demonstration


Previous
#1770 Beecrowd Online Judge Solution 1717 Shuffle Solution in C, C++, Java, Js and Python
Next
#1787 Beecrowd Online Judge Solution 1787 URI Solution in C, C++, Java, Js and Python