蓝桥杯训练 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("
");
    }
    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
", 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
", mx);
        else
            printf("%d
", -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
", 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
", 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
", 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
",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
", dp[n]);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/aiterator/p/6670596.html