Sie sind auf Seite 1von 15

Day con chung dai nhat

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace dayconchungdainhat
{
class Program
{
static int lcs(string X, string Y, int i, int j)
{
if (i == 0 || j == 0)

return 0;

if (X[i - 1] == Y[j - 1])

return 1 + lcs(X, Y, i - 1, j - 1);


else

return max(lcs(X, Y, i, j - 1), lcs(X, Y, i - 1, j));


}

static int max(int a, int b)


{
if (a > b)
return a;
else
return b;
}
static void lcs1(string X, string Y, out int[,] c, out int[,] b)
{
int m = X.Length, n = Y.Length, i, j;
c = new int[m + 1, n + 1];
b = new int[m + 1, n + 1];
for (i = 0; i <= m; i++)
c[i, 0] = 0;
for (j = 0; i <= n; i++)
c[0, j] = 0;
for (i = 1; i <= m; i++)
{
for (j = 1; j <= n; j++)
{
if (X[i - 1] == Y[j - 1])
{
c[i, j] = c[i - 1, j - 1] + 1;
b[i, j] = 1;
}
else
{
c[i, j] = max(c[i, j - 1], c[i - 1, j]);
if (c[i, j] == c[i, j - 1])
b[i, j] = 1;
else
b[i, j] = -1;
}
}
Console.Write(c[i, j] + "," + b[i, j] + "\t");

}
Console.WriteLine();
Console.WriteLine("Day con chung dai nhat cua\n{0}\n{1}\n{2} gom:\n", X, Y,
c[m, n]);
}
static void DCLN(int[] a)

{
int s = 0, e = 0, s1 = 0, i = 0;
int MaxS = 0, MaxE = 0;
Console.WriteLine("\ni\t ai \t MaxE \t S1\t MaxS \t s\t e");
for (i = 0; i < a.Length; i++)
MaxE = MaxE + a[i];
if (MaxE > MaxS)
{
MaxS = MaxE;
s = s1;
e = i;

}
if (MaxE < 0)
{
MaxE = 0;
s1 = i + 1;
}
Console.WriteLine("\n{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}", i, a[i], MaxE, s1,
MaxS, s, e);
Console.WriteLine("Day con chung dai naht:{0}\n {1} {2}", MaxS, s, e);
for (i = s; i < e; i++)
{
Console.Write(a[i] + ",");
}
Console.Write(a[e]);

}
static void show_lcs(int[,] a, string X, string Y)
{
if (lcs(X, Y, X.Length, Y.Length) == 0)
Console.WriteLine("Không có dãy con chung");
else
{
for (int i = X.Length; i > 0; i--)
for (int j = Y.Length; j > 0; j--)
{
if (X[i - 1] == Y[j - 1])

{
Console.WriteLine("Day con chung la: {0}", X[i - 1]);
i--;
j--;
}
else
{
Console.WriteLine("Day con chung la: {0}", max((X[i - 1]),
Y[j - 1]));
}

}
}

static void Main(string[] args)


{
string X = "AGGTAB";
string Y = "GXTXAYB";
int m = X.Length;
int n = Y.Length;

Stopwatch sw = new Stopwatch();


sw.Start();
Console.WriteLine("Lenght of LCS is {0} \n", X, Y, lcs(X, Y, m, n));
sw.Stop();
Console.Write("Thoi gian chay t={0}", sw.Elapsed);
int[,] c;
lcs1(X, Y, out c, out b);
show_lcs(X, Y, a);
Console.ReadKey();
}
}
}

QuickSort
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TH2
{
class Program
{

public static void QuickSort(ref int[] x)


{
qs(x, 0, x.Length - 1);
}
public static void qs(int[] x, int left, int right)
{
int i, j;
int pivot, temp;
i = left;
j = right;
pivot = x[(left + right) / 2];
do
{
while ((x[i] < pivot) && (i < right)) i++;
while ((pivot < x[j]) && (j > left)) j--;
if (i <= j)
{
temp = x[i];
x[i] = x[j];
x[j] = temp;
i++; j--;
}
} while (i <= j);
if (left < j)
qs(x, left, j);
if (i < right)
qs(x, i, right);
}
public static void DisplayElements(ref int[] xArray, char status, string
sortname)
{
if (status == 'a')
Console.WriteLine("After sorting using algorithm:" + sortname);
else
Console.WriteLine("Before sorting");
for (int i = 0; i <= xArray.Length - 1; i++)
{
if ((i != 0) && (i % 10 == 0))
Console.Write("\n");
Console.Write(xArray[i] + " ");
}
Console.ReadLine();
}
static void MixDataUp(ref int[] x, Random rdn)
{
for (int i = 0; i <= x.Length - 1; i++)
{
x[i] = (int)(rdn.NextDouble() * x.Length);
}
}
static void Main(string[] args)
{
Console.WriteLine("Sorting Algorithms Demo Code\n\n");
const int nItems = 20;
Random rdn = new Random(nItems);
int[] xdata = new int[nItems];
MixDataUp(ref xdata, rdn);
DisplayElements(ref xdata, 'b', "");
QuickSort(ref xdata);
DisplayElements(ref xdata, 'a', "QuickSort");
Console.WriteLine("\n\n");
Console.ReadLine();
}
}
}
ATM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ATM
{
class Program
{
static int n;
static long[] a;
static long[] b;
static void sotienATM()
{
Console.Write("Nhap menh gia co trong ATM: ");
n = int.Parse(Console.ReadLine());
a = new long[n];
b= new long[n];
for(int i=0;i<n;i++)
{
Console.Write("Nhap menh gia: " + (i + 1) + "");
a[i] = long.Parse(Console.ReadLine());
Console.Write("Nhap so luong tuong ung: ");
b[i] = long.Parse(Console.ReadLine());
Console.Write("______________________");
}
}
static void sx()
{
long tg;
for(int i=0;i<n-1;i++)
for(int j=n-1;j>i;j--)
{
if(a[i]<a[j])
{
tg = a[i];
a[i] = a[j];
a[j] = tg;
tg = b[i];
b[i] = b[j];
b[j] = tg;
}
}
}
static void Main(string[] args)
{
sotienATM();
sx();
long m;
int j = 0;
Console.Write("Nhap so tien can rut: ");
m = long.Parse(Console.ReadLine());
for(int i=0;i<n;i++)
{
if(m-a[i]>=0 && b[i]>0)
while(m - a[i] >= 0 && b[i] > 0)
{
m = m - a[i];
b[i] = b[i] - 1;
j++;
}
if(j>0)
{
Console.Write("To tien menh gia: " + a[i] + "so luong rut " + j);
}
j = 0;
if (m < 0)
break;
}
Console.ReadKey();
}
}
}

Cai tui
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TTTham_Knack
{
class Program
{
class ThamLam
{
static void Nhap(out double[] v, out double[] w, out double[] c, out int n)
{
Console.Write("Nhap so luong mat hang co trong quan n=: ");
n = int.Parse(Console.ReadLine());
v = new double[n];
w = new double[n];
c = new double[n];
Random r = new Random();
for (int i = 0; i < n; i++)
{
v[i] = r.Next(1, 50);
}
Random rs = new Random();
for (int i = 0; i < n; i++)
w[i] = rs.Next(10000, 20000000);
for (int i = 0; i < n; i++)
c[i] = w[i] / v[i];
}
static void SapXep(double[] v, double[] w, double[] c)
{
double tg = 0;
for (int i = 0; i < c.Length; i++)
for (int j = c.Length - 1; j > i; j--)
{
if (c[i] < c[j])
{
tg = c[i];
c[i] = c[j];
c[j] = tg;
tg = v[i];
v[i] = v[j];
v[j] = tg;
tg = w[i];
w[i] = w[j];
w[j] = tg;
}
}
}
static void Chon(double[] v, double[] w, int m)
{
Console.Write("\nNhap khoi luong toi da ma cai tui ten cuop dung duoc M=
");
m = int.Parse(Console.ReadLine());
int z = 0;
double x = 0;
double y = 0;
Console.Write("Hang lay duoc la: ");
Console.Write("-----------------------------------------\n");
for (int i = 0; i < v.Length; i++)
{
if (x + v[i] <= m)
{
z++;
Console.WriteLine("Trong luong: " + v[i] + "\t |GiaTri: " +
w[i]);
x += v[i];
y += w[i];
}
}
if (z > 0)
{
Console.Write("\n");
Console.WriteLine("Vay voi khoi luong ma tui dung duoc la {0}kg thi
ten trom lay duoc toi da so hangvoi khoi luong va gia tri: ", m);
Console.Write("Tong khoi luong: {0}kg ", x);
Console.Write("Tong gia tri: {0}", y);
}
else
{
Console.Write("\nTui khong chua duoc vat nao!! ");
}
}
static void Hien(double[] v, double[] w, double[] c)
{
Console.Write("\nTrong luong ");
for (int i = 0; i < v.Length; i++)
{
Console.Write(" || ", v[i]);

}
Console.Write("\nGia tri ");
for (int i = 0; i < w.Length; i++)
{
Console.Write(" || ", w[i]);

}
Console.Write("\nTi so GT/TL ");
for (int i = 0; i < c.Length; i++)
{
Console.Write(" || ", c[i]);

}
}
static void Main(string[] args)
{
double[] a; double[] b; double[] c;
int n;
int m = 0;
Nhap(out a, out b, out c, out n);
SapXep(a, b, c);
Hien(a, b, c);

}
}
}
}

Fibonaci
De quy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace DayFibonasi
{
class Program
{
public static int f(int a) // Fibonacci

{
if (a == 0 || a == 1)
{
return a;
}
else
{
return (f(a - 2) + f(a - 1));
}
}

static void Main(string[] args)


{
int n;
Console.Write("Nhap vao gia tri cho n = ");
n = int.Parse(Console.ReadLine());
Stopwatch s = new Stopwatch();
s.Start();

for (int i=0; i<n; i++)

Console.WriteLine("Phan tu cua day fibonasci:{0} ", f(i));


s.Stop();
Console.Write("thoi gian chay {0}", s.Elapsed);

Console.ReadKey();
}
}
}
Lap
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace lap
{
class Program
{
public static int Giaithuatvonglap(int n)
{
int i, fb1 = 0, fb2 = 1;
int fb = 0;
for (i = 1; i < n; i++)
{

fb = fb1 + fb2;
fb1 = fb2;
fb2 = fb;

}
return fb;
}

static void Main(string[] args)


{
int n;
Console.WriteLine("nhap n:\n"); n = int.Parse(Console.ReadLine());

Console.WriteLine(" số fibonacci có thứ hạng {0} là {1}:", n,


Giaithuatvonglap(n));
Stopwatch s = new Stopwatch();
s.Start();
Giaithuatvonglap(n);

s.Stop();

Console.WriteLine("Thời gian: " + s.Elapsed + "\n");

Console.ReadKey();
}
}
}

Lap mang
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace lapmang
{
class Program
{
public static int Fib(int n)
{
int[] a = new int[n + 1];
a[0] = a[1] = 1;
int kq = 0;
for (int i = 2; i <= n; i++)
{
a[i] = a[i - 1] + a[i - 2];
kq = a[n];

}
return kq;
}
static void Main(string[] args)
{
int n;
Console.WriteLine("nhap n:\n"); n = int.Parse(Console.ReadLine());

Console.WriteLine("so hang {0} là {1}:", n, Fib(n));


Stopwatch s = new Stopwatch();
s.Start();
Fib(n);

s.Stop();

Console.WriteLine("Thời gian: " + s.Elapsed + "\n");

Console.ReadKey();
}
}
}
MergeSort
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace mergeSort
{

class Program
{ private static void Merge(ref int[]a, int l, int m, int r)
{
int[] b = new int[(r - 1) + 1];
int i = l;
int j = m + 1;
int k = 0;
while (i<=m && j<=r)
{
if (a[i]<a[j])
{
b[k] = a[i];
i++;

}
else
{
b[k] = a[i];
j++;
}
}
k++;
while(i<=m)
{
b[k] = a[i];
i++;
k++;
}
while(j<=r)
{
b[k] = a[j];
j++;
k++;
}
int bIndex = 0;
while(l<=r)
{
a[l] = b[bIndex];
l++;
bIndex++;
}
}
public static void MergeSort(ref int[]a, int l, int r)
{
if (l >= r) return;
int m = (l + r) / 2;
MergeSort(ref a, l, m);
MergeSort(ref a, m + 1, r);
Merge(ref a, l, m, r);
}

static void Main(string[] args)


{

int[] a = new int[10];

Random r = new Random();


for (int i = 0; i < 10; i++)
{
a[i] = r.Next(-100, 100);
Console.WriteLine("ket qua:{0}", a[i]);
}

Console.WriteLine("Ket qua: " + a);

Console.ReadKey();
}
}
}
Minmax
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Min_Max
{
class Program
{

public static void MinMax(int[] a, int l,int r,out int min,out int max)
{
int min1, max1, min2, max2;
if (l == r)
{
min = a[l];
max = a[l];
}
else
{

MinMax(a, l, (l + r) / 2, out min1,out max1);


MinMax(a, (l + r) / 2 + 1, r,out min2,out max2);
if (max1 > max2)
max = max1;
else
max = max2;
if (min1 < min2)
min = min1;
else
min = min2;

}
}
static void Main(string[] args)
{

int[] a = new int[100];


Array.Sort(a);

Random r = new Random();


for (int i = 0; i < 10; i++)
{
a[i] = r.Next(-100, 100);
Console.WriteLine("ket qua:{0}", a[i]);
}

Console.WriteLine("Ket qua (max,min): ({0},{1})");


Console.WriteLine("Thời gian: " + s.Elapsed + "\n");
Console.ReadKey();

}
}
}
DA thuc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace DATHUC
{
class dathuc
{
int[] a;
public void Nhap(out int[] a)
{
Random r = new Random();
Console.Write("ban da thuc n=");
int n = int.Parse(Console.ReadLine());
a = new int[n + 1];
for (int i = 0; i <= n; i++)
{
a[i] = r.Next(-10, 10);

}
}
public void Hien(int[] a)
{
Console.Write("da thuc nhu sau:P=");
for (int i = a.Length - 1; i > 0; i--)
{
Console.Write("{0}*x^{1}+", a[i], i);

}
Console.WriteLine(a[0]);
}
public double tinhdathuc1(int[] a, double x)
{
double p = a[0];
for (int i = 1; i < a.Length; i++)
{
p += a[i] * Math.Pow(x, i);

}
return p;
}
public double tinhdathuc2(int[] a, double x)
{
double p = a[0], tg = 1;
for (int i = 1; i < a.Length; i++)
{
tg = tg * x;
p += a[i] * tg;

}
return p;
}
public double tinhdathuc3(int[] a, double x)
{
double p = a[a.Length - 1];
for (int i = a.Length - 1; i > 0; i--)
{
p = p * x + a[i];

}
return p;
}

public void test()


{

Nhap(out a);
Hien(a);
Stopwatch st1 = new Stopwatch();
double x = 1;
st1.Start();
Console.WriteLine(" gtgt=" + tinhdathuc1(a, x));
st1.Stop();
Stopwatch st2 = new Stopwatch();
st2.Start();
Console.WriteLine(" gtgt=" + tinhdathuc2(a, x));
st2.Stop();
Stopwatch st3 = new Stopwatch();
st3.Start();
Console.WriteLine(" gtgt=" + tinhdathuc3(a, x));
st3.Stop();
Console.WriteLine("Voi n={0} TG chay 3 TT lan luot la:\n {1}\t{2}\t{3}",
a.Length - 1, st1.Elapsed, st2.Elapsed, st3.Elapsed);
Console.ReadLine();

class Program
{

public static void Main(string[] args)


{
dathuc dt = new dathuc();
dt.test();
}
}

}
}

Das könnte Ihnen auch gefallen