蓝桥杯训练 day4 题解

A - 整除的尾数

  • 枚举一下后两位数。

代码:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int a, b;
    while(scanf("%d%d", &a, &b), a || b)
    {
        a *= 100;
        bool is = false;
        for(int i=0; i<100; ++ i)
        {
            if((a + i) % b == 0)
            {
                printf(is == false ? "%02d" : " %02d", i);
                is = true;
            }
        }
        printf("\n");
    }
    return 0;
}

B - Victor and Machine

中文题意链接:http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=619&pid=1001 (中文题面加不上, OJ上只有英文题面)

直接计算。

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int x, y, w, n;
    while(scanf("%d%d%d%d", &x, &y, &w, &n) != EOF)
    {
        int res = x / w + 1; // 一个x+y时刻弹出小球的数量
        int t = n / res; //需要几个x+y时刻
        int ans = t * (x + y);
        n %= res;
        if(n == 0)
            ans = ans - y - x % w;//为0时,说明上一个周期就已经是n个球了,那么应该把( y )和 (x中多余的减去) 减去
        else
            ans += (n - 1) * w;
        printf("%d\n", ans);
    }
    return 0;
}

C - Distribution money

中文题意链接:http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=613&pid=1001

  • 用变量mx分别记录数组中的最大值的下表,用arr数组来记录每个ID出现的次数。
  • 如果 arr[mx] > sum - [mx]则输出mx, 否则输出-1
#include<bits/stdc++.h>
using namespace std;

int arr[10007];
int main()
{
    int n;
    while(scanf("%d", &n) != EOF)
    {
        memset(arr, 0, sizeof(arr));
        int mx = -1;
        for(int i=0; i<n; ++ i)
        {
            int a;
            scanf("%d", &a);
            arr[a] ++;
            if(mx == -1 || arr[a] > mx)
                mx = a;
        }
        if(n - arr[mx] < arr[mx])
            printf("%d\n", mx);
        else
            printf("%d\n", -1);
    }
    return 0;
}

D - A hard puzzle

两个方案。
1.找周期。
#include<bits/stdc++.h>
using namespace std;

vector<int> vec;

int main()
{
    int a, b;
    while(scanf("%d%d", &a, &b) != EOF)
    {
        vec.clear();
        a %= 10;
        int res = a;
        vec.push_back(res);
        for(int i=1; i<=b; ++ i)
        {
            res = (res * a) % 10;
            if(res == vec[0])
                break;
            vec.push_back(res);
        }
        b = (b - 1) % (vec.size());
        printf("%d\n", vec[b]);
    }
    return 0;
}
2.快速幂。
#include<bits/stdc++.h>
using namespace std;

int quick_pow(int a, int b)
{
    int res = 1;
    while(b)
    {
        if(b & 1)
            res = (res * a) % 10;
        a = (a * a) % 10;
        b >>= 1;
    }
    return res;
}
int main()
{
    int a, b;
    while(scanf("%d%d", &a, &b) != EOF)
    {
        printf("%d\n", quick_pow(a%10, b));
    }
    return 0;
}

E - Race to 1 Again

题解见:http://www.cnblogs.com/aiterator/p/6444840.html

F - 饭卡

简单01背包。

  • 首先将物品按照花费从小到大排序。
  • n-5的钱去买前n-1个物品中尽可能多的物品。
  • 最后用剩下的钱买最后一件物品,这样最后剩的钱最少。
#include<bits/stdc++.h>
using namespace std;

int val[1007], dp[1007];

int main()
{
    int n;
    while(scanf("%d", &n), n)
    {
        for(int i=0; i<n; ++ i)
            scanf("%d", &val[i]);
        int m;
        scanf("%d", &m);

        if(m < 5)
        {
            printf("%d\n", m);
            continue;
        }
        sort(val, val+n);
        memset(dp, 0, sizeof(dp));
        for(int i=0; i<n-1; ++ i)
        {
            for(int j=m-5; j>=val[i]; -- j)
                dp[j] = max(dp[j], dp[j-val[i]] + val[i]);
        }
        printf("%d\n",m - dp[m-5] - val[n-1]);
    }
    return 0;
}

G - 钱币兑换问题

完全背包;

1 分,2分, 3分可以用任意次。

#include<bits/stdc++.h>
using namespace std;

long long dp[32800];
int main()
{
    memset(dp, 0, sizeof(dp));
    dp[0] = 1;
    for(int i=1; i<=3; ++ i)
    {
        for(int j=i; j<=32768; ++ j)
            dp[j] += dp[j-i];
    }
    int n;
    while(scanf("%d", &n) != EOF)
    {
        printf("%lld\n", dp[n]);
    }
    return 0;
}

转载于:https://www.cnblogs.com/aiterator/p/6670596.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值