C#面试题伪代码
收集了几个常见的要求写伪代码的面试题:
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 Console.WriteLine("Hello World!"); 6 DoBubbleSorting(); 7 Calc(6); 8 CalcFibonacciSequence(8); 9 DoIsPower(); 10 TestStringLength(); 11 TestStruct(); 12 } 13 14 15 /// <summary> 16 /// 面试题1、冒泡排序 17 /// </summary> 18 public static void DoBubbleSorting() 19 { 20 int[] array = {3, 1, 9, 5, 6, 7}; 21 22 Console.WriteLine("排序前数字:"); 23 Console.WriteLine(string.Join(",", array)); 24 for (int i = 0; i < array.Length; i++) 25 { 26 for (int j = i + 1; j < array.Length; j++) 27 { 28 if (array[i] > array[j]) 29 { 30 int tmp = array[j]; 31 array[j] = array[i]; 32 array[i] = tmp; 33 } 34 } 35 } 36 Console.WriteLine("排序后数字:"); 37 Console.WriteLine(string.Join(",",array)); 38 } 39 40 /// <summary> 41 /// 面试题2、求以下表达式的值,写出您想到的一种或几种实现方法:1-2+3-4+……+m 42 /// </summary> 43 public static void Calc(int count) 44 { 45 Console.WriteLine("求以下表达式的值,写出您想到的一种或几种实现方法:1-2+3-4+……+m"); 46 int num = 0; 47 for (int i = 1; i <= count; i++) 48 { 49 if (i % 2==1) 50 { 51 num += i; 52 } 53 else 54 { 55 num -= i; 56 } 57 } 58 Console.WriteLine($"{count}个数值为:"+num); 59 } 60 61 /// <summary> 62 /// 面试题3、一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少,用递归算法实现 63 /// </summary> 64 public static void CalcFibonacciSequence(int num) 65 { 66 Console.WriteLine("一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少,用递归算法实现"); 67 Console.WriteLine($"第{num}个数是:"+Fibonacci(num)); 68 } 69 70 static int Fibonacci(int num) 71 { 72 if (num > 0 && num <= 2) 73 { 74 return 1; 75 } 76 else 77 { 78 return Fibonacci(num - 1) + Fibonacci(num - 2); 79 } 80 } 81 82 public static void DoIsPower() 83 { 84 Console.WriteLine("现有一个整数number,请写一个方法判断这个整数是否是2的N次方,以4(100) 7(0111) 8(1000)为例"); 85 86 Console.WriteLine("4 & 3 --> 100 & 011 = 0"); 87 Console.WriteLine("7 & 6 --> 0111 & 0110 != 0"); 88 Console.WriteLine("8 & 7 --> 1000 & 0111 = 0"); 89 90 Console.WriteLine(GetFlag(5)?"":"5不是2的N次方"); 91 Console.WriteLine(GetFlag(16) ? "16是2的N次方" : ""); 92 } 93 94 public static bool GetFlag(int num) 95 { 96 return (num > 0) && (num & num - 1) == 0; 97 } 98 99 /// <summary> 100 /// 面试题5 101 /// </summary> 102 public static void TestStringLength() 103 { 104 string strTmp = "abcdefg某某某"; 105 Console.WriteLine($"{strTmp}的二进制长度和字符串长度各是多少?"); 106 Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); 107 //int i = System.Text.Encoding.GetEncoding("gb2312").GetBytes(strTmp).Length; 108 int i = System.Text.Encoding.Default.GetBytes(strTmp).Length;//卧槽,这里有坑,默认字符集utf8的话汉字是2-4个字节,这里是16个,gb2312的话,应该是13个吧 109 int j = strTmp.Length; 110 Console.WriteLine($"二进制长度{i},字符串长度{j}"); 111 } 112 113 /// <summary> 114 /// 面试题6 115 /// </summary> 116 public static void TestStruct() 117 { 118 var obj=new MyStruct(100,100); 119 var obj2 = obj; 120 obj2.X = 200; 121 Console.WriteLine($"obj.X->{obj.X},obj.Y->{obj.Y};obj2.X->{obj2.X},obj2.Y->{obj2.Y}"); 122 } 123 } 124 125 /// <summary> 126 /// 面试题4、请写出C#中的单例模式 127 /// </summary> 128 public class Single 129 { 130 private static Single instance; 131 private Single() { } 132 public static Single GetInstance() 133 { 134 if (instance == null) 135 { 136 instance = new Single(); 137 } 138 return instance; 139 } 140 } 141 142 143 public struct MyStruct 144 { 145 public MyStruct(int x, int y) 146 { 147 X = x; 148 Y = y; 149 } 150 151 public int X { get; set; } 152 public int Y { get; set; } 153 154 155 }