笔试强训day14(幸运的袋子,计算日期到天数的转换)

目录

第一题-选择题1

第二题-幸运袋子

第三题-计算日期到天数的转化


第一题-选择题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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

penguin_bark

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值