forked from oeddyo/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanswer.cpp
More file actions
50 lines (38 loc) · 973 Bytes
/
answer.cpp
File metadata and controls
50 lines (38 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
using namespace std;
string number;
bool isValid(string num){
int sz = num.size();
bool should_double = true;
int check_sum = 0;
for(int i=sz-2; i>=0; i--){
int cur_digit = num[i]-'0'; //assume all are number digits
if(should_double==1){
cur_digit *= 2;
}
should_double = !should_double;
if(cur_digit>=10){
int tmp = cur_digit%10;
tmp += (cur_digit/10)%10;
cur_digit = tmp;
}
check_sum += cur_digit;
cout<<"adding digit "<<cur_digit<<endl;
}
check_sum += num[sz-1]-'0';
cout<<"debug: check sum = "<<check_sum<<endl;
if(check_sum % 10 == 0){
return true;
}else{
return false;
}
}
int main(){
if(isValid("")){
cout<<"Number "<<number<<" is valid!"<<endl;
}else{
cout<<"not valid"<<endl;
}
return 0;
}