URI Online Judge | 1786
SSN 2
By Alexandre Campos, UNIUBE 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 |
URI Online Judge Solution 1786 | SSN 2 in C++
#include <iostream>
#include <cstring>
#define for(i, n) for(int i = 0; i < (n); ++i)
using
namespace
std;
int
main(
int
argc,
char
const
*argv[])
{
int
b1, b2, l, r;
string s;
while
(getline(cin, s))
{
l = 1, r = 9, b1 = b2 = 0;
for
(i, 9)
{
b1 += (l * (s[i] -
'0'
));
b2 += (r * (s[i] -
'0'
));
l++; r--;
}
if
(b1 % 11 == 10) b1 = 0;
else
b1 %= 11;
if
(b2 % 11 == 10) b2 = 0;
else
b2 %= 11;
cout << s[0] << s[1] << s[2] <<
"."
<< s[3] << s[4] << s[5] <<
"."
<< s[6] << s[7] << s[8] <<
"-"
<< b1 << b2 << endl;
}
return
0;
}
No comments:
Write commentsTo know more about the problem, give us your valuable commment. We'll try to help you. Thanks