Coding Problem
Problem Description
Here is the logic.I am the brother of mata Chuang.Mata chuang is the brother of cxk.So what am I?Am I the grand-brother of cxk?Or am I in a position that same brother level of cxk?Or cxk is the grand-brother of me?
Fine,it doesn't matter to the problem.Problem can't be a problem until it has description.But description are just words.Meaningless words.So,we treat words miserably like this, given a string s consist of 3*n lowercase letter.The binary ascii string of s is a string consist of 8*3*n 0-1 string(one char,8bits,The lower significnt bit is in the front, the higher is in the back.). We want you output the binary ascii string.But we don't want the string too long.So we want you put every 6 bit together(The higher significnt bit is in the front, the lower is in the back.) and output the corresponding array,with a length of 8*3*n/6.Please check the sample for specific
Input
one line,one string. (n<=1e5)
Output
one line,4*n integer representing the target array.
Sample Input
abc
Sample Output
33 36 27 6
Hint
- s=abc binary ascii string=10000110 01000110 11000110
- answer binary array=100001 100100 011011 000110
- final answer array=33 36 27
问题链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1005&cid=846
问题分析:
- 样例输入abc a的ASCII码为97 a->97,b->98,c->99
- a:97 转为8位二进制为 0110 0001 存储时反过来存,即为:1000 0110
- b:98 转为8位二进制为 0110 0010 存储时反过来存,即为:0100 0110
- c:99 转为8位二进制为 0110 0011 存储时反过来存,即为:1100 0110
- 将字符串转化的二进制存入一个二维数组中,遍历整个数组
- 设置一个计数器,cnt == 6时,就将当前所记录的数输出
-
此题的格式为:每个数后都有一个空格,且没有换行符
源代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
char s[1001000];
int a[1001000][10];
int main()
{
cin >> s;
int len = strlen(s);
int q = 0, p = 0;
for(int i = 0; i < len; i++)
{
int t = s[i];
q = 0;
while(t!=0) //将当前数转为二进制
{
a[p][q] = t%2;
t = t>>1; //t = t / 2;
q++;
}
p++;
}
int s = 0, t = 5, c = 32; //每6位输出,最高位是2的5次方=>32,s累加
for(int i = 0; i < p; i++)
{
for(int j = 0; j < 8; j++)
{
s += a[i][j]*c;
c = c>>1;
t--; //每记录一个数,t--
if(t == -1) //每记录到6个数
{
printf("%d ", s);
s = 0; //s置为0, t重新置为5;
t = 5;
c = 32;
}
}
}
return 0;
}