目录
1. ACM编程题 标题:字符串中找出连续最长的数字串 | 时间限制:1秒 | 内存限制:32768K
2. 完善核心代码 标题:数组中出现次数超过一半的数字 | 时间限制:1秒 | 内存限制:65536K |
一. 单选
1. 以下程序的输出结果是()
#include <stdio.h>
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);
}
A 6
B 6789
C '6'
D 789
数组名是数组首元素的地址
p=a+i;//在首元素的地址处偏移量8个单位,指向字符 '9'地址
p-3;//往前偏移量3个单位,指向字符'6'的地址.因为是 %s打印,字符串打印,遇到'/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
x%2=1,y=3进入switch(y)循环,到default终止,输出hello因为switch(x%2) case(1)没有终止循环的语句,所以会继续执行case(2)语句所以最终的输出结果是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}};
二维数组进行初始化时,行可以省略,列不能省略c选项中的初始化造成数组越界访问,是非法的d选项中,数组初始化要连续初始化,d并没有连续初始化
4. 能把函数处理结果的二个数据返回给主调函数,在下面的方法中不正确的是()
A return这二个数
B 形参用数组
C 形参用二个指针
D 用二个全局变量
只能return一个数据类型
5. int *p[4] 与选择项中的()
等价
A int p[4]
B int *p
C int *(p[4])
D int (*p)[4]
int *p[4]是一个int类型的指针数组,是数组,数组大小为4A 是一个int类型数组,数组大小为4B 是一个int类型的指针D 是一个数组指针,是指针,每个指针指向一个int类型
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++);
for(ch=getchar(); ; )中,ch=getchar()是一条初始化语句,只获取一次值
7. 以下代码
char *ptr;
char myString[] = "abcdefg";
ptr = myString;
ptr += 5;
代码执行之后ptr指向的内容是?
A Compiler error
B f
C efg
D defg
char定义字符类型数组名是数组首元素的地址ptr+=5;//数组首元素地址偏移量5个单位,指向 f 的地址
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 都不一样
(1) 常量指针(2) 常量指针(3) 指针常量常量指针:指针所指向的空间的值是一个常量,不能修改其值,可以修改其指向指针常量:不能修改指针的指向,但是可以通过指针解引用修改其所指向空间的值区分: const 在* 左边为常量指针,const 在* 右边为指针常量
9. 32位系统中,定义**a[3][4],则变量占用内存空间为()。
A 4
B 48
C 192
D 12
在32位系统中,一个指针占用4个字节内容**a[3][4] 中入栈了3*4=12个指针变量12*4=48;
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
大端:低位存高地址小端:高位存低地址int i=1;大端:00 00 00 01小端:01 00 00 00----------------------->低地址 高地址
二. 编程
1. ACM编程题 标题:字符串中找出连续最长的数字串 | 时间限制:1秒 | 内存限制:32768K
#include <iostream>
#include<string>
using namespace std;
int main() {
string str,cur,ret;
getline(cin, str);
for(int i=0;i<=str.length();i++){
if(str[i]>='0'&&str[i]<='9'){//只记录数字字符串
cur+=str[i];
}
else{//遇到非数字字符串
if(cur.size()>ret.size()){
ret=cur;
}else{
cur.clear();//清理较小字符串
}
}
}
cout<<ret<<endl;
}
2. 完善核心代码 标题:数组中出现次数超过一半的数字 | 时间限制:1秒 | 内存限制:65536K |
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers)
{
if (numbers.empty()) return 0;
int times = 1;
int ret = numbers[0];
for (int i = 1; i < numbers.size(); i++)
{
if (times != 0)
{
if (ret == numbers[i])
times++;
else
times--;
}
else
{
ret=numbers[i];
times=1;
}
}
times=0;
for(int i=0;i<numbers.size();i++)
{
if(ret==numbers[i])
times++;
}
if(times>numbers.size()/2)
return ret;
else
return 0;
}
};