目录
第一题-选择题1
主要依赖于继承方式,继承方式可以是私有,此时不能访问父类
赋值兼容规则
1.子类对象可以赋值给父类对象
2.子类对象地址可以赋值给父类对象的指针
3.父类对象可以引用子类对象
第二题-选择题2
第三题-参数解析
思路
首先将存储一行的字符串依次遍历,然后判断是否为' " ',如果是引号则空格不能跳过,否则遇到空格跳过同时将存储的字符串计数统计,最后记得将最后一组数据放到vector中
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main ()
{
string str;
while(getline(cin, str))
{
string tmp = "";
vector<string> ret;
bool flag = true;
for(int i = 0; i < str.size(); ++i)
{
//遇到'""'
if(str[i] == '"')
{
flag = !flag;
}
//遇到' ' ,同时没有遇到'"'
else if(str[i] == ' ' && flag)
{
ret.push_back(tmp);
tmp = "";
}
//只遇到纯字符
else
{
tmp+=str[i];
}
}
//将最后一个字符串插入
ret.push_back(tmp);
cout<<ret.size()<<endl;
for(auto& s : ret)
{
cout<<s<<endl;
}
}
return 0;
}
第四题-跳石板
思路:
主要就是每走一步之前要和注意走的这一步会对接下来的步数有什么影响,具体看代码
#include<iostream>
#include<vector>
#include<math.h>
#include<limits.h>//INT_MAX
using namespace std;
//求出每个约数保存到a中
void get_nums(int x, vector<int>& a)
{
for(int i = 2; i <= sqrt(x); ++i)
{
if(x%i == 0)
{
a.push_back(i);
if(x/i != i)
a.push_back(x/i);
}
}
}
int jump(int n, int m)
{
vector<int> count(m+1,INT_MAX);//存储步数结果,INT_MAX表示不可能达到,从1开始计数
count[n] = 0;//将开始移动的位置初始化为0
//从n开始移动到m
for(int i = n; i < m;++i)
{
if(count[i] == INT_MAX)//INT_MAX表示不可能达到
continue;
vector<int> a;//获取保存约数
get_nums(i, a);//获取约数
//将每一步的可能分析
for(int j = 0 ; j < a.size(); ++j)
{
//如果当前约数步数没到m,同时该位置可以到达
if(a[j]+i<=m && count[a[i]+i]!=INT_MAX)
{
//需要挑选一个最小值
count[a[j]+i] = count[a[j]+i] > count[i]+1 ? count[i]+1 : count[a[j]+i];
}
else if(a[j]+i <= m)
{
count[a[j]+i] = count[i]+1;
}
}
}
return count[m]==INT_MAX ? -1 : count[m];
}
int main ()
{
int n, m ;
while(cin>>n>>m)
{
int jp = jump(n, m);
cout<<jp<<endl;
}
return 0;
}