c++面试题

也不知道为什么,现在的面试题越来越离谱了,题目也是越来越奇葩,出这样的题目有什么意思呢???

题一:

#include <iostream>
using namespace std;

int m_1 = 1;
int func(int m_4)
{
	static int m_2 = 1;
	int m_3 = 1;
	m_1++;
	m_2++;
	m_3++;
	if(m_4 ==0)
	{
		cout<<"m_1=="<<m_1<<" "<<"m_2=="<<m_2<<" "<<"m_3=="<<m_3<<" "<<"m_4=="<<m_4<<endl;
	}
	else
	{
		return m_4 + func(m_4-1);
	}
}

int main()
{
	int m_4 = 5,m_5=0;
	m_5 = func(m_4);
	cout<<"m_5=="<<m_5<<endl;
	return 0;
}


输出打印结果:

m_1==7 m_2==7 m_3==2 m_4==0
m_5==134520911


题二:

#include <iostream>
using namespace std;

class A
{
private:
	int a1;
protect:
	int a2;

public:
	void func(){};
};

class B : protect A
{
public:
	int b1;
protect:
	int b2;
public:
	void func(){};
};

class C : private B
{
private:
	int c1;
public:
	int c2;
	void func(){};
};

class D : public C
{
protect:
	int d1;
public:
	int d2;
	void func(){};
};

D类func函数都能访问哪些变量???


题三:

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
	char str[] = "abcdefghijklmn";
	char *pstr = str+3;
	int *pint = (int*)(str+5);
	short int *pshort = (short int*)(str+2);

	printf("%s\n%s\n%s\n", (char*)(pstr++), (char*)(++pint), (char*)(pshort++));
	return 0;
}
打印结果:

defghijklmn
jklmn
cdefghijklmn


题四:

#include <iostream>
#include<cstdio>
using namespace std;

int main()
{
	int a = 0x1A2B3C4D;
	printf("%x\n",(a+2));
	return 0;
}

打印结果:

1a2b3c4f


题五:

ip地址转成long数字

long数字转成ip地址

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;

//java实现
//ip地址转成long型数字
// long ip2long(String ip)
// {
//     String[] items = ip.split("\\.");
// 	return Long.valueof(items[0])<<24
// 		| Long.valueof(items[1])<<16
// 		| Long.valueof(items[2])<<8
// 		| Long.valueof(items[3]);
// }
// //long型数字转成ip地址
// String long2ip(unsigned long n)
// {
// 	StringBuilder sb = new StringBuilder();  
// 	sb.append(ipInt & 0xFF).append(".");  
// 	sb.append((ipInt >> 8) & 0xFF).append(".");  
// 	sb.append((ipInt >> 16) & 0xFF).append(".");  
// 	sb.append((ipInt >> 24) & 0xFF);  
// 	return sb.toString();  
// }

char* long2ip(long in, char *out)
{
	unsigned char*p;
	p = (unsigned char*)(&in);
        snprintf_s(out,16,16, "%d.%d.%d.%d", p[0],p[1],p[2],p[3]);
	return out;
}
long ip2long(char* ip)
{
	unsigned addr[16];
	sscanf(ip,"%d.%d.%d.%d", addr, addr+1, addr+2, addr+3);
	long *ul = (long*)(&addr);
	return *ul;
}
int main()
{
	char ip = "127.0.0.1";
	long l = ip2long(ip);
	printf("%ld\n", l);
}

附加:

struct ipaddr
{
    unsigned char ip1;
    unsinged char ip2;
    unsigned char ip3;
    unsigned char ip4;
}IPADDR;

int uint_to_ip(struct ipaddr *ipa,unsigned int ip)
{
    ipa->ip1 = 0;
    ipa->ip2 = 0;
    ipa->ip3 = 0;
    ipa->ip4 = 0;
    ip->ip1 = (ip>>0)&0xFF;
    ip->ip2 = (ip>>8)&0xFF;
    ip->ip3 = (ip>>16)&0xFF;
    ip->ip4 = (ip>>24)&0xFF;
}

然后写个printf("%d.%d.%d.%d/n",ipa->ip1,ip->ip2,ip->ip3,ip->ip4);就OK了..


题六:

#include <iostream>
using namespace std;

class Haha
{
public:
	int i;
    Haha():i(0){};
	void print(){cout <<"Haha class:"<< i << endl;}
	~Haha(){};
};

class Hehe
{
public:
	int j;
    Hehe():j(1){};
	void print(){cout << "Hehe class:"<<j<<endl;}
	~Hehe(){};
};
int main(int argc, char *argv[])
{
    Haha haha;
    Hehe *hehe;
    hehe =(Hehe*)&haha;
    hehe->print();  //声明谁调用谁
    Hehe hh;
    hehe = &hh;
    hehe->print();
    return 0;
}


打印结果:

Hehe class:0
Hehe class:1


题七:判断链表是否为环

struct node { char val; node* next;}
   bool check(const node* head) {} //return false : 无环;true: 有环一种O(n)的办法就是(搞两个指针,一个每次递增一步,一个每次递增两步,如果有环的话两者必然重合,反之亦然):
bool check(const node* head)
{
    if(head==NULL)  returnfalse;
    node *low=head, *fast=head->next;
    while(fast!=NULL && fast->next!=NULL)
    {
        low=low->next;
        fast=fast->next->next;
        if(low==fast) returntrue;
    }
    returnfalse;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值