Advertisement
olekturbo

newrek2

Dec 18th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. ZADANIE 1////
  2. #include <stdio.h>
  3.  
  4. unsigned int newton(unsigned int, unsigned int);
  5.  
  6. int main(){
  7. unsigned int n, k;
  8. printf("Podaj n: ");
  9. scanf("%d", &n);
  10. for(k = 0; k <= n; k++) printf("%d ", newton(n,k));
  11. return 0;
  12. }
  13.  
  14. unsigned int newton(unsigned int n, unsigned int k){
  15. if(k == 0 || k == n) return 1;
  16. else return newton(n-1,k) + newton(n-1,k-1);
  17.  
  18. }
  19.  
  20. ZAD 2////
  21.  
  22. #include <stdio.h>
  23.  
  24. unsigned int newton(unsigned int, unsigned int);
  25. double Ksukcesow(unsigned int, unsigned int, double, double);
  26.  
  27. int main(){
  28. unsigned int n, k;
  29. double p, q;
  30. printf("Podaj liczbe prob: ");
  31. scanf("%d", &n);
  32. printf("Podaj liczbe sukcesow: ");
  33. scanf("%d", &k);
  34. do{
  35. printf("Podaj prawdopodobienstwo: (p nalezy do <0,1>) ");
  36. scanf("%lf", &p);
  37. }while(p < 0 || p > 1);
  38. q = 1-p;
  39.  
  40. printf("Prawdopodobienstwo %d sukcesow w %d probach wynosi: %lf", k, n, Ksukcesow(n, k, p, q));
  41. return 0;
  42. }
  43.  
  44. unsigned int newton(unsigned int n, unsigned int k){
  45. if(k == 0 || k == n) return 1;
  46. else return newton(n-1,k) + newton(n-1,k-1);
  47.  
  48. }
  49. double Ksukcesow(unsigned int n, unsigned int k, double p, double q){
  50. //potegowanie p
  51. int i;
  52. double pot = 1;
  53. double pot2 = 1;
  54. for(i = 0; i < k; i++) pot*=p;
  55. for(i = 0; i < n-k; i++) pot2*=q;
  56.  
  57. return newton(n,k) * pot * pot2;
  58.  
  59. }
  60.  
  61. ZAD3 ////
  62. #include <stdio.h>
  63.  
  64. unsigned int newton(unsigned int, unsigned int);
  65. double Cumulative(unsigned int, unsigned int, double, double);
  66.  
  67. int main(){
  68. unsigned int n, k;
  69. double p, q;
  70. printf("Podaj liczbe prob: ");
  71. scanf("%d", &n);
  72. printf("Podaj liczbe sukcesow: ");
  73. scanf("%d", &k);
  74. do{
  75. printf("Podaj prawdopodobienstwo: (p nalezy do <0,1>) ");
  76. scanf("%lf", &p);
  77. }while(p < 0 || p > 1);
  78. q = 1-p;
  79.  
  80. printf("Prawdopodobienstwo %d lub mniej sukcesow w %d probach wynosi: %lf", k, n, Cumulative(n, k, p, q));
  81. return 0;
  82. }
  83.  
  84. unsigned int newton(unsigned int n, unsigned int k){
  85. if(k == 0 || k == n) return 1;
  86. else return newton(n-1,k) + newton(n-1,k-1);
  87.  
  88. }
  89. double Cumulative(unsigned int n, unsigned int k, double p, double q){
  90. //potegowanie p
  91. int i;
  92. double wynik = 0;
  93. double pot = 1;
  94. double pot2 = 1;
  95. for(i = 0; i < k; i++) pot*=p;
  96. for(i = 0; i < n-k; i++) pot2*=q;
  97.  
  98. for(i = 0; i <= k; i++) wynik+=newton(n,i) * pot * pot2;
  99.  
  100. return wynik;
  101.  
  102. }
  103.  
  104. /////ZAD 4
  105.  
  106. #include <stdio.h>
  107. #include <math.h>
  108. double Hoeffding(unsigned int, unsigned int, double);
  109.  
  110. int main(void){
  111. unsigned int n, k;
  112. double p;
  113. printf("Podaj n,k p: ");
  114. scanf("%d", &n);
  115. scanf("%d", &k);
  116. scanf("%lf", &p);
  117. printf("%lf", Hoeffding(k,n,p));
  118. }
  119.  
  120. double Hoeffding(unsigned int k, unsigned int n, double p){
  121.  
  122. return exp((-2/n) * pow(n*p-k, 2));
  123. }
  124.  
  125. //ZADANIE 5
  126.  
  127. #include <stdio.h>
  128. #define MAX 100
  129. int tab[MAX][MAX];
  130.  
  131. int newton(int n, int k){
  132.  
  133. int i, j;
  134.  
  135. for(i = 1; i < MAX; i++){
  136.  
  137. for(j = 0; j < MAX; j++){
  138.  
  139. tab[i][j] = 0;
  140. }
  141. }
  142.  
  143. for(i = 0; i < MAX; i++) tab[i][0] = 1; //zerowa kolumna
  144. for(i = 0; i < MAX; i++) tab[i][i] = 1; //diagonala
  145.  
  146. for(i = 1; i < MAX; i++){
  147.  
  148. for(j = 1; j < MAX; j++){
  149.  
  150. tab[i][j] = tab[i-1][j-1] + tab[i-1][j];
  151. }
  152. }
  153.  
  154. return tab[n][k];
  155.  
  156. }
  157.  
  158. int main(void){
  159. int n, k;
  160. printf("Podaj n i k: ");
  161. scanf("%d%d", &n, &k);
  162. printf("%d", newton(n, k));
  163.  
  164. return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement