HDU Find Small A(找‘a’)C++

As is known to all,the ASCII of character 'a' is 97. Now,find out how many character 'a' in a group of given numbers. Please note that the numbers here are given by 32 bits’ integers in the computer.That means,1digit represents 4 characters(one character is represented by 8 bits’ binary digits).

Input

The input contains a set of test data.The first number is one positive integer N (1≤N≤100),and then N positive integersai (a_{i}2^{32}- 1) follow

Output

Output one line,including an integer representing the number of 'a' in the group of given numbers.

Sample

InputOutput
3 97 24929 100 
3

 Translate:

众所周知,字符“a”的 ASCII 是 97。现在,找出一组给定数字中有多少个字符“a”。请注意,这里的数字由计算机中的 32 位整数给出。这意味着,1 位表示 4 个字符(一个字符由 8 位的二进制数字表示)。

输入包含一组测试数据。第一个数字是一个正整数 N (1≤N≤100),然后是 N 个正整数 (1≤ai≤2^32 - 1 )

输出一行,包括一个整数,表示给定数字组中“a”的数量。

内心: 这不简单????只要是一个数大于等于97不就是包含了一个‘a’了?看看样例,的确如此!

........Wrong Answer.......

这道题考察的非常之精细

思路:

题目给出一个字符由8位二进制数字表示,那么我们每次就看8位,如果得到的数为97sum++;

接着把这8位移走。

代码:

#include<iostream>
#include<math.h>
using namespace std;
int sum;
int main()
{
	int n;
	cin >> n;
	while (n--)
	{
		int  a;
		int t = pow(2, 8);
		cin >> a;
		while (a)
		{
			if (a%t==97)sum++;
			a >>=8;
		}
	}
	cout << sum << endl;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值