一、单选题
选B。
原因: HTTPS:Hyper Text Transfer Protocol Secure,超文本传输安全协定,使用SSL加密
选A。
选B。
选B。
void get_memory(char** p, int n) {
p = (char*)malloc(n);
}
void test() {
char* str = NULL;
get_memory(str, 100);
// str 仍然为 NULL
strcpy(str, "hello"); // 运行错误
free(str);
}
正确改写:
void get_memory(char** p, int n) {
if (p == NULL) return 1;
*p = (char*)malloc(n);
}
void test() {
char* str = nullptr;
get_memory(&str, 100);
strcpy(str, "hello");
if (str != NULL) free(str);
}
原因:指针作为函数参数传递
传递指针会发生拷贝,被拷贝后的指针再指向其他的地址对原指针产生不了任何作用,那我们就应该让原指针与拷贝后的指针指向的依然是同一地址,那么拷贝后的指针再去指向别的区域,原指针也会同样指向该内存。
为了让它们的地址相同,所以要传递二级指针。
比如,下面的代码会输出内存分配成功!,这个函数就写正确了。
int* getMemory(int** ptr)
{
*ptr = new int;
return *ptr;
}
int main()
{
int* p = nullptr;
getMemory(&p);
if (p == nullptr)
cout << "内存分配失败!" << endl;
else
{
cout << "内存分配成功!" << endl;
delete p;
}
return 0;
}
选A。
说明:
B:静态方法属于整个类,在对象创建之前就已经分配空间,类的非静态成员要在对象创建后才有内存,所有静态方法只能访问静态成员,不能访问非静态成员,
选C。
- 下面代码会输出什么?
char str1[] = "abc";
char str2[] = "abc";
const char str3[] = "abc";
const char str4[] = "abc";
const char* str5 = "abc";
const char* str6 = "abc";
cout << boolalpha << ( str1==str2 ) << endl;
cout << boolalpha << ( str3==str4 ) << endl;
cout << boolalpha << ( str5==str6 ) << endl;
答:false, false, true
字符串"abc"在常量区里只有一份。
三组比较的都是地址:5,6为指向常量字符串的指针,所指向的地址相同。
而前4个是将字符串的值复制给栈上数组1~4,比较的是数组的首地址,各数组的地址不相同。
选C。
原因:
| 类型 | Win32 | Win64 |
|---|---|---|
| char | 1 | 1 |
| short | 2 | 2 |
| int | 4 | 4 |
| long | 4 | 4 |
| long long | 8 | 8 |
| float | 4 | 4 |
| double | 8 | 8 |
| * | 4 | 8 |
说明:X86\X64平台下指针占用字节
X86是32位操作系统,CPU是32位的,数据宽度也是32位,CPU一次可处理的位数是32位数据,也就是4个字节。
X64是64位操作系统,CPU是64位的,数据宽度也是64位,CPU一次可处理的位数是64位数据,也就是8个字节。
二、多选题
选BC。
原因:
char表示范围:-128 ~ 127
unsigned char表示范围:0 ~ 255
说明:
unsigned char是用1字节存储的,它没有符号位,因此能表示0~1111 1111,最大值就是0xFF,也就是255。
char类型是8位,最高位是符号位,0正1负,所以01111111是127。
-127是10000001,而10000000换算过来就是-128。
一个n位无符号int型数值,其范围为0 ~ 2^(n) - 1;
一个n位有符号int型数值,其范围为-2^(n-1) ~ 2^(n-1) - 1。
选BCD。
原因:
A:3, 2
y先执行++a,a为2;
y再执行printf,a入栈,在打印到终端之前切换到x
x执行++a,a为3;
x执行printf,输出3;再切换到y
y执行打印,输出2
B:2 3
x先执行++a,a为2;
x再执行printf,输出2;切换到y
y执行++a,a为3;
y执行printf,输出3;
C:3 3
x先执行++a,a为2;切换到y
y执行++a,a为3;
y执行printf,输出3;切换到x
x执行printf,输出3
D: 2 2
x先执行++a,a为2,在写入内存之前切换到y
y执行++a,a为2,写入内存并执行printf,输出2;切换到x
x将++a操作写入内存,a的值保持2,再执行printf,输出2
说明:
(1)并发和并行
并发:交替进行,单核CPU
并行:一起执行,多核CPU
(2)两个线程可随时被强占;
(3)++a和printf不是原子指令,可随时被打断。
选BD。
原因:
线程共享的资源:堆,全局变量,静态变量
线程独有的资源:栈,寄存器
选BCD。
原因:C2=3的出栈序列:1324,1342,2314,2341,4321
选ACD。
选BC。
原因:
链表是不能采用二分查找的,因为链表不具备随机访问特性。
二分查找的必要条件是:线性表、有序
选ABD。
原因:
三、编程题
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> v(n, 0);
for (int i = 0; i < n; i++)
{
cin >> v[i];
}
int maxx = *max_element(v.begin(), v.end());
vector<bool> isPrime(maxx, 1);
cout << "2~" << maxx << "的质数为: " << endl;
for (int i = 2; i*i < maxx; ++i)
{
if (isPrime[i] == 1)
{
for (int j = i*i; j < maxx; j+=i)
{
isPrime[j] = 0;
}
}
}
int cnt = 0;
vector<int> prime;
for (int i = 2; i < maxx; ++i)
{
if (i%10 == 0)
{
cout << endl;
}
if (isPrime[i] == 1)
{
cout << i << " ";
prime.push_back(i);
cnt++;
}
}
cout << endl;
int times = 0;
int num = 1;
for (int idx = 0; idx < n; idx++)
{
for (num = 1; num < v[idx]+1; num++)
{
for (int i = 0;i < cnt; i++)
{
for (int j = 0; j < cnt; j++)
{
for (int k = 0; k < cnt; k++)
{
if (num == pow(prime[i], 2) + pow(prime[j], 3) + pow(prime[k], 4))
{
cout << num << "= " << prime[i] << "^2 +" << prime[j] << "^3 +" << prime[k] << "^4" << endl;
times++;
}
}
}
}
}
cout << "1~" << num-1 << "有" << times << "个这样的数字" << endl;
}
cout << endl;
return 0;
}
优化为三层for循环:
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> v(n, 0);
for (int i = 0; i < n; i++)
{
cin >> v[i];
}
int maxx = *max_element(v.begin(), v.end());
vector<bool> isPrime(maxx, 1);
cout << "2~" << maxx << "的质数为: " << endl;
for (int i = 2; i*i < maxx; ++i)
{
if (isPrime[i] == 1)
{
for (int j = i*i; j < maxx; j+=i)
{
isPrime[j] = 0;
}
}
}
int cnt = 0;
vector<int> prime;
for (int i = 2; i < maxx; ++i)
{
if (i%10 == 0)
{
cout << endl;
}
if (isPrime[i] == 1)
{
cout << i << " ";
prime.push_back(i);
cnt++;
}
}
cout << endl;
vector<int> ans(maxx+1, 0);
for (int i = 0;i < cnt; i++)
{
for (int j = 0; j < cnt; j++)
{
for (int k = 0; k < cnt; k++)
{
int tmp = pow(prime[i], 2) + pow(prime[j], 3) + pow(prime[k], 4);
if (tmp < maxx+1)
{
ans[tmp] = 1;
cout << tmp << "= " << prime[i] << "^2 +" << prime[j] << "^3 +" << prime[k] << "^4" << endl;
}
}
}
}
for (int i = 1; i < maxx+1; i++)
{
ans[i] = ans[i] + ans[i-1];
}
for (int i = 0; i < n; i++)
{
cout << "1~" << v[i] << "有" << ans[v[i]] << "个这样的数字" << endl;
}
return 0;
}
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int main()
{
int n;
cin >> n;
unordered_map<long, int> m;
vector<int> v(n, 0);
for (int i = 0; i < n; i++)
{
cin >> v[i];
m[v[i]]++;
}
int q;
cin >> q;
long cur = 0;
while (q--)
{
int x;
cin >> x;
cur += x;
long res = 0;
for (auto& e : m)
{
res += abs(e.first + cur) * e.second;
}
cout << res << endl;
}
return 0;
}