553的一些程序输出题

前言

害,还不出结果,真是急人。把历年的东大真题中的程序输出题和改错题做个总结,顺带回顾复习。
如果你也是在准备东大553复试并且觉得对你用,记得给我点个赞。
纯手打...

数组

题目

#include<iostream>
using namespace std;

void func(int a[]) {
    a[0] = a[-1] + a[1];            //*a = *(a - 1) + *(a + 1); 使用这句输出一样
}

int main() {
	int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	for (int i = 1; i < 9; i++)
		func(&a[i]);            //1
	for (int i = 0; i < 10; i++)
		cout << a[i] << " " << endl;
	return 0;
}

输出

1
4
8
13
19
26
34
43
53
10

考点

  1. func的实参依次传入的是每个数组元素的地址,如果直接使用a就一直是数组首元素的地址了
  2. func函数内其实就是指针类型的运算,a[-1]等同于*(a - 1),一般用在数组中,即需要连续的物理存储空间,表示上一个存储的元素

数组

题目

#include<iostream>
using namespace std;

int main() {
	int a[3][3] = { 0, 1, 2, 0, 1, 2, 0, 1, 2 };        //2
	int b[3] = { 0 };                                   //1
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			b[i] += a[i][a[j][i]];
	for (int i = 0; i < 3; i++)
		cout << b[i] << " " << endl;
}

输出

0
3
6

考点

  1. 数组的初始化
  2. 多维数组初始化时按照行优先

输出流

题目

#include<iostream>
using namespace std;

int func(int a, int b) {
	int c = 0;
	static int d = 3;        //1
	if (a > b)
		c = 1;
	if (a < b)
		c = -1;
	return ++d + c;
}

int main() {
	int x = 2, y = 3;
	for (int i = 1; i <= 2; i++) {
		cout << x << ", " << y << ", " << func(x++, y) << endl;             //先执行了func,再执行的输出,所以输出时x已经自增了
		x = y + 1;
	}
	return 0;
}

输出

3, 3, 3
5, 3, 6

考点

非常非常非常有意思的一道题,值得探究探究,和考究细心

  1. 静态变量d在编译时已经分配了内存,所以不会随着跳出函数而消失,其值也是
  2. 对于输出流而言,是否是先执行完其中所有的表达式以及函数再进行输出,或者是按照一定的结合性比如从右向左进行?深度探究!
  3. 毫无疑问对于后置自增符而言func传进去的实参是未自增前的

深度探究

int x = 3;
cout << x << ", " << x++ <<endl;
4, 3
int x = 3;
cout << x++ << ", " << x << endl;
3, 4
int x = 3;
cout << x++ << ", " << x++ << endl;
4, 3

讨论:
第一种设想:从右到左边执行边输出,基于这种设想,实验一成立,实验二不成立,实验三成立;
第二种设想:从左到右边执行边输出,基于这种设想,实验一不成立,实验二成立,实验三不成立;
第三种设想:先全部执行完,再逐个输出,这种设想显然错误,因为执行完之后x的值应该全部相同。
陷入困惑

网上查阅资料看到了

对于没有指定执行顺序的运算符来说,如果表达式指向并修改了同一个对象,将会引发错误并产生未定义的行为,比如<<运算符没有明确规定何时以及如何对运算对象求值。因此上面的输出表达式是未定义,无法推断它的行为,结果可能是210,也可能是012,甚至编译器还可能做完全不同的操作。测试会输出210只是因为使用的编译器是使用网上所说的执行顺序。

那这题不是有歧义么?

字符

题目

#include<iostream>
using namespace std;

void func(char str[]) {
	int count = 0;
	while (*str != 0) {            //3
		if (*str >= '0' && *str <= '9')        //2
			count++;
		str++;            //4
	}
	cout << count << endl;
}

int main() {
	char str[] = "abc8d0e32fg\0hi1k3";        //1
	func(str);
	return 0;
}

输出

4

