Skip to content

Commit 21e3f88

Browse files
committed
Barclays Challenge 500
1 parent d0d7438 commit 21e3f88

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

FloatingPoint.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <queue>
8+
#include <cstring>
9+
#include <climits>
10+
#include <cstdio>
11+
#include <cmath>
12+
using namespace std;
13+
typedef pair<int,int> pi;
14+
typedef set<int> si;
15+
typedef vector<int> vi;
16+
typedef vector<vi> vvi;
17+
typedef vector<string> vs;
18+
19+
class FloatingPoint{
20+
public:
21+
int representations(int number, int mantissa, int exponent) {
22+
int first = 0, last = log2(number);
23+
while(!( (1<<first) & number )) first++;
24+
cout<<first<<" "<<last<<endl;
25+
int total = 0;
26+
while(first >= 0){
27+
if((last - first + 1) <= mantissa && first < (1<<exponent)) total++;
28+
first--;
29+
}
30+
return total;
31+
}
32+
33+
// BEGIN CUT HERE
34+
public:
35+
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
36+
private:
37+
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
38+
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
39+
void test_case_0() { int Arg0 = 24; int Arg1 = 4; int Arg2 = 2; int Arg3 = 3; verify_case(0, Arg3, representations(Arg0, Arg1, Arg2)); }
40+
void test_case_1() { int Arg0 = 1; int Arg1 = 3; int Arg2 = 3; int Arg3 = 1; verify_case(1, Arg3, representations(Arg0, Arg1, Arg2)); }
41+
void test_case_2() { int Arg0 = 8; int Arg1 = 3; int Arg2 = 3; int Arg3 = 3; verify_case(2, Arg3, representations(Arg0, Arg1, Arg2)); }
42+
void test_case_3() { int Arg0 = 16; int Arg1 = 5; int Arg2 = 2; int Arg3 = 4; verify_case(3, Arg3, representations(Arg0, Arg1, Arg2)); }
43+
void test_case_4() { int Arg0 = 17408; int Arg1 = 10; int Arg2 = 10; int Arg3 = 6; verify_case(4, Arg3, representations(Arg0, Arg1, Arg2)); }
44+
45+
// END CUT HERE
46+
47+
};
48+
49+
// BEGIN CUT HERE
50+
int main(){
51+
52+
FloatingPoint ___test;
53+
___test.run_test(-1);
54+
}
55+
// END CUT HERE

FloatingPoint.html

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><p>
2+
A <i>floating point</i> representation of a number in <i>base</i> <tt>b</tt> is written as <tt>M * b<sup>E</sup></tt>, where M is called the <i>mantissa</i> and E is the <i>exponent</i>. Depending on the allowed values for <tt>M</tt> and <tt>E</tt>, a number may have multiple possible representations. In this problem, we will work with base 2, and we will limit M and E to be unsigned integers, each with a fixed number of bits. You must determine the number of possible representations a given number has in such a system.
3+
</p>
4+
<p>
5+
For example, a 4-bit mantissa has a range of 0 to 15, inclusive, and a 2-bit exponent has a range of 0 to 3, inclusive. Using this system, the number 24 can be represented as: 12*2<sup>1</sup>, 6*2<sup>2</sup>, and 3*2<sup>3</sup>. Note that every time the mantissa is doubled or halved, the exponent is decremented or incremented by 1, respectively. Given an int <b>number</b>, and the sizes in bits of the <b>mantissa</b> and <b>exponent</b>, return the number of floating point representations that are possible for that <b>number</b>.
6+
</p>
7+
</td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Class:</td><td>FloatingPoint</td></tr><tr><td>Method:</td><td>representations</td></tr><tr><td>Parameters:</td><td>int, int, int</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int representations(int number, int mantissa, int exponent)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td colspan="2"><h3>Limits</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Time limit (s):</td><td>2.000</td></tr><tr><td>Memory limit (MB):</td><td>64</td></tr></table></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>number</b> will be between 1 and 1000000000 (10<sup>9</sup>), inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>mantissa</b> will be between 1 and 20, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>exponent</b> will be between 1 and 20, inclusive.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>24</pre></td></tr><tr><td><pre>4</pre></td></tr><tr><td><pre>2</pre></td></tr></table></td></tr><tr><td><pre>Returns: 3</pre></td></tr><tr><td><table><tr><td colspan="2">Example from the problem statement.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>1</pre></td></tr><tr><td><pre>3</pre></td></tr><tr><td><pre>3</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1</pre></td></tr><tr><td><table><tr><td colspan="2">The number 1 can be represented in only one way (regardless of the mantissa and exponent sizes).</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>8</pre></td></tr><tr><td><pre>3</pre></td></tr><tr><td><pre>3</pre></td></tr></table></td></tr><tr><td><pre>Returns: 3</pre></td></tr><tr><td><table><tr><td colspan="2">To represent the number 8 we can choose (ordered by exponent) from: 8*2<sup>0</sup>, 4*2<sup>1</sup>, 2*2<sup>2</sup>, 1*2<sup>3</sup>. The exponent has the range for larger numbers, but the smallest mantissa is used in the last case in this list.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>16</pre></td></tr><tr><td><pre>5</pre></td></tr><tr><td><pre>2</pre></td></tr></table></td></tr><tr><td><pre>Returns: 4</pre></td></tr><tr><td><table><tr><td colspan="2">To represent the number 16 we can choose (ordered by exponent) from: 16*2<sup>0</sup>, 8*2<sup>1</sup>, 4*2<sup>2</sup>, and 2*2<sup>3</sup>. Here the next possibility (1*2<sup>4</sup>) would require an exponent that cannot be represented with 3 bits.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>17408</pre></td></tr><tr><td><pre>10</pre></td></tr><tr><td><pre>10</pre></td></tr></table></td></tr><tr><td><pre>Returns: 6</pre></td></tr><tr><td><table><tr><td colspan="2">Note that 17408 = 17*2<sup>10</sup>. Thus the number 17 (or 1001 in binary notation) can be shifted to fit in 6 positions within the 10 mantissa bits (using leading or trailing zeros): from 17*2<sup>10</sup> to 544*2<sup>5</sup>. Any exponent less than 5 would overflow the mantissa.</td></tr></table></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>

0 commit comments

Comments
 (0)