记录一下遇到过的面试题

①、打印出2000-2500之间所有的闰年年份
- 年份必须为4的倍数
for (int a = 2000; a <= 2500; a++) { if (a % 4 == 0) { Console.WriteLine(a); } }
②、打印出1+2!+3!+…+20!的和,就是1-20的阶乘之和
第一种方法
static void Main(string[] args) { int sum = 0; for (int i = 1; i <= 20; i++) { sum += jieCheng(i); } Console.WriteLine(sum); Console.ReadKey(); } static int jieCheng(int n) { if (n == 1) { return 1; } else { return n * jieCheng(n - 1); } }
第二种方法
int sum = 0; int n = 20; for (int i = 1; i <= n; i++) { int temp = 1; for (int j = 1; j <= i; j++) { temp *= j; } sum += temp; } Console.WriteLine(sum); Console.ReadKey();
③、写出一个方法,参数是三个整数(x,y,z),按从小到大排序
第一种使用数组自带的排序
static void PaiXu(int x, int y, int z) { int[] num = new int[3]; num[0] = x; num[1] = y; num[2] = z; Array.Sort(num); Console.WriteLine("从小到大结果依次为:"); for (int i = 0; i < num.Length; i++) { Console.WriteLine(num[i]); } }
如果题目要求从用户输入的三个整数来进行排序的话,可以这样写
int[] num = new int[3]; Console.WriteLine("请输入三个整数"); for (int i = 0; i < num.Length; i++) { Console.WriteLine("请输入第{0}个数字", i + 1); num[i] = Convert.ToInt32(Console.ReadLine()); } //从小到大排序 Array.Sort(num); Console.WriteLine("数字从小到大依次为:"); for (int i = 0; i < num.Length; i++) { Console.WriteLine(num[i]); } Console.ReadKey();
④、1,1,2,3,5,8,…获取第30个数值
没有规定的话这个方便理解
int[] list = new int[30]; list[0] = 1; list[1] = 1; for (int i = 2; i < 30; i++) { list[i] = list[i - 1] + list[i - 2]; Console.WriteLine(list[i]); } Console.ReadKey();
必须使用递归的话可以这样写
static void Main(string[] args) { for (int i = 0; i < 30; i++) { Console.WriteLine(GetNum(i)); } Console.ReadKey(); } static int GetNum(int n) { if (n == 0 || n == 1) { return 1; } else { return GetNum(n - 1) + GetNum(n - 2); } }
⑤、最经典的冒泡排序
int tem = 0;
int[] list = { 1, 23, 12, 421, 31, 213, 42 };
for (int i = 0; i < list.Length - 1; i++)
{
for (int j = 0; j < list.Length - 1 - i; j++)
{
if (list[j] > list[j + 1])
{
tem = list[j];
list[j] = list[j + 1];
list[j + 1] = tem;
}
}
}
Console.ReadKey();
⑥、产生一个int数组,长度为100,并向其中随即插入1-100,且不能重复
List<int> lst = new List<int>();
Random r = new Random();
while (true)
{
int temp = r.Next(1, 101);
if (lst.Count == 100)
{
break;
}
if (!lst.Contains(temp))
{
lst.Add(temp);
}
}
for (int i = 0; i < lst.Count; i++)
{
Console.WriteLine(lst[i]);
}
Console.ReadKey();
⑦、打印九九乘法表
int i, j;
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= i; j++)
{
Console.Write("{0}*{1}={2,2} ", j, i, j * i);
}
Console.WriteLine();
}
Console.ReadKey();
⑧、无师自通的水仙花数,两种方式都可以取个十百位
for (int i = 100; i < 1000; i++)
{
//int n1 = i / 100 % 10;//百位
//int n2 = i / 10 % 10; //十位
//int n3 = i % 10;//个位
string num = i.ToString();
int n1 = int.Parse(num[0].ToString());//百位
int n2 = int.Parse(num[1].ToString());//十位
int n3 = int.Parse(num[2].ToString());//个位
if (n1 * n1 * n1 + n2 * n2 * n2 + n3 * n3 * n3 == i)
{
Console.WriteLine(i);
}
}
⑨、写一个简单的快速排序
static void Main(string[] args)
{
int[] arr = { 1, 21, 3, 24, 14, 4, 2, 43, 22, 65, 19, 23 };
QuickSort(arr, 0, arr.Length - 1);
foreach (var item in arr)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
public static void QuickSort(int[] arr, int left, int right)
{
if (left < right)
{
int pivotIndex = Partition(arr, left, right);
QuickSort(arr, left, pivotIndex - 1);
QuickSort(arr, pivotIndex + 1, right);
}
}
private static int Partition(int[] arr, int left, int right)
{
int pivot = arr[right]; // 选择最右边的元素作为基准
int i = left - 1;
for (int j = left; j < right; j++)
{
if (arr[j] < pivot)
{
i++;
//Swap(arr, i, j);
(arr[i], arr[j]) = (arr[j], arr[i]);
}
}
//Swap(arr, i + 1, right);
(arr[i+1], arr[right]) = (arr[right], arr[i+1]);
return i + 1;
}
private static void Swap(int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}