计蒜客2018 蓝桥杯省赛 B 组模拟赛(五)部分题解

B素数个数

思路:全排列枚举并判断前导0计素数个数

code

#include<vector>
#include<map>
#include<queue>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string>
#include<string.h>
#include<cstdio>
using namespace std;
typedef long long ll;
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define S(a) scanf("%d",&a)
const int maxn=101;
bool check(int n)
{
    int k=sqrt(n);
    FOR(i,2,k+1)
    if(n%i==0)
        return false;
    return true;
}
int main()
{
    ios::sync_with_stdio(false);
    int a[8];
    FOR(i,0,8)
    a[i]=i;
    int ans=0;
    int t=pow(10,7);//第一位不能为0
    do
    {
        int sum=0;
        FOR(i,0,8)
        sum=sum*10+a[i];
        if(sum/t&&check(sum))
            ans++;
    }
    while(next_permutation(a,a+8));//全排列函数
    cout<<ans<<endl;
    return 0;
}

D快速幂

#include <iostream>
using namespace std;

int pw(int x, int y, int p) {
    if (!y) {
        return 1;
    }
    int res = pw(x*x,y>>1,p);
    if (y & 1) {
        res = res * x % p;
    }
    return res;
}

int main() {
    int x, y, p;
    cin >> x >> y >> p;
    cout << pw(x, y, p) << endl;
    return 0;
}

E:末尾0的个数

#include <iostream>
using namespace std;
int main() {
    int n, ans = 0;
    cin >> n;
    while (n) {
        ans += n/=5;
    }
    cout << ans << endl;
    return 0;
}

G合并数字

思路:用一个栈保存前驱元素。然后从前往后扫描就行。

code:

#include<bits/stdc++.h>
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define S(a) scanf("%d",&a)
#define  pb(a) push_back(a)
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int maxn= 100010;
int a[maxn];
string s,str;
int n,m;
int T;
map<int,int>mp;
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    FOR(i,0,n)
    cin>>a[i];
    int cnt=0;
    int j=1;
    stack<int>st;
    st.push(a[0]);
    while(j<n)
    {
        while(!st.empty()&&abs(st.top()-a[j])==1)
        {
            cnt++;
            if(a[j]>st.top())
            {
                j++;
            }
            else
            {
                st.pop();
            }
        }
        st.push(a[j]);
        j++;
    }
    cout<<cnt<<endl;
    return 0;
}
H蒜头君下棋
#include<vector>
#include<map>
#include<queue>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string>
#include<string.h>
#include<cstdio>
using namespace std;
typedef long long ll;
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define S(a) scanf("%d",&a)
int main()
{
    ios::sync_with_stdio(false);
    int n,m;
    cin>>n>>m;
    if(n==1)
    {
        cout<<m<<endl;
        return 0;
    }
    if(m==1)
    {
        cout<<n<<endl;
        return 0;
    }
    int ans=0;
    int a=m/2;
    if(m%2)a++;
    int b=m/2;
    FOR(i,0,n)
    {
        if(i%2)
        ans+=b;
        else
        ans+=a;
    }
    cout<<ans<<endl;
    return 0;
}


猜你喜欢

转载自blog.csdn.net/reallsp/article/details/79689408