一、选择题
1.
const char *p,char const *p和char *const p区别(面试题常见)-CSDN博客
2.
#include <iostream>
using namespace std;
void f(int a = []()
{
static int b = 1;
return b++;
}())
{
std:cout << a;
}
int main()
{
f();
f();
return 0;
}
输出结果为12
3.
3.以下错误的行为是D
A. std:cout<<"测试\n";
B. operator<<(std::cout,"测试\n");
C. endl(std::cout);
D. std::cout<<endl;
D选项中endl前也需要加std::
endl和\n都是换行的 endl 是 iostream中的预设操纵符 endl在输出流中插入一个换行符然后刷新缓冲区(endl在iostream中执行的是一个操作而不是提供数据) endl 出了写\n进输出流外还调用了flush函数 刷新缓冲区 让数据直接显示或写进文件 如果写\n 就不一定立即显示因为数据可能还存在缓冲区没有立即写入设备 为了提高效率可以写\n 这样会更快。
4.下面对静态数据成员的描述中,正确的是( D )。
A) 静态数据成员可以在类体内进行初始化
B) 静态数据成员不可以通过类的对象调用
C) 静态数据成员不能受private(私有)控制符的作用
D) 静态数据成员可以直接通过类名调用
二、编程题
//C语言解法
bool checkRecord(char* s) {
int absents = 0, lates = 0;
int n = strlen(s);
for (int i = 0; i < n; i++) {
char c = s[i];
if (c == 'A') {
absents++;
if (absents >= 2) {
return false;
}
}
if (c == 'L') {
lates++;
if (lates >= 3) {
return false;
}
} else {
lates = 0;
}
}
return true;
}
//C语言解法
char *licenseKeyFormatting(char *s, int k) {
int lenS = strlen(s), lenChs = 0;
char chs[lenS];
for (int i = 0; i < lenS; ++i) {
if (s[i] != '-')chs[lenChs++] = s[i]>='a'?s[i]-('a'-'A'):s[i];
}
char *res = calloc(lenChs / k + lenChs + 2,sizeof(char));
int pos = 0;
for (int i = 0; i < lenChs; ++i) {
res[pos++] = chs[i];
if ((i+1) % k == lenChs % k&&i<lenChs-1) {
res[pos++] = '-';
}
}
return res;
}