-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
56 lines (42 loc) · 1.21 KB
/
main.cpp
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
50
51
52
53
54
55
56
#include <QCoreApplication>
#include <iostream>
#include <regex>
#include <functional>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::string str;
std::cin >> str;
std::regex reg("[+]{0,1}[1-9][0-9]{0,17}");
long double counter(0);
if( std::regex_match(str.begin(), str.end(), reg) )
{
str.erase( std::remove_if(str.begin(), str.end(), [&](const char &c){ return ( (c == '+') || (c == '\n') ); }), str.end() );
long double inputValue = std::stold( str );
std::function<long long( long long, int & )> recursiveCheck =
[&]( long long previousValue, int &number )->long long
{
++number;
long long nextValue = previousValue + number;
if( nextValue >= inputValue )
return nextValue;
else
return recursiveCheck( nextValue, number );
};
long long previousValue(0);
forever
{
++counter;
long double nextValue = previousValue + counter;
previousValue = nextValue;
if( nextValue >= inputValue )
break;
}
// long long calculatedValue = recursiveCheck( 0, counter );
// if( calculatedValue != inputValue )
if( previousValue != inputValue )
counter = 0;
}
std::cout << counter << std::endl;
return a.exec();
}