C#五道笔试题
1、 请编程实现一个冒泡排序算法?
class Program
{
static void Main(string[] args)
{
int[] test = { 5, 6, 2, 1, 8, 4, };
popSort.popSort1(test);
}
}
public static class popSort
{
public static void popSort1(int[] a)
{
int temp = 0;
for (int i = 0; i < a.Length - 1; i++)
for (int j = i + 1; j < a.Length; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
foreach (var n in a)
{
Console.WriteLine(n);
}
}
}
}
2、 产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复。
class Program
{
static void Main(string[] args)
{
ArrayList a = new ArrayList();
Random r = new Random();
for (int j = 0; j <= 100; j++)
{
int temp = r.Next(1, 101);
if (!a.Contains(temp))
{
a.Add(temp);
}
else
{
j--;
}
}
a.Sort();
foreach (var i in a)
{
Console.WriteLine(i);
}
Console.ReadKey();
}
}
3、 一列数的规则如下: 1、1、2、3、5、8、13、21、34… 求第30位数是多少, 用递归算法实现。
class Program
{
static void Main(string[] args)
{
int i = 30;
int temp = Fibonacci.Fib(i);
Console.WriteLine(temp);
Console.ReadKey();
}
}
public static class Fibonacci
{
public static int Fib(int a)
{
if (a <= 0)
return 0;
if (a < 3)
{
return 1;
}
else
{
return Fib(a - 1) + Fib(a - 2);
}
}
}
}
4、 给定任一32位无符号整型数,判断是否为2的n次方。
class Program
{
static void Main(string[] args)
{
int a = 8;
bool b = Judgement.Jud(a);
Console.WriteLine(b);
}
}
public static class Judgement
{
public static bool Jud(int i)
{
if(i==1)
{
return true;
}
else
{
do
{
if (i % 2 == 0)
i = i / 2;
else
return false;
}
while (i != 1);
}
return true;
}
}
}
5、 已知一个三位数加上另一个三位数之和为四位数,算式中数字恰好为0、1、2、3、4、5、6、7、8、9各出现一次,请编程来求这个算式
class Program
{
static void Main(string[] args)
{
int count=0;
for (int a = 203; a <= 595; a++)
for (int b = 987; b >= 595; b--)
{
int result = a + b;
if (isTrue(result, a, b))
{
count = count + 1;
Console.Write(a + " + ");
Console.Write(b + " = ");
Console.WriteLine(result);
}
}
Console.WriteLine(count);
Console.ReadKey();
}
public static bool isTrue(int i, int j, int k)
{
string str = "0123456789";
string s = i + "" + j + "" + k;
for (int t = 0; t < str.Length; t++)
{
if (!s.Contains(str[t]))
{
return false;
}
}
return true;
}
}
}