Post date: Sep 26, 2013 12:43:36 AM
Problem
/*
* Print the numbers between 30 to 3000. Microsoft
CONSTRAINT:
The numbers shouldnt contain digits either in incresing order or decreasing order.
FOLLOWING NOT ALLOWED
##123,234,345,1234,2345##increasing order,
##32,21,321,432,3210 etc##decresing order.
FOLLOWING ALLOWED:
243,27,578,2344 etc.,
Now see who ll code ths....
* */
Solution
/*
============================================================================
Author : James Chen
Email : a.james.chen@gmail.com
Description : Print the numbers between 30 to 3000. Microsoft
Created Date : 26-09-2013
Last Modified :
============================================================================
*/
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
for (int i = 30; i < 100; ++i)
{
string str = to_string(i);
bool increased = true;
bool decreased = true;
for (int j = 1; j < str.size(); ++j)
{
increased = increased && (str[j - 1] == str[j] - 1);
decreased = decreased && (str[j - 1] == str[j] + 1);
}
if (!increased && !decreased)
cout << i << endl;
}
return 0;
}
Output
30
31
33
35
36
37
38
39
40
41
42
44
46
47
48
49
50
51
52
53
55
57
58
59
60
61
62
63
64
66
68
69
70
71
72
73
74
75
77
79
80
81
82
83
84
85
86
88
90
91
92
93
94
95
96
97
99
. . .