首页
学习
活动
专区
圈层
工具
发布
29 篇文章
1
PAT (Basic Level) Practice (中文)1047 编程团体赛
2
PAT (Basic Level) Practice (中文)1083 是否存在相等的差
3
PAT (Basic Level) Practice (中文)1082 射击比赛
4
PAT (Basic Level) Practice (中文)1081 检查密码
5
PAT (Basic Level) Practice (中文)1077 互评成绩计算
6
PAT (Basic Level) Practice (中文)1076 Wifi密码
7
PAT (Basic Level) Practice (中文)1064 朋友数
8
PAT (Basic Level) Practice (中文)1063 计算谱半径
9
PAT (Basic Level) Practice (中文)1057 数零壹
10
PAT (Basic Level) Practice (中文)1056 组合数的和
11
PAT (Basic Level) Practice (中文)1042 字符统计
12
PAT (Basic Level) Practice (中文)1041 考试座位号
13
PAT (Basic Level) Practice (中文)1023 组个最小数
14
PAT (Basic Level) Practice (中文)1022 D进制的A+B
15
PAT (Basic Level) Practice (中文)1019 数字黑洞
16
PAT (Basic Level) Practice (中文)1007 素数对猜想
17
PAT (Basic Level) Practice (中文)1091 N-自守数
18
PAT (Basic Level) Practice (中文)1026 程序运行时间
19
PAT (Basic Level) Practice (中文)1061 判断题
20
PAT (Basic Level) Practice (中文)1086 就不告诉你
21
PAT (Basic Level) Practice (中文)1016 部分A+B
22
PAT (Basic Level) Practice (中文)1012 数字分类
23
PAT (Basic Level) Practice (中文)1013 数素数
24
PAT (Basic Level) Practice (中文)1011 A+B 和 C
25
PAT (Basic Level) Practice (中文)1009 说反话
26
PAT (Basic Level) Practice (中文)1008 数组元素循环右移问题
27
PAT (Basic Level) Practice (中文)1006 换个格式输出整数
28
PAT (Basic Level) Practice (中文)1004 成绩排名
29
PAT (Basic Level) Practice (中文)1002 写出这个数

PAT (Basic Level) Practice (中文)1057 数零壹

1057 数零壹

给定一串长度不超过 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。

输入格式:

输入在一行中给出长度不超过 10​5、以回车结束的字符串。

输出格式:

在一行中先后输出 0 的个数和 1 的个数,其间以空格分隔。

输入样例:

PAT (Basic)

输出样例:

3 4

代码:

代码语言:javascript
复制
#include<stdio.h>
#include<string.h>
char str[100001];

void fun(int n)
{
    int temp0=0;
    int temp1=0;
    while(n)
    {
        int t=n%2;
        if(t==0) temp0++;
        else temp1++;
        n/=2;
    }
    printf("%d %d\n",temp0,temp1);
}
int main()
{
    gets(str);
    int len=strlen(str);
    int arr[26];
    int i;
    int sum=0;
    for(i=0;i<26;i++)
    {
        arr[i]=i+1;
    }
  /*  for(i=0;i<len;i++)  方法一
    {
        if(str[i]=='a'||str[i]=='A')sum+=1;
        if(str[i]=='b'||str[i]=='B')sum+=2;
        if(str[i]=='c'||str[i]=='C')sum+=3;
        if(str[i]=='d'||str[i]=='D')sum+=4;
        if(str[i]=='e'||str[i]=='E')sum+=5;
        if(str[i]=='f'||str[i]=='F')sum+=6;
        if(str[i]=='g'||str[i]=='G')sum+=7;
        if(str[i]=='h'||str[i]=='H')sum+=8;
        if(str[i]=='i'||str[i]=='I')sum+=9;
        if(str[i]=='j'||str[i]=='J')sum+=10;
        if(str[i]=='k'||str[i]=='K')sum+=11;
        if(str[i]=='l'||str[i]=='L')sum+=12;
        if(str[i]=='m'||str[i]=='M')sum+=13;
        if(str[i]=='n'||str[i]=='N')sum+=14;
        if(str[i]=='o'||str[i]=='O')sum+=15;
        if(str[i]=='p'||str[i]=='P')sum+=16;
        if(str[i]=='q'||str[i]=='Q')sum+=17;
        if(str[i]=='r'||str[i]=='R')sum+=18;
        if(str[i]=='s'||str[i]=='S')sum+=19;
        if(str[i]=='t'||str[i]=='T')sum+=20;
        if(str[i]=='u'||str[i]=='U')sum+=21;
        if(str[i]=='v'||str[i]=='V')sum+=22;
        if(str[i]=='w'||str[i]=='W')sum+=23;
        if(str[i]=='x'||str[i]=='X')sum+=24;
        if(str[i]=='y'||str[i]=='Y')sum+=25;
        if(str[i]=='z'||str[i]=='Z')sum+=26;
    } */
    for(i=0;i<len;i++)  //方法二
    {
        if(str[i]>='a'&&str[i]<='z') sum+=(str[i]-97+1);
        if(str[i]>='A'&&str[i]<='Z') sum+=(str[i]+32-97+1);
    }

    fun(sum);
    //printf("%d\n",sum);
    return 0;

}
下一篇
举报
领券