Post date: Jul 01, 2013 12:5:54 PM
Problem
Print the numbers of form 2^i.5^j in increasing order. For eg:
1, 2, 4, 5, 8, 10, 16, 20
Solution
/*
============================================================================
Author : James Chen
Email : a.james.chen@gmail.com
Description : Print the numbers of form 2^i.5^j in increasing order
Created Date : 1-July-2013
Last Modified :
============================================================================
*/
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
static void PrintIncreasingNums(int n)
{
if (n < 0) return;
vector<int> vec;
vec.push_back(1);
int count2 = 0;
int count5 = 0;
while (vec.size() < n)
{
if (vec[count2] * 2 < vec[count5] * 5)
{
vec.push_back(vec[count2] * 2);
count2++;
}
else if (vec[count2] * 2 > vec[count5] * 5)
{
vec.push_back(vec[count5] * 5);
count5++;
}
else
{
vec.push_back(vec[count5] * 5);
count2++;
count5++;
}
}
for(auto it = vec.begin(); it != vec.end(); ++it){
cout << setw(5) << left << *it;
}
cout << endl;
}
void main()
{
PrintIncreasingNums(20);
}
Output
1 2 4 5 8 10 16 20 25 32 40 50 64 80 100 125 128 160 200 250
Press any key to continue . . .