andruhovski icon

TaskD-2016

andruhovski | PRO | 05/10/16 07:32:09 PM UTC | 0 ⭐ | 1755 👁️ | Never ⏰ | []
C++ |

850 B

|

None

|

0 👍

/

0 👎

// Task04.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iostream>
using namespace std;
 
int _tmain(void)
{
    string str;
    getline(cin, str);
    auto len = str.length();
    vector<unsigned short> num(len);
    transform(begin(str), end(str), begin(num), [](char ch){ return ch - 48; });
    auto sum = 3 - accumulate(begin(num), end(num), 0) % 3;
    auto replace = false;
    for (auto i = 2; i >= 0; i--)
    {
        auto res = find_if(begin(num), end(num), [sum, i](unsigned short s_i){ return (s_i + sum + i * 3) < 10; });
        if (res != end(num))
        {
            *res += (sum + i * 3);
            replace = true;
            break;
        }
    }
    
    if (!replace) 
        num[len-1] -= sum;
    
    copy(begin(num), end(num), ostream_iterator<unsigned short>(cout));
    return 0;
}

Comments