zoukankan      html  css  js  c++  java
  • 2020牛客寒假算法训练营第二场题解(持续更新中)

    A做游戏

    贪心,最多赢多少场只要保证尽可能赢就行了,即石头对应剪刀,剪刀对应布,布对应石头。

    #include<iostream>
    #include<cmath>
    using namespace std;
    int main()
    {
        long long a,b,c,x,y,z;
        cin>>a>>b>>c>>x>>y>>z;
        long long ans=0;
        ans+=min(a,y);
        ans+=min(b,z);
        ans+=min(c,x);
        cout<<ans<<endl;
        return 0;
    }
    View Code

    B排数字

    题意为给一串数字可以随意打乱保证616字串的数量最多,那么首先我们统计数串中1和6的个数,其他的数字均没有用,题目就转换成了给定数量的1和6,要组合成子串616最多的字符串。

    只需要比较6和1的数量即可,分为1的数量>=6的数量-1和1的数量<=6的数量-1两种情况考虑即可

    #include <iostream>
    #include <cstring>
    #include <string>
    #include <algorithm>
    #include <queue>
    #include <stack>
    #include <stdio.h>
    #include <cmath>
    #include <string.h>
    
    using namespace std;
    #define ll long long
    static const int WHITE=0;
    static const int GRAY=1;
    static const int BLACK=2;
    static const int INF=0x3f3f3f3f;
    ll Pow(ll a,ll b,ll mod){if(b==0) return 1%mod; ll sum=1; a=a%mod; while(b>0) { if(b%2==1) sum=(sum*a)%mod; b/=2; a=(a*a)%mod;}return sum;}
    int main()
    {
        //freopen("C:\Users\16599\Desktop\in.txt","r",stdin);
        int s,a,b;
        string str;
        a=0;b=0;
        cin>>s;
        cin>>str;
        for(int i=0;i<s;i++)
        {
            if(str[i]=='1')
            a++;
            if(str[i]=='6')
            b++;
        }
        if(b<2){cout<<"0"<<endl;return 0;}
        if(a>=b-1){cout<<b-1<<endl;return 0;}
        else{cout<<a<<endl;}
    
        
        return 0;
    }
    View Code

    D数三角

    这一题单独作为一篇博客来说明

    E做计数

    题意为判断有多少个三元组(i,j,k)满足i*j<=n并且√i+√j=√k。

    我们把两边同时开平方可以得到i+j+2√(ij)=k,若要满足i,j,k均为整数,只需要满足i*j为完全平方数即可。

    所以我们要枚举小于n的所有完全平方数,再对找它的因子就可以了。

    #include <iostream>
    #include <cstring>
    #include <string>
    #include <algorithm>
    #include <queue>
    #include <stack>
    #include <stdio.h>
    #include <cmath>
    #include <string.h>
    
    using namespace std;
    #define ll long long
    static const int WHITE=0;
    static const int GRAY=1;
    static const int BLACK=2;
    static const int INF=0x3f3f3f3f;
    ll Pow(ll a,ll b,ll mod){if(b==0) return 1%mod; ll sum=1; a=a%mod; while(b>0) { if(b%2==1) sum=(sum*a)%mod; b/=2; a=(a*a)%mod;}return sum;}
    vector <int> vec;
    int main()
    {
        //freopen("C:\Users\16599\Desktop\in.txt","r",stdin);
        int n;
        cin>>n;
        int ans=0;
        for(int i=1;;i++)
        {
            if(i*i<=n)
            vec.push_back(i*i);
            else
            break;
        }
        for(int i=0;i<vec.size();i++)
        {
            int cnt=0;
            for(int j=1;j<=sqrt(vec[i]);j++)
            {
                if(vec[i]%j==0)
                cnt++;
            }
            ans+=cnt*2-1;
        }
        cout<<ans<<endl;
        return 0;
    }
    View Code
  • 相关阅读:
    必懂的wenpack优化
    必懂的webpack高级配置
    webpack基础知识
    vue-cli
    codemirror使用
    js实现二叉树
    react-生命周期
    window 批量修改或去除文件后缀名
    十分钟搞清字符集和字符编码
    php判断一个值是否在一个数组中,区分大小写-也可以判断是否在键中
  • 原文地址:https://www.cnblogs.com/zlhdbk/p/12272694.html
Copyright © 2011-2022 走看看