1057. 数零壹(20)

本题要求计算给定字符串中字母的序号之和,并分析该数值的二进制表示中有多少个0和多少个1。输入字符串长度不超过10^5,通过遍历字符串并累加字符对应的字母序号来实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1057. 数零壹(20)
时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B
判题程序 Standard 作者 CHEN, Yue

给定一串长度不超过105的字符串,本题要求你将其中所有英文字母的序号(字母a-z对应序号1-26,不分大小写)相加,得到整数N,然后再分析一下N的二进制表示中有多少0、多少1。例如给定字符串“PAT (Basic)”,其字母序号之和为:16+1+20+2+1+19+9+3=71,而71的二进制是1000111,即有3个0、4个1。
输入格式:
输入在一行中给出长度不超过105、以回车结束的字符串。
输出格式:
在一行中先后输出0的个数和1的个数,其间以空格分隔。
输入样例:
PAT (Basic)
输出样例:
3 4

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <vector>
#include <string>
#include <cfloat>
#include <queue>
#include <map>

using namespace std;
const int MaxN = 100010;


int main()
{
#ifdef _DEBUG
    freopen("data.txt", "r+", stdin);
#endif // _DEBUG
    std::ios::sync_with_stdio(false);

    char str[MaxN];
    long long res = 0;
    cin.getline(str,MaxN);
    for (int i = 0; str[i]; ++i)
    {
        if ('a' <= str[i] && str[i] <= 'z')
            res += str[i] - 'a' + 1;
        else if ('A' <= str[i] && str[i] <= 'Z')
            res += str[i] - 'A' + 1;
    }

    int yi = 0, ning = 0;
    while (res)
    {
        if (res & 1) ++yi;
        else ++ning;
        res >>= 1;
    }
    cout << ning << " " << yi;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值