一、选择题
1、以下程序的输出结果是()
#include <stdio.h>
int main()
{
char a[10] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 0}, *p;
int i;
i = 8;
p = a + i;
printf("%s\n", p - 3);
return 0;
}
A.6
B.6789
C.'6'
D.789
2、以下程序的输出结果是()
#include <iostream.h>
int main()
{
int x=3,y=3;
switch(x%2)
{
case 1:
switch (y)
{
case 0:
cout<<"first";
case 1:
cout<<"second";
break;
default:
cout<<"hello";
}
case 2:
cout<<"third";
}
return 0;
}
A.second third
B.hello
C.first second
D.hellothird
3、以下能对二维数组a进行正确初始化的语句是()
A.int ta[2][]={{0,1,2},{3,4,5}};
B.int ta[][3]={{0,1,2},{3,4,5}};
C.int ta[2][4]={{0,1,2},{3,4},{5}};
D.int ta[][3]={{0,,2},{},{3,4,5}};
4、能把函数处理结果的二个数据返回给主调函数,在下面的方法中不正确的是()
A.return这二个数
B.形参用数组
C.形参用二个指针
D.用二个全局变量
5、int *p[4] 与选择项中的() 等价
A.int p[4]
B.int *p
C.int *(p[4])
D.int (*p)[4]
6、设变量已正确定义,以下不能统计出一行中输入字符个数(不包含回车符)的程序段是
A.n=0;while(ch=getchar()!='\n')n++;
B.n=0;while(getchar()!='\n')n++;
C.for(n=0;getchar()!='\n';n++);
D.n=0;for(ch=getchar();ch!='\n';n++);
7、以下代码执行之后ptr指向的内容是?
char *ptr;
char myString[] = "abcdefg";
ptr = myString;
ptr += 5;
A.Compiler error
B.fg
C.efg
D.defg
8、下面3段程序代码的效果一样吗()
int b;
(1)const int *a = &b;
(2)int const *a = &b;
(3)int *const a = &b;
A.(2)=(3)
B.(1)=(3)
C.(1)=(2)
D.都不一样
9、32位系统中,定义**a[3][4],则变量占用内存空间为()。
A.4
B.48
C.192
D.12
10、假设在一个 32 位 little endian 的机器上运行下面的程序,结果是多少?
#include <stdio.h>
int main()
{
long long a = 1, b = 2, c = 3;
printf("%d %d %d\n", a, b, c);
return 0;
}
A.1,2,3
B.1,0,2
C.1,3,2
D.3,2,1
二、编程题
1、字符串中找出连续最长的数字串 题目链接
2、数组中出现次数超过一半的数字 题目链接
三、选择题题解
1、以下程序的输出结果是()
#include <stdio.h>
int main()
{
char a[10] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 0}, *p;
int i;
i = 8;
p = a + i;
printf("%s\n", p - 3);
return 0;
}
A.6
B.6789
C.'6'
D.789
正确答案:B
题解:
根据题意,我们可知a是一个有是个元素的字符数组,p是一个字符指针,将a+i赋值给p时,a代表数组第一个元素地址,+8以后指向字符9,打印时,以字符串打印,p - 3也就是往前移动三个字符,也就到了字符6,打印规则是直到遇到\0或者0;因此打印6789;
2、以下程序的输出结果是()
#include <iostream.h>
int main()
{
int x=3,y=3;
switch(x%2)
{
case 1:
switch (y)
{
case 0:
cout<<"first";
case 1:
cout<<"second";
break;
default:
cout<<"hello";
}
case 2:
cout<<"third";
}
return 0;
}
A.second third
B.hello
C.first second
D.hellothird
正确答案:D
题解:
这题考察多分支switch case语句的使用,首先我们看到x,y的初值均为3,首先第一个switch中,x % 2为1;我们走最外层case 1;里面又是一个多分支语句,此时判断y,y为3,没有对应的选项,走default,打印hello,注意,这里case语句没有break,因此会继续往下执行,走最外层case 2, 打印了third,因此答案为D;
3、以下能对二维数组a进行正确初始化的语句是()
A.int ta[2][]={{0,1,2},{3,4,5}};
B.int ta[][3]={{0,1,2},{3,4,5}};
C.int ta[2][4]={{0,1,2},{3,4},{5}};
D.int ta[][3]={{0,,2},{},{3,4,5}};
正确选项:B
题解:
本题考察二维数组相关知识,二维数组中可以不指定行,但是不能不指定列,故排除A;B正确;C只有两行,却用三行初始化,D第一行中双逗号中间没有数字,不符合语法;
4、能把函数处理结果的二个数据返回给主调函数,在下面的方法中不正确的是()
A.return这二个数
B.形参用数组
C.形参用二个指针
D.用二个全局变量
正确答案:A
题解:
return只能返回一个数,其他选项正确,故选A
5、int *p[4] 与选择项中的() 等价
A.int p[4]
B.int *p
C.int *(p[4])
D.int (*p)[4]
正确答案:C
题解:
根据题意得知,题目中的p为指针数组,而A是一维数组,排除,B是一级指针排除,C是指针数组,与题意相符,D是数组指针,排除;
6、设变量已正确定义,以下不能统计出一行中输入字符个数(不包含回车符)的程序段是
A.n=0;while(ch=getchar()!='\n')n++;
B.n=0;while(getchar()!='\n')n++;
C.for(n=0;getchar()!='\n';n++);
D.n=0;for(ch=getchar();ch!='\n';n++);
正确答案:D
题解:
D选项中的for循环括号中第一条语句为初始化语句,只会执行一次,故选D
7、以下代码执行之后ptr指向的内容是?
char *ptr;
char myString[] = "abcdefg";
ptr = myString;
ptr += 5;
A.Compiler error
B.fg
C.efg
D.defg
正确答案:B
题解:
ptr字符指针,数组名为第一元素的地址,+5指向f,故选B;
8、下面3段程序代码的效果一样吗()
int b;
(1)const int *a = &b;
(2)int const *a = &b;
(3)int *const a = &b;
A.(2)=(3)
B.(1)=(3)
C.(1)=(2)
D.都不一样
正确答案:C
题解:
一和二没有任何区别,都在*前,为常量指针,而三为指针常量;
9、32位系统中,定义**a[3][4],则变量占用内存空间为()。
A.4
B.48
C.192
D.12
正确答案:B
题解:
分析题意得知,a为一个指针数组,且该数组为三行四列的二维数组,数组中储存的是一个二级指针,因此数组元素个数为4*3=12,每个指针大小为4,故合计占48字节;选B;
10、假设在一个 32 位 little endian 的机器上运行下面的程序,结果是多少?
#include <stdio.h>
int main()
{
long long a = 1, b = 2, c = 3;
printf("%d %d %d\n", a, b, c);
return 0;
}
A.1,2,3
B.1,0,2
C.1,3,2
D.3,2,1
正确答案:B
题解:
由题意知,是在小端环境下,高字节放高地址处低字节放低地址处,而a,b,c又为longlong类型,占8字节,当他们传入printf函数时,又以%d(以十进制输出整型int),在printf函数栈帧中数据如下存放,依次取出1 0 2;故选B
四、编程题题解
1、字符串中找出连续最长的数字串
解题思路:我们定义三个字符串,一个用于获取输出字符串,一个用于保存当前最大字符串,每次遇到一个数字字符串,都将其保存起来,然后与最后一个结果字符串比较,若大于结果字符串,则拷贝给结果字符串,若小于,则不动结果字符串;
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str, cur, ret;
cin >> str;
int i = 0;
while(i < str.size())
{
cur.clear();
if(str[i] > '0' && str[i] < '9')
{
while(i < str.size() && str[i] >= '0' && str[i] <= '9')
{
cur.push_back(str[i]);
i++;
}
if(cur.size() > ret.size())
{
ret = cur;
}
}
else
{
i++;
}
}
cout << ret << endl;
}
2、数组中出现次数超过一半的数字
解题思路一:题目已经说明必有正确答案,因此必有一个数比整个数组的一半还要多,我们可以先对数组进行排序,然后直接取数组中间那个数;
解题思路二:已知众数比数组的一般还要多,我们遍历数组,首先取数组头那个数设为众数,并设置一个count计数,记录当前数字个数,我们依次与后面的数比较,若相等,计数count++,若不等计数count--,如果count为0,更换众数;(推荐写法)
根据上图,依次推导,本图未画完;
class Solution
{
public:
int MoreThanHalfNum_Solution(vector<int> numbers)
{
// 候选人数字
int candidate = -1;
// 出现次数
int count = 0;
// 循环遍历数组
for(int i = 0; i < numbers.size(); i++)
{
// 如果出现的次数为0,更新候选人
if(count == 0)
{
candidate = numbers[i];
count++;
}
else
{
// 如果出现次数不为0,且候选人数组与数组当前数字相等,count++,否则--
if(candidate == numbers[i])
count++;
else
count--;
}
}
// 重新统计选出的候选人数字是否超过了数组数字的一半
count = 0;
for(int i = 0; i < numbers.size(); i++)
{
if(numbers[i] == candidate)
count++;
}
if(count >= numbers.size() / 2)
return candidate;
else
return -1;
}
};