1.C#和C++除了语法上的差别以外,有什么不同的地方?
答:(1)c#有垃圾自动回收机制,程序员不用担心对象的回收。
(2)c#严禁使用指针,只能处理对象。如果希望使用指针,则仅可在unsafe 程序块中能使用指针。
(3)c#只能单继承。
(4)必须通过类名访问静态成员。不能像C++中那样,通过对象访
问静态成员。
(5)在子类中覆盖父类的虚函数时必须用关键字override,覆盖父类
的方法要用关键字new
2.C++是不是类型安全的? (Autodesk)
答案:不是。两个不同类型的指针之间可以强制转换。C#是类型安全的。
3.有四个同样的容器,里面装满了粒数相同的药丸,正常药丸的质量为m,变质药丸的质量为m+1,现在已知这四个容器中,有一个装的全是变质药丸,用电子秤只称一次,找出哪个容器装的是变质药丸(Microsoft)
答:把四个容器依次编号为1、2、3、4,然后从中分别取出1、2、3、4 粒药丸,称这10 粒药丸的质量,如果质量为10m+1,则说明第一个容器装的是变质药丸,如果为10m+2 则说明第二个装的变质药丸,依次类推。
4.C++中:
sizeof(a)是这个数组的字节
sizeof(*a)是第一个数,int型多少字节
5.There are two int variables: a and b, don’t use “if”, “? :”, “switch”or other judgement statements, find out the biggest one of the twonumbers.
答:( ( a + b ) + abs( a – b ) ) / 2
6.一个链表的结点结构
1 struct Node 2 { 3 int data ; 4 Node *next ; 5 }; 6 typedef struct Node Node ;
(1)已知链表的头结点head,写一个函数把这个链表逆序 ( Intel)
1 Node * ReverseList(Node *head) //链表逆序 2 { 3 if ( head == NULL || head->next == NULL ) 4 return head; 5 Node *p1 = head ; 6 Node *p2 = p1->next ; 7 Node *p3 = p2->next ; 8 p1->next = NULL ; 9 while ( p3 != NULL ) 10 { 11 p2->next = p1 ; 12 p1 = p2 ; 13 p2 = p3 ; 14 p3 = p3->next ; 15 } 16 p2->next = p1 ; 17 head = p2 ; 18 return head ; 19 }
(2)已知两个链表head1 和head2 各自有序,请把它们合并成一个链表
依然有序。
Node * Merge(Node *head1 , Node *head2) { if ( head1 == NULL) return head2 ; if ( head2 == NULL) return head1 ; Node *head = NULL ; Node *p1 = NULL; Node *p2 = NULL; if ( head1->data < head2->data ) { head = head1 ; p1 = head1->next; p2 = head2 ; } else { head = head2 ; p2 = head2->next ; p1 = head1 ; } Node *pcurrent = head ; while ( p1 != NULL && p2 != NULL) { if ( p1->data <= p2->data ) { pcurrent->next = p1 ; pcurrent = p1 ; p1 = p1->next ; } else { pcurrent->next = p2 ; pcurrent = p2 ; p2 = p2->next ; } } if ( p1 != NULL ) pcurrent->next = p1 ; if ( p2 != NULL ) pcurrent->next = p2 ; return head ; }
6..写一个函数找出一个整数数组中,第二大的数.
1 final int MIN = -32767; 2 3 public int findSecMax(int[] data,int count) 4 { 5 int maxNumber = data[0]; 6 int secMax = MIN; 7 8 for(int i=1;i<count;i++) 9 { 10 if(data[i]>maxNumber) 11 { 12 secMax = maxNumber; 13 maxNumber = data[i]; 14 }else 15 { 16 if(data[i]>secMax) 17 { 18 secMax = data[i]; 19 } 20 } 21 } 22 return secMax; 23 }