Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- //Maciej Januszewski, III semestr, PS2
- //Wyznacza wartość symbolu Newtona z definicji(iteracyjnie).
- //Najpierw trzeba zdefiniować własność silni.
- long silnia(int a)
- {
- long s;
- //Z własności wiemy, że 1!=1 oraz 0! = 1
- if (a == 0 || a == 1)
- {
- return 1;
- }
- else
- {
- s = 1;
- for (int i = 1; i <= a; i++)
- {
- s *= i;
- }
- }
- return s;
- }
- //Mając daną już własność silni, korzystam ze wzoru.
- long newton(int n, int k)
- {
- return silnia(n)/(silnia(k)*silnia(n-k));
- }
- //Wyznacza wartość symbolu Newtona rekurencyjnie ze wzoru.
- unsigned long int newton_rek(long int n ,long int k)
- {
- if ( n == k || k == 0 )
- {
- return 1;
- }
- if (k > n)
- {
- return 0;
- }
- else return newton_rek(n-1,k-1) + newton_rek(n-1,k);
- }
- //Program główny.
- int main()
- {
- int n = 0;
- int k = 0;
- long funkcja1 = 0;
- long funkcja2 = 0;
- FILE *f = fopen("In0101.txt", "r+");
- if (f == NULL)
- {
- printf("Nie udalo sie otworzyc pliku In0101.txt\n");
- return 1;
- }
- fread(n, sizeof(long), 1 , f);
- fread(k, sizeof(long), 1 , f);
- fclose(f);
- FILE *ff = fopen("Out0101.txt", "w+");
- if (ff == NULL)
- {
- printf("Nie udalo sie otworzyc pliku Out0101.txt\n");
- return 1;
- }
- funkcja1 = newton(n,k);
- funkcja2 = newton_rek(n,k);
- fwrite(funkcja1, sizeof(long), 1 , ff);
- fwrite(funkcja2, sizeof(long), 1 , ff);
- fclose(f);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement