idk
This commit is contained in:
parent
da64db914e
commit
7715d07891
5
Makefile
5
Makefile
@ -23,6 +23,11 @@ sheet03:
|
|||||||
chmod +x build/sheet03
|
chmod +x build/sheet03
|
||||||
build/sheet03
|
build/sheet03
|
||||||
|
|
||||||
|
sheet04:
|
||||||
|
${BUILD_CMD} src/sheet04.cpp -o build/sheet04
|
||||||
|
chmod +x build/sheet04
|
||||||
|
build/sheet04
|
||||||
|
|
||||||
pizza:
|
pizza:
|
||||||
${BUILD_CMD} src/pizza.cpp -o build/pizza
|
${BUILD_CMD} src/pizza.cpp -o build/pizza
|
||||||
chmod +x build/pizza
|
chmod +x build/pizza
|
||||||
|
@ -24,6 +24,8 @@ int main(int argc, char **argv)
|
|||||||
pol2cart(0.5, 0, x, y);
|
pol2cart(0.5, 0, x, y);
|
||||||
std::cout << x << " " << y << '\n';
|
std::cout << x << " " << y << '\n';
|
||||||
pascal(argc, argv);
|
pascal(argc, argv);
|
||||||
|
std::cout << '\n';
|
||||||
|
pascal_mem(argc, argv);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,39 +79,45 @@ void pascal(int argc, char **argv)
|
|||||||
std::cout<< '\n';
|
std::cout<< '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unsigned fibonacci_nonrec(unsigned n)
|
|
||||||
{
|
|
||||||
unsigned fibo = 0;
|
|
||||||
unsigned prev_fibo = 1;
|
|
||||||
unsigned prev_prev_fibo = 0;
|
|
||||||
|
|
||||||
for(int i=2; i<=n; ++i)
|
|
||||||
{
|
|
||||||
fibo = prev_fibo + prev_prev_fibo;
|
|
||||||
prev_prev_fibo = prev_fibo;
|
|
||||||
prev_fibo = fibo;
|
|
||||||
}
|
|
||||||
|
|
||||||
return fibo;
|
|
||||||
}
|
|
||||||
void pascal_mem(int argc, char **argv)
|
void pascal_mem(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
if (argc != 2 ) { return; }
|
||||||
if (argc != 2 || std::stol(argv[1]) == 0) { return; }
|
|
||||||
|
|
||||||
unsigned depth = std::stol(argv[1]);
|
unsigned depth = std::stol(argv[1]);
|
||||||
depth = (depth*depth-depth)/2;
|
|
||||||
std::vector<unsigned long long> pascal(depth,0);
|
if(depth == 1 )
|
||||||
|
{
|
||||||
|
std::cout << "1\n";
|
||||||
|
}
|
||||||
|
else if(depth==0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned vector_depth = (depth*depth-depth)/2;
|
||||||
|
std::vector<unsigned long long> pascal(vector_depth,0);
|
||||||
pascal[0] = 1;
|
pascal[0] = 1;
|
||||||
for(unsigned long long i=1; i<depth; i=(i*i-i)/2)
|
std::cout << "1\n";
|
||||||
|
// pascal[1] = 1;
|
||||||
|
// pascal[2] = 1;
|
||||||
|
// std::cout << "1\t1\n";
|
||||||
|
unsigned rel_index = 2;
|
||||||
|
while(rel_index<=depth)
|
||||||
{
|
{
|
||||||
for(unsigned long long j=1; j<=i;++j)
|
unsigned index = (rel_index*rel_index-rel_index)/2;
|
||||||
|
unsigned prev_index = ((rel_index-1)*(rel_index-1)-(rel_index-1))/2;
|
||||||
|
unsigned next_index = ((rel_index+1)*(rel_index+1)-(rel_index+1))/2;
|
||||||
|
|
||||||
|
pascal[index] = 1;
|
||||||
|
std::cout << pascal[index] << '\t';
|
||||||
|
for(unsigned i=prev_index; i<index-1; ++i)
|
||||||
{
|
{
|
||||||
|
pascal[i+rel_index] = pascal[i] + pascal[i+1];
|
||||||
|
std::cout << pascal[i+rel_index] << '\t';
|
||||||
}
|
}
|
||||||
pascal[i] = pascal[i-1];
|
pascal[next_index-1] = 1;
|
||||||
++i;
|
std::cout << pascal[next_index-1] << '\n';
|
||||||
|
++rel_index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
32
src/sheet04.cpp
Normal file
32
src/sheet04.cpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#include <cstddef>
|
||||||
|
#include <array>
|
||||||
|
#include <initializer_list>
|
||||||
|
class vec
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
unsigned size;
|
||||||
|
double *vector;
|
||||||
|
public:
|
||||||
|
vec(size_t size) : size(size), vector(new double[size]) {}
|
||||||
|
vec(size_t size, double ival) : size(size), vector(new double[size])
|
||||||
|
{
|
||||||
|
for(size_t i=0; i<size;++i) { vector[i]=ival; }
|
||||||
|
}
|
||||||
|
|
||||||
|
vec(std::initializer_list<double> ilist) : size(ilist.size()), vector(new double[size]) {
|
||||||
|
size_t i = 0;
|
||||||
|
for(std::initializer_list<double>::iterator itr = ilist.begin(); itr!=ilist.end();++itr)
|
||||||
|
{
|
||||||
|
vector[i++] = *itr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~vec() {delete[] vector; }
|
||||||
|
vec(const vec &m) : vector(new double[m.size])
|
||||||
|
{
|
||||||
|
for(size_t i=0; i<m.size;++i)
|
||||||
|
{
|
||||||
|
vector[i] = m.vector[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user