1、目录检索
【1】 给定一个非空字符串,找出不含有重复字符的最长子串的长度。【easy】
【2】用多态的思想求圆、矩形的周长和面积。【medium】
【3】冒泡排序。【easy】
【4】快速排序。【medium】
【5】一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少,用递归算法实现。【easy】
【6】用python编程,求系数分别为a、b、c 的二元一次方程的根。【easy】
【1】 给定一个字符串,找出不含有重复字符的最长子串的长度。
public int GetLongestSubstring(string str)
{
string tempStr = "";
int max = 1;
for (int i = 0; i < str.Length - 1; i++)
{
if (str.Length - i - 1 < max)
break;
else
{
tempStr += str[i];
for (int j = i + 1; j < str.Length; j++)
{
if (!tempStr.Contains(str[j])) //判断当前子串tempStr是否已经存在字符str[j]
{
tempStr += str[j];
max = max > tempStr.Length ? max : tempStr.Length;
}
else
break;
}
tempStr = ""; //置空
}
}
return max;
}
【2】用多态的思想求圆、矩形的周长和面积。
class MyPolymorphism
{
static void Main(string[] args)
{
Circle circle = new Circle(5);//利用构造函数初始化赋值,Circle可以换成Shape,因为Circle继承了Shape
double s1 = circle.GetArea();
double c1 = circle.GetCirc();
Console.WriteLine("圆面积为:{0},周长为{1}!", s1, c1);
Retangle retangle = new Retangle(3, 4);//利用构造函数传参
double s2 = retangle.GetArea();
double c2 = retangle.GetCirc();
Console.WriteLine("矩形面积为:{0},周长为{1}!", s2, c2);
Console.ReadKey();
}
//抽象类
public abstract class Shape
{
public abstract double GetCirc();
public abstract double GetArea();
}
public class Circle : Shape //继承
{
public Circle(double r)//构造函数,利用构造函数传参
{
R = r;
}
//自动属性
public double R { get; set; }
public override double GetCirc()//override 方法提供从基类继承的成员的新实现,GetCirc()称之为“重写基方法”
{
return 2 * Math.PI * R;
}
public override double GetArea()
{
return R * R * Math.PI;
}
}
public class Retangle : Shape
{
public Retangle(double length, double width)
{
Length = length;
Width = width;
}
double Length { get; set; }
double Width { get; set; }
public override double GetCirc()
{
return (Length + Width) * 2;
}
public override double GetArea()
{
return Length * Width;
}
}
}
【3】冒泡排序。
public int[] BubbleSort(int[] data)
{ // 小-->大
int temp;
for (int i = 0; i < data.Length - 1; i++)
{
bool flag = false;
for (int j = 0; j < data.Length - i - 1; j++)
{
if (data[j] > data[j + 1])
{
temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
flag = true;
}
}
if (!flag)
break;
}
return data;
}
【4】 快速排序
public int QuickSort(int[] data, int low, int high)
{ // 小==>大 一趟排序
int key = data[low];
//三个while条件很关键
while (low < high)
{
while (low < high && data[high] >= key)
{
high--;
}
data[low] = data[high];
while (low < high && data[low] <= key)
{
low++;
}
data[high] = data[low];
}
//结束时low=high
data[low] = key; //每一轮都有一个数“归位”
return high;
}
public void Sort(int[] data, int low, int high)
{
if (low >= high) //边界条件
return;
else
{
int index = QuickSort(data, low, high); //一趟排序获取index
Sort(data, low, index - 1); //递归对数组左边进行排序
Sort(data, index + 1, high); //递归对数组右边进行排序
}
}
【6】用python编程,求系数分别为a、b、c 的二元一次方程的根。
import math
def getvaule(a,b,c):
for i in (a,b,c):
if not isinstance(i,(int,float)):
raise ValueError('Invalid Input!')
if a==0:
return -c/b
else:
delta=b*b-4*a*c #△判别式
if delta==0:
return -b/(2*a)
elif delta<0:
print('none')
else:
x1=-(b+math.sqrt(delta))/(2*a)
x2=-(b-math.sqrt(delta))/(2*a)
return x1,x2