diff options
Diffstat (limited to 'Random')
-rw-r--r-- | Random/mandelbrot.c | 55 | ||||
-rw-r--r-- | Random/matrixTeste.c | 40 | ||||
-rw-r--r-- | Random/mergesort.cpp | 49 |
3 files changed, 144 insertions, 0 deletions
diff --git a/Random/mandelbrot.c b/Random/mandelbrot.c new file mode 100644 index 0000000..9f8a796 --- /dev/null +++ b/Random/mandelbrot.c @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <stdlib.h> +#include <complex.h> +#include <math.h> +#define ite 500 + +double map(double x, double p1, double p2, double c1, double c2); +double mandel(double y, double x); + +int main(){ + const int dimx = 800, dimy = 800; + int i, j; + FILE *fp = fopen("out.ppm", "wb"); + + fprintf(fp, "P6\n%d %d\n255\n", dimx, dimy); + int c; + unsigned char color[3]; + unsigned char temp; + for(j=-dimy/2; j<dimy/2; ++j){ + for(i=-dimx/2; i<dimx/2; ++i){ + temp = (int)mandel(map(j, -dimx/2, dimx/2, -2, 2), map(i, -dimx/2, dimx/2, -2, 2)); + color[0] = temp; + color[1] = temp; + color[2] = temp; + fwrite(color, 3, 1, fp); + } + } + + fclose(fp); + return 0; +} + +double mandel(double y, double x){ + int i; + double ca = x, + cb = y; + double zx, zy, tzx, tzy; + zx = x; + zy = y; + for(i=0; i<ite; ++i){ + tzx = zx; + tzy = zy; + zx = tzx*tzx - tzy*tzy + ca; + zy = 2*tzx*tzy + cb; + if(zx*zx + zy*zy > 4) + break; + } + + return map(i, ite, 0, 0, 255); +} + + +double map(double x, double p1, double p2, double c1, double c2){ + return 1.0*(x-p1)*(c2-c1)/(p2-p1) + c1; +}
\ No newline at end of file diff --git a/Random/matrixTeste.c b/Random/matrixTeste.c new file mode 100644 index 0000000..436e9c9 --- /dev/null +++ b/Random/matrixTeste.c @@ -0,0 +1,40 @@ +#include <stdio.h>
+
+void solve(int t){
+ int a, i, j , k, n;
+ int matrix[101][101];
+ a=0;
+ if (!(t%2))
+ n = t/2;
+ else
+ n = t/2 + 1;
+ for (k = 0; k<n; ++k){
+ for (i = a; i < t-a; ++i){
+ for (j = a; j < t-a; ++j){
+ matrix[i][j]=a+1;
+ }
+ }
+ ++a;
+ }
+
+
+ for (i = 0; i < t; ++i){
+ for (j = 0; j < t; ++j){
+ printf("%d ", matrix[i][j]);
+ }
+ printf("\n");
+ }
+ printf("\n");
+}
+
+
+int main(){
+
+ int t;
+ do{
+ scanf("%d", &t);
+ solve(t);
+ }while(t!=0);
+
+ return 0;
+}
diff --git a/Random/mergesort.cpp b/Random/mergesort.cpp new file mode 100644 index 0000000..82bd33b --- /dev/null +++ b/Random/mergesort.cpp @@ -0,0 +1,49 @@ +#include <bits/stdc++.h>
+using namespace std;
+
+
+void merge(int *A, int p, int q, int r){
+ int n1 = q - p + 1;
+ int n2 = r - q;
+ int L[n1];
+ int R[n2];
+
+ for(int i = 0; i<n1; ++i)
+ L[i] = A[p+i];
+ for(int j = 0; j<n2; ++j)
+ R[j] = A[q+1+j];
+
+ int i = 0, j = 0;
+
+ for(int k = p; k <= r; ++k){
+ if(L[i] <= R[j] && i < n1){
+ A[k] = L[i];
+ ++i;
+ }else{
+ A[k] = R[j];
+ ++j;
+ }
+ }
+
+}
+void merge_sort(int *A, int p, int r){
+ if(p<r){
+ int q = (p+r)/2;
+ merge_sort(A, p, q);
+ merge_sort(A, q+1, r);
+ merge(A, p, q, r);
+ }
+}
+
+int main(){
+
+ int A[10]={9,8,7,6,5,4,3,2,1,0};
+ int p = 0, r = 9;
+ merge_sort(A, p, r);
+
+ for(int i = 0; i < r+1; ++i)
+ printf("%d|", A[i]);
+
+ cout << "\n";
+ return 0;
+}
\ No newline at end of file |