85 lines
1.7 KiB
C++
85 lines
1.7 KiB
C++
/*
|
|
VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I."
|
|
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
|
|
*/
|
|
#include "stdafx.h"
|
|
#include <iostream>
|
|
#include <exception>
|
|
#include <string>
|
|
#include <chrono>
|
|
#include <vector>
|
|
#include <algorithm>
|
|
#include <queue>
|
|
#include <mutex>
|
|
#include <condition_variable>
|
|
#include <atomic>
|
|
|
|
/*
|
|
given an array representing a non-negative integer (ex: 123 represented
|
|
as [1,2,3]), return the next integer (output: [1,2,4]).
|
|
run through all edge cases (ex: [9,9,9,9,9,9,9,9] etc)
|
|
*/
|
|
|
|
int dig(char ch) {
|
|
return ch - '0';
|
|
}
|
|
|
|
char chr(int dig) {
|
|
return dig + '0';
|
|
}
|
|
|
|
std::string incr(std::string n) {
|
|
int carry = 1;
|
|
for (size_t i = n.size(); i-- > 0; ) {
|
|
int ds = dig(n[i]) + carry;
|
|
if (ds >= 10) {
|
|
carry = ds / 10;
|
|
ds %= 10;
|
|
}
|
|
else {
|
|
carry = 0;
|
|
}
|
|
n[i] = chr(ds);
|
|
}
|
|
|
|
if (carry) {
|
|
return chr(carry) + n;
|
|
}
|
|
else {
|
|
return n;
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
try {
|
|
auto begin = std::chrono::high_resolution_clock::now();
|
|
|
|
std::cout << incr("0") << std::endl;
|
|
std::cout << incr("1") << std::endl;
|
|
std::cout << incr("9") << std::endl;
|
|
std::cout << incr("10") << std::endl;
|
|
std::cout << incr("15") << std::endl;
|
|
std::cout << incr("19") << std::endl;
|
|
std::cout << incr("99") << std::endl;
|
|
std::cout << incr("9999999") << std::endl;
|
|
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
std::chrono::duration<double> seconds = end - begin;
|
|
std::cout << "Time: " << seconds.count() << std::endl;
|
|
return 0;
|
|
}
|
|
catch (const std::exception& e)
|
|
{
|
|
std::cerr << std::endl
|
|
<< "std::exception(\"" << e.what() << "\")." << std::endl;
|
|
return 2;
|
|
}
|
|
catch (...)
|
|
{
|
|
std::cerr << std::endl
|
|
<< "unknown exception." << std::endl;
|
|
return 1;
|
|
}
|
|
}
|