/// <summary>
/// 写一个函数处理字符串,比如输入”I am a girl”,输出”girl a am I”
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
private static string StringConvert(string p)
{
string convertion = null;
Stack stack = new Stack();
int i = 0;
int j = 0;
for (int h = 0; h < p.Length; h++)
{
if (p.Substring(h,1)==" ")
{
j = h;
stack.Push(p.Substring(i,j-i));
i = j + 1;
}
}
stack.Push(p.Substring(i,p.Length-i));
int count = stack.Count;
for (int m = 0; m<count; m++)
{
if (m == 0)
{
convertion = (string)stack.Pop();
}
else
{
convertion = convertion+" " + (string)stack.Pop();
}
}
return convertion;
}
------------------------------------------------------------------------------------------------------------------------
求两个数的最大公约数【辗转相除法】.
当两个数都较大时,采用辗转相除法比较方便.其方法是:
以小数除大数,如果能整除,那么小数就是所求的最大公约数.否则就用余数来除刚才的除数;再用这新除法的余数去除刚才的余数.依此类推,直到一个除法能够整除,这时作为除数的数就是所求的最大公约数.
例如:求4453和5767的最大公约数时,可作如下除法.
5767÷4453=1余1314
4453÷1314=3余511
1314÷511=2余292
511÷292=1余219
292÷219=1余73
219÷73=3
于是得知,5767和4453的最大公约数是73.
辗转相除法适用比较广,比短除法要好得多,它能保证求出任意两个数的最大公约数.
/// <summary>
/// 辗转相除法:求两个数的最大公约数
/// </summary>
/// <param name="num1"></param>
/// <param name="num2"></param>
/// <returns></returns>
private static int Gcd(int num1, int num2)
{
int gcd=0;
if (num1==num2)
{
gcd = num1;
}
if (num1>num2)
{
int tmp = num1;
num1 = num2;
num2 = tmp;
}
if (num2 % num1 == 0)
{
gcd = num1;
}
else
{
int tmp = num1;
num1 = num2 % num1;
num2 = tmp;
gcd = Gcd(num1, num2);
}
return gcd;
}
P.S: 最小公倍数 = 二数中的大数/最大公约数)*小数
------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// 求int[]数组里面最大和第二大的数
/// </summary>
/// <param name="max"></param>
/// <param name="second"></param>
/// <param name="a"></param>
private static void MaxAndSecond(ref int max, ref int second, int[]a)
{
max = second = a[0];
for (int i = 0; i < a.Length; i++)
{
if (a[i]>max)
{
second = max;
max = a[i];
}
else if(a[i]>second)
{
second = a[i];
}
}
}
------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// 求1~N的素数
/// </summary>
/// <param name="p"></param>
private static void PrintPrime(int p)
{
if (p>=2)
{
System.Console.WriteLine("Parime:" + 2);
}
//不需要判断2以外的偶数,因为它们都不是素数,而i从非偶数3开始计数
for (int i = 3; i <= p; i = i + 2)
{
if (IsPrime(i))
{
System.Console.WriteLine("Parime:" + i);
}
}
Console.Read();
}
private static bool IsPrime(int n)
{
//1,2的处理
if (n==1)
{
return false;
}
if (n == 2)
{
return true;
}
//1,2以外的数的处理
else
{
for (int i = 2; i < Math.Sqrt(n) + 1; i++)
{
if (n % i == 0)
return false;
}
}
return true;
}
P.S: 1不是素数。
------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// 实现一个函数,要求在字符串strA中找出strB出现的次数
/// </summary>
/// <param name="strA"></param>
/// <param name="strB"></param>
/// <returns></returns>
private static int GetSubStrCount(char[] strA, char[] strB)
{
int count = 0;
for (int i = 0; i < strA.Length; i++)
{
int length = 0;
int iA = i;
for (int j = 0; j < strB.Length; j++)
{
if (strB[j] != strA[iA])
{
break;
}
else
{
length = j+1;
iA++;
}
}
if (length==strB.Length)
{
count++;
}
}
return count;
}