Skip to content

Instantly share code, notes, and snippets.

@antonio-abrantes
Created May 24, 2018 11:22
Show Gist options
  • Save antonio-abrantes/eac3de9c38a6ea8e1eff32b4aecdec62 to your computer and use it in GitHub Desktop.
Save antonio-abrantes/eac3de9c38a6ea8e1eff32b4aecdec62 to your computer and use it in GitHub Desktop.
Validar CPF em C++ - [URI - 1769]
#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;
int main(int argc, char const *argv[])
{
int l, r, _l, _r;
bool b1, b2, b3, b4;
string cpf;
while(getline(cin, cpf))
{
b1 = b2 = b3 = b4 = false;
l = 1;
r = 9;
_l = _r = 0;
for (int i = 0; i < 11; ++i)
{
if(i != 3 && i != 7){
_l += (l * (cpf[i] - '0'));
_r += (r * (cpf[i] - '0'));
l++; r--;
}
}
if(((cpf[12] - '0') == (_l % 11)) && ((cpf[13] - '0') == (_r % 11)))
b1 = true;
if(((cpf[12] - '0') == (_l % 11)) && ((cpf[13] - '0' == 0 && _r % 11 == 10)))
b2 = true;
if(((cpf[13] - '0') == (_r % 11)) && ((cpf[12] - '0' == 0 && _l % 11 == 10)))
b3 = true;
if(((cpf[12] - '0' == 0 && _l % 11 == 10)) && ((cpf[13] - '0' == 0 && _r % 11 == 10)))
b4 = true;
if(b1 || b2 || b3 || b4)
puts("CPF valido");
else
puts("CPF invalido");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment