选择题
B
p指向'6' 而以%s打印遇到\0才停止
D
B
必须连续初始化,要不就不初始化。
A
return 只能返回一个值
C
D
for()的初始化内容 循环仅执行一次
B
这里打印错误 B选项为f
C
const在*之前 const* const修饰指针指向的内容,指针指向可以改变,指向的内容不能改变
const在*之后 *const const修饰指针,指针指向不能改变,指针指向的内容可以改变
B
32位系统下,指针大小为4字节
B
little endian为小端存储(倒着存储) int i=1 存放形式(16进制)是 01 00 00 00
大端的存放形式 00 00 00 01 long long为八字节
a: 01 00 00 00 00 00 00 00
b: 02 00 00 00 00 00 00 00
c: 03 00 00 00 00 00 00 00
当printf调用时,创建函数栈帧,栈从高地址向低地址申请空间。
且按创建顺序相反的顺序入栈
低地址 高地址
01 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 03 00 00 00 00 00 00 00
printf按%d打印
a 01 00 00 00 b:00 00 00 00 c:02 00 00 00
编程
思路:遍历一遍即可
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s,tmp,result;
getline(cin,s);
for(int i=0;i<=s.size();i++)
{
if(s[i]>='0' && s[i]<='9')
{
tmp+=s[i];
}
else
{
if(tmp.size()>result.size())
{
result=tmp;
}
tmp.clear();
}
}
cout<<result<<endl;
return 0;
}
思路一:哈希
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers)
{
int n=numbers.size();
int arr[10010]={0};
for(auto& e:numbers)
{
arr[e]++;
if(arr[e]>numbers.size()/2)
{
return e;
}
}
return 0;
}
};
思路二 先以nums[0]作为出现次数最多的人 int times=1 表示出现次数为1,遇到跟它不同的数 times--,当times=0时 选取另一个数重新开始计数
class Solution {
public:
int majorityElement(vector<int>& nums)
{
int number=nums[0];
int times=1;
for(int i=1;i<nums.size();i++)
{
if(times==0)
{
number=nums[i];
times=1;
}
else if(number==nums[i])
{
times++;
}
else
{
times--;
}
}
return number;
}
};