Advertisement
andruhovski

TaskD-2016

May 10th, 2016
1,326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. // Task04.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <string>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <numeric>
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. int _tmain(void)
  13. {
  14.     string str;
  15.     getline(cin, str);
  16.     auto len = str.length();
  17.     vector<unsigned short> num(len);
  18.     transform(begin(str), end(str), begin(num), [](char ch){ return ch - 48; });
  19.     auto sum = 3 - accumulate(begin(num), end(num), 0) % 3;
  20.     auto replace = false;
  21.     for (auto i = 2; i >= 0; i--)
  22.     {
  23.         auto res = find_if(begin(num), end(num), [sum, i](unsigned short s_i){ return (s_i + sum + i * 3) < 10; });
  24.         if (res != end(num))
  25.         {
  26.             *res += (sum + i * 3);
  27.             replace = true;
  28.             break;
  29.         }
  30.     }
  31.    
  32.     if (!replace)
  33.         num[len-1] -= sum;
  34.    
  35.     copy(begin(num), end(num), ostream_iterator<unsigned short>(cout));
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement