Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // lecture6_hw2
- //
- // Created by Михаил Мустакимов on 16.11.17.
- // Copyright © 2017 Mikhail Mustakimov. All rights reserved.
- //
- #include <iostream>
- #include <string>
- using namespace std;
- short calcCheckNum(short n, short ln) {
- short tmp = n * ((ln % 2 == 0)? 1 : 2);
- tmp = (tmp > 9)? (tmp-9)%10 : tmp;
- return tmp;
- }
- int main(int argc, const char * argv[]) {
- string number;
- getline(cin, number);
- long sum = 0;
- long ln = 0;
- long xcount = 0;
- short xln = 0;
- for (long i=number.length()-1; i>=0; i--) {
- char ch = number[i];
- if (ch >= '0' && ch <= '9') {
- short n = ch - '0';
- sum += calcCheckNum(n, ln++);
- }
- if (ch == 'X' || ch == 'x'){
- xcount++;
- xln = ln++;
- }
- }
- if (xcount != 1){
- cout << "WRONG";
- return 0;
- }
- for (int i=0; i<10; i++) {
- long tmp = sum + calcCheckNum(i, xln);
- if (tmp % 10 == 0) {
- cout << i << endl;
- break;
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment