【练习记录】2022年春夏笔试-C++游戏实习

选择+编程

还没有分析,只是记录

选择

Q:数组的sizeof值等于?

A:等于数组所占用的内存字节数,如: char a1 [] = "abc"; int a2 ; sizeof (a1); // 结果为4,字符 末尾还存在一个NULL终止符 sizeof (a2); // 结果为3*4=12(依赖于int)

 

Q:进栈顺序为a、b、c、d,则出栈顺序可以为?

A:

a-b-c-d(进a-出a-进b-出b-进c-出c-进d-出d)

b-a-c-d(进a-进b-出b-出a-进c-出c-进d-出d)

原理好像是:

是否为合法出栈序列是非常好判断的,只需要牢记一个规则:将入栈元素按入栈顺序由1到n进行编号,凡是合法的出栈序列,每个数后面的比它小的数,必然是按递减次序排列的。所以4132是非法的,因为4后面的比它小的数字不是按照递减次序排列的。

现在我们需要设计一个函数来检验某个给定的出栈序列是否合法,那么这个规律是否能适用呢?理论上是可行的,然而这个方法无论是从时间复杂度上还是从编码难度上看都没有以下方法好。

这里更好的方法就是对整个出入栈的过程进行模拟。我们先假定给定的出栈序列是合法的且将出栈序列压入到一个队列当中。然后开始模拟入栈操作(即按照从1到n的次序入栈),当某个元素入栈后如果其编号等于队头元素编号则说明此元素应当进行出栈操作同时还要进行出队操作。如果到最后栈为空,则说明该出栈序列是合法的,否则为非法。
(原文链接:https://blog.csdn.net/Zee_Chao/article/details/89971101)

实现代码:来源:https://blog.csdn.net/qq_51687381/article/details/113574195

(还没有懂,为什么要输入栈的规模,以及为什么数字之间要用空格间隔)

#include<iostream>
#include<stack>
using namespace std;
int main() {
	int x;
	while (1) {
		stack<int> st2;
		stack<int> st3;
		cin >> x;
		if (x == 0) {
			break;
		}
		int flag = 1;
		for (int i = 1; i <= x; i++) {
			int y;
			cin >> y;
			if (st2.empty()) {
				st2.push(y);
			}
			else if (st2.top() < y) {
				st2.pop();
				st2.push(y);
			}
			else if (st2.top() > y) {
				if (st3.empty()) {
					st3.push(y);
				}
				else if (st3.top() > y) {
					st3.pop();
					st3.push(y);
				}
				else if (st3.top() < y) {
					flag = 0;
				}
			}

		}
		if (flag) {
			cout << "Yes" << endl;
		}
		else {
			cout << "No" << endl;
		}
	}
}
/*
5
4 2 3 5 1
No
5
1 5 4 3 2
Yes
5
5 4 3 2 1
Yes
5
4 3 5 1 2
No
5
1 2 3 4 5
Yes
5
4 5 3 2 1
Yes
*/

Q:以下哪种数据结构的查找效率好于 O(n) ?

A:

 

jump table: 似乎是O(logn)

 

Q:转换操作符可以有多少个参数?

conversion operator:转换操作符

A:(链接:https://www.zhihu.com/question/377331788/answer/1061697215)

重载 operator() 时,参数就可以有无限多个。因为重载 operator() 的目的就是构造一个仿函数,使类的对象模拟函数的行为。

其他的运算符,能有几个参数需要考虑两个问题。1) 这个运算符是单目运算符还是双目运算符;2) 是实现为类的成员函数的形式,还是非成员函数的形式。

 

举个栗子,如果你需要重载双目+,也就是需要实现 a + b 这种功能,那么继续分两种实现方式讨论:

1) 你是实现为类的成员函数的形式的,那么就需要一个参数 (因为运算符的左操作数是以 this 指针的形式隐式传递给运算符重载函数的)

2) 你是实现为非成员函数的形式的,那么就需要两个参数

另外,到这里,我们还不能忘了,还有一种 +,是单目 + 。就是你能写出 int a; +a; 的那种 + 。

对于这种单目运算符,如果你是实现为成员函数的,就是 0 个参数,如果是非成员函数,则是 1 个。

 

总结:

1) 重载单目运算符时,如果实现为成员函数,则一般需要 0 个参数,如果实现为非成员函数,则一般需要 1 个参数。

2) 重载双目运算符时,如果实现为成员函数,则一般需要 1 个参数,如果实现为非成员函数,则一般需要 2 个参数。

3) 小括号运算符,参数可以有任意多个。

4) 例外情况:后置自增 (减) 运算符,它们虽然是单目运算符。但是因为需要与前置自增 (减) 运算符区别,人为加了个 int 型参数。

 

Q:判断:Linux 进程调度完全公平调度器采用红黑树来管理进程控制块?

A: Completely Fair Scheduler ,完全公平调度器,用于Linux系统中普通进程的调度。 CFS 采用了红黑树算法来管理所有的调度实体 sched_entity ,算法效率为 O (log (n)) 。

 

A:C++中,构造函数不可以是虚函数,而析构函数可以且常常是虚函数。 虚函数的定义:类成员函数前面添加virtual关键字,则函数被称为虚函数。

A:链表是基于数组实现的(错误,正确的似乎是反过来?)

 

其余VS中:

0521ea xuanzeti

what is the output of the following code ?
#if defined _m_x64 || defined _m_arm || defined _m_arm64
	#define _unaligned __unaligned
#else
	#define _unaligned
#endif
extern "c++" {
	template <typename _ctype, size_t_sz>
	char(*__c_helper(_unaligned _ctype(&_ay)[_sz]))[_sz];

#define __crtcof(a) (sizeof(*__c_helper(_a)) + 0)
}
int main(){
	int abcd[10];
	cout << __crtcof(abcd) << endl;
	return 0;
}

2
#include<iostream>
class a {};
class b {
public:
	int x = 0;
};
class c:public a, b {};
struct d: private a, b{};
int main() {
	c c;
	c.x = 3;
	d d;
	d.x = 3;
	std::cout << c.x << d.x;
	严重性	c2247	“b::x”不可访问,因为“c”使用“private”从“b”继承	project1	d : \epan\研二上\leetcode相关\project1\project1\test.cpp	405

}

3
#include<iostream>
#include<string>
using namespace std;
int main() {
	string str("ubuntu");
	cout << str.size();
	cout << str.max_size();
	return 0;
}//62147483647//6&& max size of the complier

4
#include<iostream>
#include<string>
using namespace std;
int main() {
	int i;
	int j = 10;
	i = (j++, j + 100, 999 + j);
	cout << j;//11
	return 0;
}

5
#include<iostream>
#include<string>
using namespace std;
int main() {
	string str("ubuntu");
	cout << str.size();
	cout << str.max_size();
	return 0;
}

6
#include<stdio.h>
using namespace std;
int main() {
	int a[5] = { 1,2,3,4,5 };
	int *ptr = (int*)(&a + 1);
	printf("%d, %d\n", *(a + 1), *(ptr - 1));
	return 0;
}



#include<iostream>
using namespace std;
typedef union {
	char a;
	int b;

}myuniont;
int main() {
	myuniont s1;
	s1.a = 'a';
	s1.b = 98;
	myuniont s2;
	s2 = s1;
	cout << s2.a;
	return 0;
}

  

其余存档:

编程

 

posted @ 2022-05-22 15:41  translucent  阅读(97)  评论(0)    收藏  举报