Problem
given y bytes and you can transfer only x bytes at once..
give a mathematical expression having only + - / *
which gives the number of iterations to copy y bytes. ( dont try giving modulo operator answers )
Solution
Assume y > 0
/*
============================================================================
Author : James Chen
Email : a.james.chen@gmail.com
Description : Compute iteration number
Created Data : 21-06-2013
Last Modified :
============================================================================
*/
#include <iostream>
#include <iomanip>
using namespace std;
unsigned int ComputerIterations(int y, int x)
{
return (y + (x - 1)) / x;
}
void DoTest(unsigned int y, unsigned int x)
{
cout << "y :";
cout << setw(4) << y << endl;
cout << "x :";
cout << setw(4) << x << endl;
int output = ComputerIterations(y, x);
cout << "output :";
cout << setw(4) << output << endl;
cout << "---------------------------" << endl;
}
int main(int argc, char* argv[])
{
int y = 100;
for(int x = 1; x < 20; ++x){
DoTest(y, x);
}
return 0;
}
Output
y : 100
x : 1
output : 100
---------------------------
y : 100
x : 11
output : 10
---------------------------
y : 100
x : 21
output : 5
---------------------------
y : 100
x : 31
output : 4
---------------------------
y : 100
x : 41
output : 3
---------------------------
y : 100
x : 51
output : 2
---------------------------
y : 100
x : 61
output : 2
---------------------------
y : 100
x : 71
output : 2
---------------------------
y : 100
x : 81
output : 2
---------------------------
y : 100
x : 91
output : 2
---------------------------
y : 100
x : 101
output : 1
---------------------------
y : 100
x : 111
output : 1
---------------------------
y : 100
x : 121
output : 1
---------------------------
y : 100
x : 131
output : 1
---------------------------
y : 100
x : 141
output : 1
---------------------------
y : 100
x : 151
output : 1
---------------------------
y : 100
x : 161
output : 1
---------------------------
y : 100
x : 171
output : 1
---------------------------
y : 100
x : 181
output : 1
---------------------------
y : 100
x : 191
output : 1
---------------------------
Press any key to continue . . .