目录
第一题-选择题1
答案:B0 : :displayoB0 : :displayoB0: :displayo
思路:
首先创建三个不同类型的对象,然后调用fun,由于fun在编译阶段就完成了,所以和虚函数没有关系,只会调用父类函数
第二题-幸运袋子
思路:
首先我们要知道,如果一个袋子里面没有1的话,是不可以成为幸运袋子的,利用回溯的算法,判断当前是不是幸运袋子,然后依次放球判断,如果不是则回溯删除球
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//收集存储的队列,队列长度,当前数据位置,当前数据和,当前数据乘
int get_count(vector<int>& x, int n, int pos, int sum, int multi)
{
int count = 0;//计算方案数
for(int i = pos; i < n; ++i)
{
sum+=x[i];
multi*=x[i];
//如果当前数是幸运袋子
if(sum > multi)
{
count += 1 + get_count(x, n, i+1, sum, multi);
}
//如果当前不是幸运袋子,但是是1,说明后面还是有方案的
else if(sum<=multi && x[i] == 1)
{
count += get_count(x, n, i+1, sum, multi);
}
//如果都不符合,回溯
else
{
break;
}
sum-=x[i];
multi/=x[i];
//去重
while(i < n-1 && x[i] == x[i+1])
{
i++;
}
}
return count;
}
int main ()
{
int n;
while(cin>>n)
{
vector<int> x(n);
for(int i = 0; i < n; ++i)
{
cin>> x[i];
}
//排序
sort(x.begin(), x.end());
cout<<get_count(x, n, 0, 0, 1)<<endl;
}
return 0;
}
第三题-计算日期到天数的转化
思路:
别的不多说,只说十年润百年不润,每四百年再一润
#include<iostream>
using namespace std;
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main ()
{
int year, month, day;
while(cin>>year>>month>>day)
{
int count = 0;
for(int i = 1; i <= (month-1); ++i)
{
if((((year%4 == 0 && year%100 != 0) || (year%400 == 0))) && i == 2)
{
++count;
}
count+=days[i];
}
count+=day;
cout<<count<<endl;
}
return 0;
}