考点

  1. 字符数组的初始化
  2. 字符通过ASCII码比较大小
  3. ASCII码中的0就是终止符'\0'
  4. 指针类型的运算

虚函数

题目

#include<iostream>
using namespace std;

class A {
public:
	virtual void print() = 0;     //1
};

class B :public A {
public:
	void print() {
		cout << "B::print" << endl;
	}
};

class C :public B {
public:
	void print() {
		cout << "C::print" << endl;
	}
};

int main() {
	A *p;
	B b;
	C c;

	p = &b;            //2
	p->print();        //3

	p = &c;
	p->print();
	return 0;
}

输出

B::print
C::print

考点

  1. 基类可以声明纯虚函数,纯虚函数不能给出具体定义,有纯虚函数的对象被称之为抽象类,抽象类不能实例化
  2. 可以用派生的对象给基类的指针赋值,这叫做类型兼容
  3. 通过上述指针调用虚函数时调用的是基类的,这被称之为包含多态

内嵌对象

题目

#include<iostream>
using namespace std;

class Student {
public:
	Student() {
		cout << "Student+" << endl;
	}
	~Student() {
		cout << "-Student" << endl;
	}
};

class Teacher {
public:
	Teacher() {
		cout << "Teacher+" << endl;
	}
	~Teacher() {
		cout << "-Teacher" << endl;
	}
private:
	Student stu;
};

int main() {
	Teacher t;
	return 0;
}

输出

Student+
Teacher+
-Teacher
-Student

考点

  1. 基类和内嵌对象的构造函数总是先被调用,且是按照定义的顺序
  2. 析构函数的调用顺序和构造函数相反

常识

题目

#include<iostream>
using namespace std;
int main(){
	for (int i = 0; i < 8; i++){
		if (i % 2 == 0)
			cout << i + 1 << endl;
		if (i % 3 == 0)
			continue;
		if (i % 5 == 0)
			cout << "Ending of programming\n";
	}
	cout << "Ending of programming\n";
	return 0;
}

输出

1			//0对任何数取模结果都是0
3
5
Ending of programming
7
Ending of programming

考点

  1. 0的取模和被除

输入流

题目

#include<iostream>
using namespace std;
int main(){
	int c;				//1
	if ((c = cin.get()) != EOF){
		main();
		cout << c;
	}
	return 0;
}

输出

考点

  1. 选择使用int接受是因为int才能接受EOF

递归

题目

#include<iostream>
using namespace std;
void fun(int a[], int cur, int s){			//1
	if (cur < s){
		fun(a, cur + 1, s);
		cout << a[cur] << ", ";
	}
}
int main(){
	int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	fun(a, 0, 10);
	return 0;
}

输出

10,9,8,7,6,5,4,3,2,1

考点

  1. 递归实现倒序输出,和二叉树的遍历有异曲同工之妙

字符

题目

#include<iostream>
using namespace std;
void main(){
	char* str="THIS IS PROGRAMMING.";
	int length;						 //字符串的长度
	int j = 0;
	for(int k = 0; k < length; k++){	                 //找到字符串中最大的那个字符位置
		if(str[j] < str[k])
			j = k;
	}
	int temp = j;
	str[j] = str[7];					 //第一个S被换成第七个字符
	str[7] = str[13];
	str[13] = str[length];
	cout<<str<<endl;
}

输出

THI  ISAPROGR

考点

  1. 没啥好说的

常识

题目

Void fun(){
	cout<<sizeof(bool)<<", "<<sizeof(char)<<", "<<sizeof(short int)<<", "<<sizeof(int) <<", "
	<<sizeof(int)<<", "<<sizeof(long int)<<", "<<sizeof(float)<<", "<<sizeof(double)<<", "
	<<sizeof(long double);
//输出各类型的所占字节数
}

输出

1, 1, 2, 4, 4, 4, 4, 8, 8

考点

  1. 常识啊!真的是常识,要记的那种

猜你喜欢

转载自www.cnblogs.com/Za-Ya-Hoo/p/12684052.html