123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Monte_Carlo_P
- {
- internal class MonteP
- {
- public void Method_Monte_Karlo(double k)
- {
- double n = 0;
- Console.WriteLine("Введите длину и ширину");
- double dlina = Convert.ToDouble(Console.ReadLine());
- double shirina = Convert.ToDouble(Console.ReadLine());
- double S0 = dlina * shirina;
- for (int i = 0; i < k; i++)
- {
- Random rand = new Random();
- double x = rand.NextDouble()*dlina;
- double y = rand.NextDouble()*shirina;
- if (Math.Pow((x - 1), 2) + Math.Pow((y - 1), 2) <= 1) n++;
- }
- double S = S0 * n / k;
- Console.WriteLine(S);
- }
- public void Primer0(double k)
- {
- double n = 0;
- double dlina = 8.0;
- double shirina = 5.0;
- double S0 = dlina * shirina;
- for (int i = 0; i < k; i++)
- {
- Random rand = new Random();
- double x = rand.NextDouble() * dlina;
- double y = rand.NextDouble() * shirina;
- if ((x*(10-x))/5 >= y && (x/3) <= y) n++;
- }
- double S = S0 * n / k;
- Console.WriteLine(S);
- }
- public void Primer1(double k)
- {
- double n = 0;
- double dlina = 15;
- double shirina = 5;
- double S0 = dlina * shirina;
- for (int i = 0; i < k; i++)
- {
- Random rand = new Random();
- double x = (rand.NextDouble() * dlina) - shirina;
- double y = rand.NextDouble();
- if (Math.Sin(x) >= y && y>=0) n++;
- }
- double S = 20 * n / k;
- Console.WriteLine(S);
- }
- public void Primer2(double k)
- {
- double n = 0;
- double dlina = 7;
- double shirina = 8;
- double S0 = dlina * shirina;
- for (int i = 0; i < k; i++)
- {
- Random rand = new Random();
- double x = rand.NextDouble() * dlina;
- double y = rand.NextDouble() * shirina;
- if ((x*(8-x))/2 >= y && y>= x/2) n++;
- }
- double S = S0 * n / k;
- Console.WriteLine(S);
- }
- public void Primer3(double k)
- {
- double n = 0;
- double dlina = 12;
- double shirina = 6;
- double S0 = dlina * shirina;
- for (int i = 0; i < k; i++)
- {
- Random rand = new Random();
- double x = rand.NextDouble() * dlina;
- double y = rand.NextDouble() * shirina;
- if (Math.Pow((x-6),2)/6 <= y && y<=6) n++;
- }
- double S = S0 * n / k;
- Console.WriteLine(S);
- }
- public void Primer4(double k)
- {
- double n = 0;
- double dlina = 10;
- double shirina = 4;
- double S0 = dlina * shirina;
- for (int i = 0; i < k; i++)
- {
- Random rand = new Random();
- double x = rand.NextDouble() * dlina;
- double y = rand.NextDouble() * shirina;
- if ((x*(12-x))/9 >= y && y>= x/5) n++;
- }
- double S = S0 * n / k;
- Console.WriteLine(S);
- }
- public void Primer5(double k)
- {
- double n = 0;
- double dlina = 8;
- double shirina = 4;
- double S0 = dlina * shirina;
- for (int i = 0; i < k; i++)
- {
- Random rand = new Random();
- double x = rand.NextDouble() * dlina;
- double y = rand.NextDouble() * shirina;
- if ((x * (8 - x)) / 4 >= y && y >= (8-x) / 8) n++;
- }
- double S = S0 * n / k;
- Console.WriteLine(S);
- }
- public void Primer6(double k)
- {
- double n = 0;
- double dlina = 2.9;
- double shirina = 0.99;
- double S0 = dlina * shirina;
- for (int i = 0; i < k; i++)
- {
- Random rand = new Random();
- double x = rand.NextDouble() * dlina;
- double y = rand.NextDouble() * shirina;
- if (Math.Sin(x) >= y && y>= Math.Pow((x-2),2)/2) n++;
- }
- double S = S0 * n / k;
- Console.WriteLine(S);
- }
- public void Choise()
- {
- Console.WriteLine("Введите k");
- double k = Convert.ToDouble(Console.ReadLine());
- Console.WriteLine("Расчет числа ПИ");
- Method_Monte_Karlo(k);
- Console.WriteLine("Расчет примера 0");
- Primer0(k);
- Console.WriteLine("Расчет примера 1");
- Primer1(k);
- Console.WriteLine("Расчет примера 2");
- Primer2(k);
- Console.WriteLine("Расчет примера 3");
- Primer3(k);
- Console.WriteLine("Расчет примера 4");
- Primer4(k);
- Console.WriteLine("Расчет примера 5");
- Primer5(k);
- Console.WriteLine("Расчет примера 6");
- Primer6(k);
- }
- }
- }
|