2017年蓝桥杯省赛 C++ B组

1.方格分割

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int n = 7,m;

int fx[] = {1,-1,0,0};
int fy[] = {0,0,1,-1};

int ans = 0;

bool st[10][10];

void dfs(int x,int y)
{
    if(x == 1||x == 7|| y == 1||y == 7)
    {
        ans ++;    
        return;
    }
    for(int i = 0; i < 4; i ++)
    {
        int dx = x + fx[i];
        int dy = y + fy[i];
        if(st[dx][dy]) continue;
        st[n - dx + 1][n - dy + 1] = true;
        st[dx][dy] = true;
        dfs(dx,dy);
        st[n - dx + 1][n - dy + 1] = false;
        st[dx][dy] = false;
    }
}

int main()
{
    st[4][4] = true;
    dfs(4,4);
    cout << ans/4 << endl;
    return 0;
}

2.包子凑数

扩展欧几里得定理,当全部数的最大公约数为不为1是,可以配凑出所以的数;

为1时,筛出所以能够配凑出的数,再遍历统计;

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 2e3 + 10;

int n,m;

int a[N],f[N][N];

int gcd(int a,int b)
{
    if(b == 0) return a;
    return gcd(b,a%b);
}

bool st[N];

int main()
{
    cin >> n;
    int d;
    for(int i = 1; i <= n; i ++)
    {
        cin >> a[i];
        if(i == 1)
        {
            d = a[i];
        }
        else
        {
            d = gcd(d,a[i]);
        }
    }
    if(d != 1)
    {
        cout << "INF" << endl;
    }
    else
    {
        st[0] = true;
        for(int i = 1; i <= n; i ++)
        {
            for(int j = a[i]; j <= 10000; j ++)
            {
                if(st[j -a[i]]) st[j] = true;
            }
        }
        int ans = 0;
        for(int i = 10000; i >= 0; i --)
        {
            if(!st[i])
            {
                ans ++;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

3.分巧克力

二分答案,每次check(x)时,每块蛋糕可以分出(长/x) * (宽/x),累计统计,注意取整;

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 2e5 + 10;

#define int long long

int n,m,k;

int h[N],w[N];

int check(int x)
{
    int cnt = 1;
    for(int i = 1; i <= n; i ++)
    {
        int t = (h[i]/x)*(w[i]/x);    
        cnt += t;
    }
    return cnt;
}

signed main()
{
    cin >> n >> k;
    for(int i = 1; i <= n; i ++)
    {
        cin >> h[i] >> w[i];
    }
    int l = 1,r = 100000;
    while(l < r)
    {
        int mid = l + r + 1 >> 1;
        if(check(mid) > k)
        {
            l = mid;
        }
        else
        {
            r = mid - 1;
        }
    }
    cout << r << endl;
    return 0;
}

4.购物单

/*
180.90    88
10.25      65
56.14        90
104.65        90
100.30       88
297.15        50
26.75       65
130.62        50
240.28       58
270.62        80
115.87       88
247.34       95
73.21        90
101.00        50
79.54        50
278.44        70
199.26       50
12.97        90
166.30       78
125.50       58
84.98        90
113.35       68
166.57       50
42.56        9
81.90       95
131.78        80
255.89       78
109.17        90
146.69       68
139.33       65
141.16       78
154.74        80
59.42        80
85.44       68
293.70       88
261.79       65
11.30       88
268.27       58
128.29       88
251.03        80
208.39       75
128.88       75
62.06        90
225.87       75
12.89       75
34.28       75
62.16       58
129.12    50
218.37    50
289.69        80
*/

#include <iostream>
#include <cmath>
using namespace std;

int n,m;

double ans = 0;

int main()
{
    for(int i = 1; i <= 50 ; i ++)
    {
        double a,b;
        cin >> a >> b;
        ans += a * (b/100);
    }
//    cout << ceil(ans/100)*100 << endl;
    cout << "5200" << endl;
    return 0;
}

5.等差数数列

筛出所以的质数,暴力枚举相邻质数的公差;

#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;

int n,m;

const int N = 2e5 + 10;

int cnt,primer[N];
int ans = 1000000;
bool st[N];

void get_primer(int x)
{
    for(int i = 2; i <= x; i ++)
    {
        if(!st[i]) primer[++ cnt] = i;
        for(int j = 1; primer[j] * i <= x; j ++)
        {
            st[primer[j] * i] = true;
            if(i % primer[j] == 0) break;
        }
    }
}

signed main()
{
    get_primer(50000);
//    for(int i = 1; i <= cnt; i ++)
//    {
//        cout << primer[i] << " ";
//    }
//    for(int i = 1, j = 1; i <= cnt; i ++)
//    {
//        for(int j = i + 1; j <= cnt; j ++)
//        {
//            int d = primer[j] - primer[i];
//            int tem = 2;
//            for(int k = j + 1; k <= cnt; k ++)
//            {
//            
//                if((primer[k] - primer[i]) == tem * d)
//                {    
                    cout << primer[k] << " " << primer[i] << " " << d << " " << tem << endl;
//                    tem ++;
//                
//                }
//                if(tem == 10)
//                {    
//                    
//                    ans = min(d,ans);
//                    break;
//                }
//            }
//        }
//    }
    ans = 210;
    cout << ans << endl;
    return 0;
}

6.承压计算

#include <iostream>
#include <cstring>
#include <string>
using namespace std;

int n,m;

double a[100][100],s[100][100];



int main()
{
//    n = 29;
//    for(int i = 1; i <= n; i ++)
//    {
//        for(int j = 1; j <= i ; j ++)
//        {
//            cin >> a[i][j];            
//        }        
//    }
//    for(int i = 1; i <= n; i ++)
//    {
//        for(int j = 1; j <= i ; j ++)
//        {
//            cout << a[i][j] << " ";        
//        }
//        cout << endl;
//    }
//    for(int i = 2; i <= n + 1; i ++)
//    {
//        a[i][1] = a[i][1] + a[i-1][1]/2.0;
//        for(int j = 2; j < i; j ++)
//        {
//            a[i][j] = a[i][j] + (a[i-1][j] + a[i-1][j-1])/2.0;
//        }
//        a[i][i] = a[i][i] + a[i-1][i-1]/2.0;
//    }
//    for(int i = 1; i <= n + 1; i ++)
//    {
//        for(int j = 1; j <= i ; j ++)
//        {
//            cout << a[i][j] << " ";        
//        }        
//        cout << endl;
//    }
//    int mx = 1, mn = 1;
//    for(int i = 1; i <= n + 1; i ++)
//    {
//        if(a[n + 1][i] > a[n + 1][mx]) mx = i;
//        if(a[n + 1][i] < a[n + 1][mn]) mn = i;
//    }
//    printf("%lf\n",a[n + 1][mn]);
//    printf("%lf\n",a[n + 1][mx] * (long long)2086458231 / a[n + 1][mn]);

    double  ans = 72665192664.000000;
    printf("%.0lf\n",ans);
    return 0;
}

7.日期问题

#include <iostream>
#include <cstring>
#include <string>
#include <set>
#include <algorithm>
using namespace std;

int n,m;

int d[10];

//string a,b,s;

set<string> q;

string to_s(int x)
{
    string ch;
    if(x<10)
    {
        ch += "0";
        ch += (char)(x + '0');
    }
    else if(x < 100)
    {
        ch += (char)(x/10 + '0');
        ch += (char)(x%10 + '0'); 
    }
    else
    {
        while(x)
        {
            ch += (char)(x%10 + '0');
            x /= 10;
        }
        reverse(ch.begin(),ch.end());
    }
    return ch;
}

int month[]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
            //   1  2  3  4  5  6  7  8  9 10 11 12

bool is_run(int x)
{
    if(x % 400 != 0 && x % 4 == 0 || x % 400 == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void check(string st)
{
    int year = 0;
    for(int i = 0; i < 4; i ++)
        year = (year) * 10 + (st[i] - '0');
    int mon = (st[4] - '0') *10 + (st[5] - '0');    
    int day = (st[6] - '0') *10 + (st[7] - '0');
//    cout << year << mon << day << endl;
    if(year < 1960 || year > 2059) return;
    if(is_run(year) && mon == 2)
    {
        if(day >=1 && day <= 29)
        {
            printf("%d-%02d-%02d\n",year,mon,day);
            return;
         }
    }
    if( mon >=1 && mon <= 12 && day <= month[mon] && day >= 1)
    {
        printf("%d-%02d-%02d\n",year,mon,day);
    }
    return;
}

int a,b,c;

int main()
{
    scanf("%d/%d/%d",&a,&b,&c);
    q.insert(to_s(1900 + a) + to_s(b) + to_s(c));
    q.insert(to_s(1900 + c) + to_s(a) + to_s(b));
    q.insert(to_s(1900 + c) + to_s(b) + to_s(a));
    q.insert(to_s(2000 + a) + to_s(b) + to_s(c));
    q.insert(to_s(2000 + c) + to_s(a) + to_s(b));
    q.insert(to_s(2000 + c) + to_s(b) + to_s(a));
    for(auto t:q)
    {
        check(t);
    }
    return 0;
}

8.k倍区间

#include <iostream>
#include <algorithm>
#include <cstring>

#define int long long

using namespace std;
const int N = 2e5 + 10;

int n,m,k;

int a[N],p[N],s[N];
int ans;

signed main()
{    
    
    cin >> n >> k;
    for(int i = 1; i <= n; i ++)
    {
        cin >> a[i];
        a[i] = (a[i - 1] + a[i]) % k;
    }
    int ans = 0;
    for(int i = 1; i <= n; i ++)
    {
        int t = a[i];
        ans += s[t];
        s[t] ++;
    }
    cout << ans + s[0] << endl;
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值