Recently in the review of discrete mathematics, this article is "Discrete Mathematics and its application" in the sixth edition of the third chapter algorithm, integers, and matrices involved in a number of algorithms, I thought about it, the light also does not play any role, so I wrote a bit, the following code is my own according to the Book of Pseudo-code written out, Preliminary verification No problem, if there is any problem please let me know, thank you!
One, decimal to arbitrary binary data conversion
According to the binary conversion rules: decimal to n binary integer part except n to write up, fractional division N rounding down writing, in fact, the integer part is used short division, this algorithm is actually a greedy algorithm of an example, because it is written upward, We can use the stack of this special data structure is very convenient to achieve upward writing:
#include <iostream>
#include <stack>
#include <stdlib.h>
std::stack<int> Converson (int n,int base) {
if (10==base)
Exit (1);
if (base<=1)
Exit (1);
std::stack<int> result;
int temp;
while (n) {
Temp=n%base;
Result.push (temp);
N/=base;
}
return result;
}
int main () {
int data=241;
std::stack<int> result;
Result=converson (data,2);
while (!result.empty ()) {
Std::cout<<result.top ();
Result.pop ();
}
std::cout<<std::endl;
return 0;
}
There is also the above code in the base is greater than the time should also pay attention to the stack is the method of representation, but the above code has improved, for example, the function returns a whole stack, the return will involve the variable scope life cycle, before returning the memory will open a temporary stack to save the original stack and then copied to the recipient, It consumes memory and time, so you can use references or pointers or global stacks to improve efficiency, not in detail here.
Second, the integer addition based on the binary
Imagine that when we add two decimal numbers, we add a bit from position to high, but if we have more than cardinality 10 we need to carry, and the binary is the same, according to the binary rules, when the two numbers add up to more than 2 we also need to carry, quite simple:
#include <algorithm>
#include <iostream>
#include <vector>
void Converson (int n,std::vector<int> &binary_result) {
int temp;
while (n) {
temp=n%2;
Binary_result.push_back (temp);
n/=2;
}
}
void Add (int m,int n,std::vector<int> &result) {
Std::vector<int> A;
Std::vector<int> b;
Converson (M,a);
Converson (N,B);
while (A.size () <b.size ())
A.push_back (0);
while (A.size () >b.size ())
B.push_back (0);
int c=0;
int D;
Std::vector<int>::iterator It1=a.begin (), It2=b.begin ();
while (It1!=a.end () && it2!=b.end ()) {
D= (*IT1+*IT2+C)/2;
Result.push_back (*IT1+*IT2+C-2*D);
C=d;
++it1;
++it2;
}
Result.push_back (c);
Std::reverse (Result.begin (), Result.end ());
}
int main () {
int a=45;
int b=65;
std::vector<int> result;
Add (A,b,result);
for (auto E:result)
std::cout<<e;
std::cout<<std::endl;
return 0;
}
Also here we rely on the vector data structure, more convenient, but note that the final addition after completion must be reversed;
Third, the multiplication based on binary
Here's a direct code:
#include <algorithm>
#include <iostream>
#include <vector>
void Converson (int n,std::vector<int> &binary_result) {
int temp;
while (n) {
temp=n%2;
Binary_result.push_back (temp);
n/=2;
}
}
int multiply (int m,int n) {
std::vector<int> result;
Std::vector<int> A;
Std::vector<int> b;
Converson (M,a);
Converson (N,B);
for (int i=0;i<b.size (); ++i) {
if (B[i])
Result.push_back (M<<i);
Else
Result.push_back (0);
}
int temp=0;
for (int i=0;i<result.size (); ++i)
Temp+=result[i];
return temp;
}
int main () {
int a=45;
int b=56;
Std::cout<<multiply (b) <<std::endl;
return 0;
}
Iv. integer division and modulo operations
Principle: Actually is also a greedy algorithm, a=q*d+r; When given a and d, as much as the D, minus the number of times quotient, the remainder is the residue;
#include <iostream>
void Division (int a,int D) {
int q=0;
int r=a;
while (r>d) {
R-=d;
++q;
}
if (a<0 && r>0) {
q=-(q+1);
R=d-r;
}
std::cout<<q<< ' <<r;
}
int main () {
Division (45,8);
return 0;
}
Discrete Mathematics--Number theory algorithm