- struct linka {
- int data;
- linka* next;
- };
- void reverse(linka*& head) {
- if(head ==NULL)
- return;
- linka *pre, *cur, *ne;
- pre=head;
- cur=head->next;
- while(cur)
- {
- ne = cur->next;
- cur->next = pre;
- pre = cur;
- cur = ne;
- }
- head->next = NULL;
- head = pre;
- }
数组a[N],存放了1至N-1个数,其中某个数重复一次。写一个函数,找出被重复的数字.时间复杂度必须为o(N)函数原型:int do_dup(int a[],int N)
- #include "stdio.h"
- int main(void)
- {
- int b=16;
- printf("%s\n",(b&(b-1)?"false":"ture") );
- return 0;
- }
网易C++笔试题3. 一个类有基类、内部有一个其他类的成员对象,构造函数的执行顺序是怎样的?
答:先执行基类的(如果基类当中有虚基类,要先执行虚基类的,其他基类则按照声明派生类时的顺序依次执行),再执行成员对象的,最后执行自己的。
网易C++笔试题6.请问下面程序有什么错误?
int a[60][250][1000],i,j,k;
for(k=0;k<=1000;k++)
for(j=0;j<250;j++)
for(i=0;i<60;i++)
a[i][j][k]=0;
答案:把循环语句内外换一下
二维数组在c++中存储,一般是按行存储的,就是将一行当作一维数组进行存储。 例如:a[2][2]这个二维数组,其在内存中存储顺序为: a[0][0] a[0][1] //先存储第一行 a[1][0] a[1][1] //再存储第二行。 假设是32位系统,整型占4个字节,则其在内存的形式为: a[0][0] a[0][1] a[1][0] a[1][1]
网易C++笔试题9.已知strcpy 函数的原型是:
char *strcpy(char *strDest, const char *strSrc);
其中strDest 是目的字符串,strSrc 是源字符串。不调用C++/C 的字符串库函数,请编写函数 strcpy
答案:
char *strcpy(char *strDest, const char *strSrc)
{
if ( strDest == NULL || strSrc == NULL)
return NULL ;
if ( strDest == strSrc)
return strDest ;
char *tempptr = strDest ;
while( (*strDest++ = *strSrc++) != ‘’)
;
return tempptr ;
}
网易C++笔试题10.写一个函数找出一个整数数组中,第二大的数
答案:
const int MINNUMBER = -32767 ;
int find_sec_max( int data[] , int count) //类似于1 4 4 4这样的序列将认为1是第二大数
{
int maxnumber = data[0] ;
int sec_max = MINNUMBER ;
for ( int i = 1 ; i < count ; i++)
{
if ( data[i] > maxnumber )
{
sec_max = maxnumber ;
maxnumber = data[i] ;
}
else
{
if ( data[i] > sec_max )
sec_max = data[i] ;
}
}
return sec_max ;
}
1、socket(family,type,protocol)
1). 创建套接字:sock_create()
2). 为套接字绑定文件句柄:sock_map_fd()
2、bind (sockfd, sockaddr, addrlen)
3、listen(sockfd, backlog)
4、connect(sockfd, sockaddr, addrlen)
5、accept(sockfd, sockaddr, addrlen)
在网络编程章节的数据接收过程中,我们主要介绍过read()、recv()、recvfrom()还有一个recvmsg()没介绍到,今天我们就来看一下这几个API函数到底有什么差别
__sock_recvmsg()。
前面关于发送数据包时我们介绍过的API有write()、send()、sendto()还有一个sendmsg()没介绍到。
①链表反转
单向链表的反转是一个经常被问到的一个面试题,也是一个非常基础的问题。比如一个链表是这样的: 1->2->3->4->5 通过反转后成为5->4->3->2->1。
最容易想到的方法遍历一遍链表,利用一个辅助指针,存储遍历过程中当前指针指向的下一个元素,然后将当前节点元素的指针反转后,利用已经存储的指针往后面继续遍历。源代码如下:
②已知String类定义如下:
class String
{
public:
String(const char *str = NULL); // 通用构造函数
String(const String &another); // 拷贝构造函数
~ String(); // 析构函数
String & operater =(const String &rhs); // 赋值函数
private:
char *m_data; // 用于保存字符串
};
尝试写出类的成员函数实现。
答案:
String::String(const char *str)
{
if ( str == NULL ) //strlen在参数为NULL时会抛异常才会有这步判断
{
m_data = new char[1] ;
m_data[0] = '/0' ;
}
else
{
m_data = new char[strlen(str) + 1];
strcpy(m_data,str);
}
}
String::String(const String &another)
{
m_data = new char[strlen(another.m_data) + 1];
strcpy(m_data,other.m_data);
}
String& String::operator =(const String &rhs)
{
if ( this == &rhs)
return *this ;
delete []m_data; //删除原来的数据,新开一块内存
m_data = new char[strlen(rhs.m_data) + 1];
strcpy(m_data,rhs.m_data);
return *this ;
}
String::~String()
{
delete []m_data ;
}
printf("%s","abc""def");
结果是abcdef。
怎么个原理?
你的另外一个帖子 都有人和你说了的
这其实就是连接起来的一个字符串
1
2
3
4
5
6
7
8
9
10
11
12
|
char
*p =
"hello"
\
"world"
;
//或者
char
*p =
"hello"
"world"
;
printf
(p);
//或者
char
*p =
"hello"
"world"
;
printf
(p);
printf
(p);
|
求Fibonacci数列
{
int result[2] = {0, 1};
if(n < 2)
return result[n];
return Fibonacci_Solution1(n - 1) + Fibonacci_Solution1(n - 2);
}
更简单的办法是从下往上计算,首先根据f(0)和f(1)算出f(2),在根据f(1)和f(2)算出f(3)……依此类推就可以算出第n项了。很容易理解,这种思路的时间复杂度是O(n)。
long long Fibonacci_Solution2(unsigned n)
{
int result[2] = {0, 1};
if(n < 2)
return result[n];
long long fibNMinusOne = 1;
long long fibNMinusTwo = 0;
long long fibN = 0;
for(unsigned int i = 2; i <= n; ++ i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;
}
二叉树层序遍历
#include <deque>
#include <iostream>
using namespace std;
struct BTreeNode // a node in the binary tree
{
int m_nValue; // value of node
BTreeNode *m_pLeft; // left child of node
BTreeNode *m_pRight; // right child of node
};
///
// Print a binary tree from top level to bottom level
// Input: pTreeRoot - the root of binary tree
///
void PrintFromTopToBottom(BTreeNode *pTreeRoot)
{
if(!pTreeRoot)
return;
// get a empty queue
deque<BTreeNode *> dequeTreeNode;
// insert the root at the tail of queue
dequeTreeNode.push_back(pTreeRoot);
while(dequeTreeNode.size())
{
// get a node from the head of queue
BTreeNode *pNode = dequeTreeNode.front();
dequeTreeNode.pop_front();
// print the node
cout << pNode->m_nValue << ' ';
// print its left child sub-tree if it has
if(pNode->m_pLeft)
dequeTreeNode.push_back(pNode->m_pLeft);
// print its right child sub-tree if it has
if(pNode->m_pRight)
dequeTreeNode.push_back(pNode->m_pRight);
}
}