Find Small A(hdu5980)二进制 / 模拟

2016年大连站区域赛

PS :在第一次模拟这场比赛时,看到很多人做这个题就去做这个题,没想到竟然没有看懂题意,还是很菜啊QAQ

题意分析:给定一个N,有N个数,每个数的二进制,每8位数为一个字节,97的进行比较,判断有多少个字节等于97?

 

 

模拟

/*************************************************************************
	> File Name: J.cpp
	> Author: ccdxc
	> Mail: linyi0430@gmail.com
	> Created Time: 2018年11月18日 星期日 13时00分57秒
 ************************************************************************/

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n , ans;
string s = "10000110";
int judge(int a){
    int t = 0;
    string s1 = "";
    while(a){
        if(a % 2){
            s1 += "1";
        }
        else s1 += "0";
        a/=2;
    }
    int k = log2(s1.size());
    if(k * k != s1.size()){
        k++;
        int num = 1 << k ;
        for(int i = s1.size();i < num;i++)
            s1 += "0";
    }
    for(int i = 0;i < s1.size() - 7;i+=8){
        string s2 = s1.substr(i , 8);
        //cout << s2 <<endl;
        if(s == s2){
            t++;
        }
    }
    return t;
}
int main(){
    //freopen("input.in","r",stdin);
    //freopen("test.out","w" ,stdout);
    int a;
    while(scanf("%d",&n) == 1 && n){
        ans = 0;
        for(int i = 1;i <= n;i++){
            cin >> a;
            if(a < 97) continue;
            else if(a == 97) ans++;
            else ans += judge(a);
        }
        cout << ans << endl;
    }
    return 0;
}

二进制

/*************************************************************************
	> File Name: ACJ.cpp
	> Author: ccdxc
	> Mail: linyi0430@gmail.com
	> Created Time: 2018年11月19日 星期一 23时31分33秒
 ************************************************************************/

#include<bits/stdc++.h>
using namespace std;
int n , a;
int main(){
    while(~scanf("%d",&n)){
        int ans = 0;
        while(n--){
            scanf("%d",&a);
            if(a < 97) continue;
            else
                while(a){
                    if(a % 256 == 97) ans++;
                    a >>= 8;
                    //cout << a<<endl;
                }
        }
        cout << ans <<endl;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值