害,还不出结果,真是急人。把历年的东大真题中的程序输出题和改错题做个总结,顺带回顾复习。
如果你也是在准备东大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
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
#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
非常非常非常有意思的一道题,值得探究探究,和考究细心
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
#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
#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
#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
#include<iostream>
using namespace std;
int main(){
int c; //1
if ((c = cin.get()) != EOF){
main();
cout << c;
}
return 0;
}
无
#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
#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
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
原文:https://www.cnblogs.com/Za-Ya-Hoo/p/12684052.html