笔试题

#include<iostream>
using namespace std;
/*int main()
{
	signed char a= 0xee;
	unsigned int b =a;
	unsigned char c = a;
	printf("%x",b);
}*/
class Base1{
public:
	Base1()
	{}
	virtual ~Base1(){}
	virtual void print(){}
	virtual Base1 *clone()const
	{
		return NULL;
	}
protected:
	int data_Base1;   //8
};
class Base2
{
public:
	Base2(){}
	virtual ~Base2(){}
	virtual Base2* clone()const
	{
		return NULL;
	}
	virtual void print(){}
protected:
	short data_Base2;//8,为啥他不是4+2=6;因为他需要字节对其;
};
class Derived:public Base1,public Base2
{
public:
	Derived()
	{}
	virtual ~Derived()
	{}
	virtual Derived* clone()const
	{
		return NULL;

	}
protected:
	static double data_Derived;//静态成员不占据空间;

};
int main()
{
	Base1 b;
	int m = sizeof(b);
	cout<<"base1="<<m<<endl;
	Base2 b2;
    m = sizeof(b2);
	cout<<"base1="<<m<<endl;
	Derived d;
	int n = sizeof(d);
	cout<<"derived="<<n<<endl;
	//printf("%d",sizeof(double));
//	printf("%d",sizeof(short));//2
	//printf("%d",sizeof(short int));//2
	//printf("%d",sizeof(unsigned short));//2
	//printf("%d",sizeof(unsigned int));//4
	//printf("%d",sizeof(long));//4
	//printf("%d",sizeof(unsigned long));//4
	//printf("%d",sizeof(long long));//8
	//printf("%d",sizeof(unsigned long long));//8


}

unsigned 不影响数据类型性的该有大小

阿里校招笔试----------------给定一个字符串和有效的字典D,请确定可以插入到S的最小空格数,使得最终的字符串完全由D中的有效单词组成,并输出其解;

给定一个字符串S和有效单词的字典D,请确定可以插入到S中的最小空格数,使得最终的字符串完全由D中的有效单词组成,并输出解。
如果没有解则应该输出n/a
例如
输入
S = "ilikealibaba"
D = ["i", "like", "ali", "liba", "baba", "alibaba"]
Example Output:
输出
"i like alibaba"
解释:
字符串S可能被字典D这样拆分
"i like ali baba"
"i like alibaba"
很显然,第二个查分结果是空格数最少的解。

代码如下所示:

//W = 5 - C + y + [y/4] + [13 * (M+1) / 5] + d - 1
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
#include <math.h>
#include <string.h>
#define MAX_DICT_LEN 255
#define MAX_STR 255

typedef struct simple_str {
    const char * str;
    int len;
} simple_str_t;


void mincut(simple_str_t* str, simple_str_t* dict, int dict_len) 
{
 const char * p = str->str ;
 string s;
 int *arry =(int *)malloc(sizeof(int)*dict_len);
 //memset(arry,0,dict_len);
 for(int i =0;i<dict_len;++i)
	 arry[i]=0;
 int i ;
 while( *p != '\0')
 {
	 for(i=0;i<dict_len;++i)
	 {
		 if(strcmp(p,dict[i].str) == 0)
		 {
			  for(int j =0;j<dict[i].len;++j)
			 {
				 s.push_back(dict[i].str[j]);
			 }
			  s.push_back(' ');
			 p += dict[i].len;
			 arry[i]=1;
			 break;
		 }
	 }
	 if(i = dict_len)
	 {
        for(i=0;i<dict_len;++i)
	   {
		 if(arry[i] == 0 && strncmp(dict[i].str,p,dict[i].len) == 0)
		 {
			 for(int j =0;j<dict[i].len;++j)
			 {
				 s.push_back(dict[i].str[j]);
			 }
			 s.push_back(' ');
			 p += dict[i].len;
			 arry[i] =1;
			 break;
		 }
	   }
	 }
 }
 for(int i=0;i<s.size();++i)
	 printf("%c",s[i]);
 printf("\n");
 delete []arry;
}

int main(int argc, const char * argv[]) {
    char strS[MAX_STR];
    char dictStr[MAX_STR];
    int nDict;
    simple_str_t dict[MAX_DICT_LEN];
    simple_str_t srcStr;

    scanf("%s", strS);
    scanf("%d", &nDict);

    srcStr.str = strdup(strS);
    srcStr.len = strlen(strS);

    for (int i = 0; i < nDict; i++)
    {
        scanf("%s", strS);
        dict[i].str = strdup(strS);
        dict[i].len = strlen(strS);
    }

    mincut(&srcStr, dict, nDict);

    return 0;
}
#endif

