Fibonacci数列
using System;
namespace Fibonacci
{
class Program
{
static void Main(string[] args)
{
int[] f = new int[30];
f[0] = 1;f[1] = 1;
//int [] f = { 1, 1 };
for(int i=2;i<f.Length-1;i++)
{
f[i] = f[i-1]+f[i-2];
if(i==28)
{
Console.WriteLine(f[i]);
}
}
}
}
倒序输出字符串
using System;
namespace daoxu
{
class Program
{
public string DaoXu(string[] a)
{
int i;
for ( i=a.Length-1;i>=0;i--)
{
Console.Write(a[i]+" ");
}
return a[i];
}
static void Main(string[] args)
{
Program n = new Program();
string[] strs = { "i", "am", "a", "good", "student" };
string s = n.DaoXu(strs);
Console.Write(s);
}
}
}
二分查找
using System;
namespace Binary_Search
{
class Program
{
static void Main(string[] args)
{
int low, high, mid,key=0;
low = 0;high = 7;
int []a={ 3,8,24,35,55,70,75,98};
while(low<=high)
{
mid=(low + high) / 2;
if(24<a[mid])
{
high = mid - 1;
key++;
}
if(24>a[mid])
{
low = mid + 1;
key++;
}
else
{
Console.WriteLine(key);
break;
}
}
}
}
}
冒泡排序
using System;
namespace MaoPao
{
class Program
{
static void Main(string[] args)
{
int[] array = { 6, 5, 9, 8, 7, 3, 2 };
for(int n=0;n<7;n++)
{
for(int m=0;m<7-n-1;m++)
{
if(array[m]<array[m+1])
{
int t = array[m];
array[m] = array[m + 1];
array[m + 1] = t;
}
}
}
for(int i=0;i<7;i++)
{
Console.WriteLine(array[i]);
}
}
}
}
5
using System;
namespace Arrays
{
class Program
{
static void Main(string[] args)
{
int max = 0;
int i=0, j=0;
int [,] a={{5,6,7,8},{4,3,2,1},{9,9,8,7}};
max = a[0, 0];
for (i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
if(a[i,j]>max)
{
max = a[i, j];
}
}
}
Console.WriteLine("最大值为" + max);
Console.WriteLine("行号为{0},列号为{1}", i-1, j-1);
}
}
}