zoukankan      html  css  js  c++  java
  • PAT Basic 1057 数零壹 (20 分)

    给定一串长度不超过 1 的字符串,本题要求你将其中所有英文字母的序号(字母 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。

    输入格式:

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

    输出格式:

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

    输入样例:

    PAT (Basic)
    

    输出样例:

    3 4

    #include <iostream>
    #include <stack>
    #include <cctype>
    using namespace std;
    int main(){
        string s;getline(cin,s);
        int sum=0;
        for(int i=0;i<s.length();i++){
            if(isalpha(s[i])) {
                sum+=(tolower(s[i])-'a'+1);
            }
        }
        stack<int> sta;
        while(sum!=0){
            sta.push(sum%2);
            sum/=2;
        }
        int count[2]={0};
        while(!sta.empty()){
            count[sta.top()]++;
            sta.pop();
        }
        cout<<count[0]<<" "<<count[1];
        system("pause");
        return 0;
    }
     
  • 相关阅读:
    Placing Rooks-CF 1342E
    Yet Another Counting Problem-CF 1342C
    [SDOI2016]齿轮
    Rinne Loves Xor
    Labyrinth CodeForces
    Nastya and Scoreboard
    Teacher Bo HDU
    Blood Cousins Return
    D Tree HDU
    设计模式学习笔记(八、行为型-策略模式)
  • 原文地址:https://www.cnblogs.com/littlepage/p/11623500.html
Copyright © 2011-2022 走看看