From ef0ce890afa1517011859ebe8c240178fdd154ed Mon Sep 17 00:00:00 2001 From: JBatzill Date: Tue, 25 Nov 2014 17:04:02 +0100 Subject: Create bitOps.cpp --- sonstiges/bitOps.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 sonstiges/bitOps.cpp diff --git a/sonstiges/bitOps.cpp b/sonstiges/bitOps.cpp new file mode 100644 index 0000000..df8cd8f --- /dev/null +++ b/sonstiges/bitOps.cpp @@ -0,0 +1,22 @@ +[lsb: 0-th bit, msb: n-th bit,] + +Get j-th bit: +(a & (1 << j)) != 0 + +Set j-th bit: +a |= (1 << j) + +Clear j-th bit: +a &= ~(1 << j) + +Toggle j-th bit: +a ^= (1 << j) + +Get value of first set bit: +(a & -a) + +Turn on all bits: +a = -1 + +Turn on first n bits: +a = (1 << n) - 1 -- cgit v1.2.3 From cbcbb2c0690ab28b4c2b7b3e2c33a56b306859a5 Mon Sep 17 00:00:00 2001 From: JBatzill Date: Tue, 25 Nov 2014 17:04:37 +0100 Subject: Update sonstiges.tex --- sonstiges/sonstiges.tex | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sonstiges/sonstiges.tex b/sonstiges/sonstiges.tex index 375dc6b..e7fd5cc 100644 --- a/sonstiges/sonstiges.tex +++ b/sonstiges/sonstiges.tex @@ -16,3 +16,6 @@ Wenn die Eingabe aus einem kleinen Intervall $\left[0, n\right)$ stammt ist Buck \subsubsection{LSD-Radixsort} \lstinputlisting{sonstiges/radixsort.cpp} + +\subsubsection{Bit Operations} +\lstinputlisting{sonstiges/bitOps.cpp} -- cgit v1.2.3 From d7f768b65199780e4779c687ca31ed09f9d6fb8e Mon Sep 17 00:00:00 2001 From: JBatzill Date: Tue, 25 Nov 2014 17:05:23 +0100 Subject: Update toDo.txt --- toDo.txt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/toDo.txt b/toDo.txt index 3395f15..43482da 100644 --- a/toDo.txt +++ b/toDo.txt @@ -1,6 +1,4 @@ - bitonic tsp - josephus problem - min cost max flow -- roman numerals -- NIM GAMES (WICHTIG) -- bit operationen (WICHTIG) \ No newline at end of file +- bit operationen (WICHTIG) -- cgit v1.2.3 From b6f8490a4d50486042624a26fa7cec8eb69bbc9d Mon Sep 17 00:00:00 2001 From: kittobi1992 Date: Tue, 25 Nov 2014 17:06:30 +0100 Subject: Roman literal converting --- sonstiges/Roman.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 sonstiges/Roman.cpp diff --git a/sonstiges/Roman.cpp b/sonstiges/Roman.cpp new file mode 100644 index 0000000..195f833 --- /dev/null +++ b/sonstiges/Roman.cpp @@ -0,0 +1,40 @@ +map m; map o; +int num[7] = {1000,500,100,50,10,5,1}; + +void buildMap() { + m['M'] = 1000; m['D'] = 500; m['C'] = 100; m['L'] = 50; m['X'] = 10; m['V'] = 5; m['I'] = 1; m[' '] = 0; + o[1000] = 'M'; o[500] = 'D'; o[100] = 'C'; o[50] = 'L'; o[10] = 'X'; o[5] = 'V'; o[1] = 'I'; +} + +int convertToInt(string &s) { + int res = m[s[0]]; + for(int i = 1; i < s.size(); i++) { + if(m[s[i-1]] < m[s[i]]) + res -= 2*m[s[i-1]]; + res += m[s[i]]; + } + return res; +} + +string convertToRoman(int n) { + string roman = ""; + for(int i = 0; i < 7; i++) { + while(n >= num[i]) { + roman += o[num[i]]; + n -= num[i]; + } + } + int pos = roman.find("CCCC"); + if(pos != string::npos) roman.replace(pos,4,"CD"); + pos = roman.find("XXXX"); + if(pos != string::npos) roman.replace(pos,4,"XL"); + pos = roman.find("IIII"); + if(pos != string::npos) roman.replace(pos,4,"IV"); + pos = roman.find("DCD"); + if(pos != string::npos) roman.replace(pos,3,"CM"); + pos = roman.find("LXL"); + if(pos != string::npos) roman.replace(pos,3,"XC"); + pos = roman.find("VIV"); + if(pos != string::npos) roman.replace(pos,3,"IX"); + return roman; +} -- cgit v1.2.3 From 083aca7f730fec966e384f80a902b062f4c3b799 Mon Sep 17 00:00:00 2001 From: kittobi1992 Date: Tue, 25 Nov 2014 17:07:12 +0100 Subject: Adding Roman Literal Converting --- sonstiges/sonstiges.tex | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sonstiges/sonstiges.tex b/sonstiges/sonstiges.tex index e7fd5cc..8960a8a 100644 --- a/sonstiges/sonstiges.tex +++ b/sonstiges/sonstiges.tex @@ -19,3 +19,6 @@ Wenn die Eingabe aus einem kleinen Intervall $\left[0, n\right)$ stammt ist Buck \subsubsection{Bit Operations} \lstinputlisting{sonstiges/bitOps.cpp} + +\subsubsection{Roman-Literal-Converting} +\lstinputlisting{sonstiges/Roman.cpp} -- cgit v1.2.3