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

  1. 乘积尾零

高精度暴力模拟

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

const int N = 2e5 + 10;

int n,m;

string a,b;

//string add(string a,string b)
//{
//    int i,j,n,m;
//    int d[1000];
//    reverse(a.begin(),a.end());
//    reverse(b.begin(),b.end());
//    if(a.length()>b.length())
//    {
//        n=a.length();
//        for(i=b.length()-1; i<n; i++)
//            b=b+'0';
//    }
//    else
//    {
//        n=b.length();
//        for(i=a.length()-1; i<n; i++)
//            a=a+'0';
//    }
//    n=a.length();
//    m=b.length();
    d.resize(n+m+1);
//    for(i=0;i<=n+m;i++) d[i]=0;
//    for(i=0; i<n; i++)
//    {
//        int yu=0;
//        for(j=0; j<m; j++)
//        {
//            d[i+j]=d[i+j]+(a[i]-'0')*(b[j]-'0')+yu;
//            yu=d[i+j]/10;
//            d[i+j]%=10;
//        }
//        d[i+j]=yu;   
//    }
//    n=i+j;
//    while(d[n]==0&&n>0) n--;
    for(i=n; i>=0; i--)
        cout << d[i] ;
//    string c;
//    for(int i = n; i >= 0; i --)
//    {
//        c.push_back((char)(d[i] + '0'));
//    }
//    return c;
//}


int main()
{
    
//    cout << add("1234","4321") << endl;
//    string s = "1";
//    for(int i = 1; i <= 10; i ++)
//    {
//        for(int j = 1; j <=10; j ++)
//        {
//            int temp;
//            string ch;
//            cin >> temp;
//            while(temp)
//            {
//                ch.push_back((char)(temp%10 + '0'));
//                temp /= 10;
//            }
//            reverse(ch.begin(),ch.end());
//            s = add(s,ch);
//            cout << s << endl;
//        }
//    } 
//    int ans = 0;
//    for(int i = s.size() - 1; i >= 0; i --)
//    {
//        if(s[i] != '0') break;
//        if(s[i] == '0')
//        {
//            ans ++;
//        }
//    }
    int ans = 31;
    cout << ans << endl;;
    return 0;
}
  1. 全球变暖

统计一个连通块的数量和处于边上的数量,如果相等,那么这个连通块代表的小岛一定会被淹没

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

typedef pair<int,int> PII;

int n,m;

char a[1010][1010];

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

bool st[1010][1010];


PII bfs(int x,int y)
{
    int l = 0,r = 1;
    queue<PII> q;
    q.push({x,y});
    st[x][y] = true;
    while(!q.empty())
    {
        int dx = q.front().first;
        int dy = q.front().second;
        q.pop();
        bool is_l = false;
        for(int i = 0; i < 4; i ++)
        {
            int lx = dx + fx[i];
            int ly = dy + fy[i];
            if(a[lx][ly] == '.') is_l = true;
            if(a[lx][ly] == '#' && !st[lx][ly])
            {
                st[lx][ly] = true;
                q.push({lx,ly});
                r ++;
            }
        }    
        if(is_l) l ++;
    }
    return {l,r};
}


int main()
{
    cin >> n;
    for(int i = 1; i <= n; i ++)
    {
        cin >> a[i];    
    }    
    int ans = 0;
    for(int i = 1; i <= n; i ++)
    {
        for(int j = 0; j < n; j ++)
        {
            if(a[i][j] == '#' && !st[i][j])
            {
                PII t = bfs(i,j);
//                cout << i << " " << j << " " << t.first << " " << t.second << endl;
                if(t.first == t.second) 
                    ans ++;
            }
            
        }
    }
    cout << ans << endl;
    return 0;
}
  1. 第几天

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

int n,m;

int mon[] = {0,31,28,31,30,31};
//             1, 2, 3, 4, 5

int main()
{
    int ans = 1;
    for(int i = 1 ;i <= 5; i ++)
    {
        for(int j = 1; j <= mon[i]; j ++)
        {
            ans ++;
            if(i >= 5 && j >= 4)
            {    
                cout << ans << endl;
                return 0;
            }
        }
    }
    return 0;
}
  1. 明码

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

int n,m;

int a[100][100];