//链家笔试
//题目描述,输入任意组对数字,这组数字能够分割成多少组,
//每组内的数字大小是都是有序的排序的
//比如:若果1 2:2 3;3 5;4 2;那么就能把这组数字分成俩种第一种是
// 第一组:1 2; 2 3; 3 5;
//第二组 4 2;
#include<stdio.h>
#include<iostream>
using namespace std;
typedef struct figure
{
  int x;
  int y;
}pa;
int main()
{
  int n ;
  scanf("%d",&n);
  pa * a = (pa*)malloc(sizeof(pa)*n); 
    int temp1;
    int temp2;
  for(int i = 0;i<n;++i)
  {
    scanf("%d",&temp1);
a[i].x = temp1;
    scanf("%d",&temp2);
    a[i].y = temp2;
  }
  for(int i =0;i<n;++i)
  {
    for(int j =0;j+i<n-1;++j)
    {
      if(a[j].x > a[j+1].x )
      {
        pa temp=a[j];
        a[j] = a[j+1];
        a[j+1] = temp;
      }
    }
  }
  for(int i =0;i<n;++i)
  {
 if(a[i].x == a[i+1].x && a[i].y >a[i+1].y)
 {
 pa temp =a[i];
 a[i] = a[i+1];
 a[i+1] = temp;
 }
  }
  for(int i =0;i<n;++i)
  {
 cout<<a[i].x<<" "<<a[i].y<<endl;
  }
  int count =0;
  for(int i =0;i<n;++i)
  {
    if(a[i].x < a[i+1].x && a[i].y > a[i+1].y)
      count++;
  }
 count++;
  printf("%d",count);
  delete []a;
  return 0;
}
#endif
#if 0
//输入两个整数序列,
//第一个序列表示栈的压入顺序,
//请判断第二个序列是否为该栈的弹出顺序。
//假设压入栈的所有数字均不相等。
//例如序列1,2,3,4,5是某栈的压入顺序,
//序列4,5,3,2,1是该压栈序列对应的一个弹出序列,
//但4,3,5,1,2就不可能是该压栈序列的弹出序列。
//(注意:这两个序列的长度是相等的)
//该题的思路是借助一个辅助栈,依据出栈的顺序把序列进栈,然后根据出栈的序列出栈
bool IsPopOrder(vector<int> pushV,vector<int> popV)
{
        if(pushV.size() < 1 || popV.size() <1 )
            return false;
        stack<int> st;
        vector<int>::iterator it1;
        it1 = pushV.begin();
        vector<int>::iterator it2;
        it2 = popV.begin();
        while(it2 != popV.end())
            {
            while(st.empty() || st.top() != *it2)
                {
                if(it1 == pushV.end())
                   return false;
                st.push(*it1);
                it1++;
            }
            st.pop();
            ++it2;
        }
        if(st.empty() && it2 == popV.end())
           return true;
 }
void main()
{
vector<int> vc1;
vector<int> vc2;
vc1.push_back(1);
vc1.push_back(2);
vc1.push_back(3);
vc1.push_back(4);
vc1.push_back(5);
vc2.push_back(4);
vc2.push_back(3);
vc2.push_back(5);
vc2.push_back(1);
vc2.push_back(2);
    bool value =IsPopOrder(vc1,vc2);
    if(value)
{
cout<<"true"<<endl;
}
else
cout<<"false"<<endl;


}

三七互娱笔试题:考的很基础,感觉自己又与机会插肩而过,心里有些难受

(1)编写string类的构造函数,拷贝构造函数,析构函数,赋值构造函数;

string(const char* str = NULL)
{
  if(str == NULL)
  {
    m_data = new char[1];
    *m_data = '\0';
   }
 else
   {
      int length = strlen(str);
      m_data = new char[length+1];
      strcpy(m_data,str);
    }
}

string(const string &other)
{
  int length = strlen(other.m_data);
  m_data = new char[length+1];
  strcpy(m_data,other.m_data);
}
string& operator=(const string& other)
{
   if(this != &other)
  { 
        string temp(other);
        char * str = temp.m_data;
        temp.m_data = m_data;
        m_data = str;
        
   }
   return *this;
}

~string()
{
  delete []m_data;
}

(四)京东笔试题(神奇树:224可以分为俩组,第一组是[2,2] [4]第一组的数字之和等于第二组的数字之和)

//思路只有他的和是偶数的时候才可能是神器数,其次如果是偶数了,在把它从小到大排序,用和的一半去减没一个数字,如果减到差等于了0,那就是神奇树,如果减到差是小于0的,就不是神奇树;

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool qiujie(vector<int>& v, int s, int half)
{
    if (half == 0) return true;
    if (half < 0) return false;
    for (int i = s; i < v.size(); ++i) 
    {
        if (half - v[i] < 0) return false;
        if (qiujie(v, i + 1, half - v[i])) return true;
    }
    return false;
}
bool countNum(long int n) 
{
    vector<int> v;
    int sum = 0;
    int half;
    while (n) 
    {
        sum += n % 10;
        v.push_back(n % 10);
        n /= 10;
    }
    if (sum % 2 || v.size() < 2) return false;
    half = sum / 2;
    sort(v.begin(), v.end());
    for (int i = 0; i < v.size(); ++i) 
    {
        if (qiujie(v, i+1 , half - v[i])) return true;
    }
    return false;

}

int main()
{
    long int l, r;
    while (cin >> l >> r) 
    {
        int count = 0;
        for (long int i = l; i <= r; ++i)
        {
            if (countNum(i)) ++count;
        }
        cout << count << endl;
    }


    return 0;
}

//构造函数与析构函数 的调用

#include<iostream>
#include<stdio.h>
using namespace std;
int x=1;
class A
{
public:
	A(){x=3;}
	virtual ~A(){x=5;}
	virtual void func1(){x=7;}
	void func2(){x=9;}
};
class B:public A
{
public:
	B(){x=10;}
	virtual ~B(){x=12;}
	virtual void func1(){x=12;}
	void func2(){x=13;}
};
B testb;
B* testc;
int main()
{
	A *testa;
	cout<<x<<endl;//10//这里也做错了,因为B testb就会调用构造函数。所以调用完B的构造函数之后应该
	                  //是10;
	testa = (A*)&testb;
	cout<<x<<endl;//10
	testa->func1();
	cout<<x<<endl;//12
	testc = new B;
	cout<<x<<endl;//10
	delete testc;
	cout<<x<<endl;//5这里做错了,因为我没有考虑继承这层关系,析构顺序与构造顺序相反,所以最
	              //后调用的是~A(),x= 5;
	testa->func2();
    cout<<x<<endl;//9
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值