int main()
{
//    for(int i = 1; i <= 10; i ++)
//    {
//        for(int j = 1; j <= 32; j ++)
//        {
//            cin >> a[i][j];
//        }
//        for(int j = 1 ;j <= 32; j ++)
//        {
//            for(int k = 7; k >= 0; k --)
//            {
//                if(a[i][j] & (1<<k))
//                {
//                     cout << "1 ";    
//                }
//                else
//                {
//                    cout << "0 ";
//                }
//            }
//            if(j % 2 == 0) cout << endl;
//        }
//    }

    int ans = 1;
    for(int i = 1; i <= 9; i ++)
    {
        ans *= 9;
    }
    cout << ans << endl;
    return 0;
}

5.测试次数

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

int n,m;

int f[10][10010];

int main()
{
    cin >> n;
    memset(f,0x3f,sizeof f);
    for(int i = 0; i <=n; i ++)
    {
        f[1][i] = i;
    }
    for(int i = 2; i <= 3; i ++)
    {
        f[i][0] = 0;
        for(int j = 1; j <= n; j ++)
        {
            for(int k = 1; k <= j; k ++)
            {
                f[i][j] = min(f[i][j],max(f[i - 1][k - 1],f[i][j - k]) + 1);
            }
        }
    }
    cout << f[3][n] << endl;
    return 0;
}

6.递增三元组

#include <iostream>
#include <cstring>
#include <algorithm>
#define int long long

using namespace std;

const int N = 2e5 + 10;

int n,m;

int a[N],b[N],c[N];


signed main()
{
    cin >> n;
    for(int i = 1; i <= n; i ++)
    {
        int t;
        cin >> t;
        a[t] ++;
    }
    for(int i = 1; i < N; i ++) a[i] += a[i-1];
    for(int i = 1; i <= n; i ++)
    {
        int t;
        cin >> t;
        b[i] = t;
    }
    int ans = 0;
    for(int i = 1; i <=n; i ++)
    {
        int t;
        cin >> t;
        c[t] ++;
    }
    for(int i = 1; i < N; i ++) c[i] += c[i-1];
    for(int i = 1; i <= n; i ++)
    {
        int t = b[i];
        ans += a[t-1] * (c[N-1] - c[t]);
    }
    cout << ans << endl;
    return 0;
}

7.螺旋折线

#include <iostream>
#include <cstring>

using namespace std;

int n,m;

int l = 1,sum = 0;

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

int main()
{
    int x,y;
    cin >> x >> y;
    int d = max(abs(x),abs(y));
    if(x <= y)
    {
        cout << 4 * d * d - (abs(x) + abs(y)) << endl;
    }
    else
    {
        cout << 4 * d * d + (abs(x) + abs(y)) << endl;
    }
    return 0;
}

8.日志统计

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

typedef pair<int,int> PII;

const int N = 1e5 + 10;

int n,m,d,k;

PII th[N];
int cnt[N];
bool st[N];

int main()
{
    cin >> n >> d >> k;
    for(int i = 0; i < n; i ++)
    {
        cin >> th[i].first >> th[i].second;
    }
    sort(th,th + n);
    for(int i = 0,j = 0; i < n; i ++)
    {
        int id = th[i].second;
        cnt[id] ++;
        while(th[i].first - th[j].first >= d)
        {
            cnt[th[j].second] --;
            j ++;
        }
        if(cnt[id] >= k) 
        {
            st[id]= true;
        }
    }
    for(int i = 0; i < N; i ++)
    {
        if(st[i]) cout << i << endl;
    }
     return 0;
}

9.乘积最大

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

using namespace std;

#define int long long

const int mod = 1000000009;

const int N = 2e5 + 10;

int n,m,k;

int a[N];

signed main()
{
    cin >> n >> k;
    int ans = 1;
    for(int i = 1 ; i <= n; i ++)
    {
        cin >> a[i];
    }
    sort(a + 1,a + n +1);
    int l = 1,r = n;
    int sig = 1, mn = 1;
    if(k & 1)
    {
        ans = a[r];
        if(ans < 0) sig = -1;
        r --;
        k --;
    }
    while(k)
    {
        int ll = a[l] * a[l + 1];
        int rr = a[r] * a[r - 1];
        // cout << ll * sig <<  " " << rr * sig << " " << ans << endl;
        if((ll * sig) >= (rr * sig))
        {
            ans = (ll % mod) * (ans % mod);
            l += 2;
        }
        else
        {
            ans = (rr % mod) * (ans % mod);
            r -= 2;
        }
        k -= 2;
    }
    cout << ans % mod << endl;
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值