思路:先转化为二进制再转化为八进制
代码:
#include <stdio.h>
#include <string.h>
#define M 100005
char s[M*4];
char str[M];
int ans[150005];
int main(){
int n;
//char temp[6];
scanf("%d", &n);
while(n --){
scanf("%s", str+1);
memset(s, 0, sizeof(s));
int i, j, len = strlen(str+1)+1, temp;
// printf("%d..\n", len);
for(i = 1; i < len; ++i){
if(str[i] >= '0'&&str[i] <= '9'){
temp = str[i] - '0';
}
else temp = 10+str[i]-'A';
j = i*4;
while(temp){
s[j] = temp%2+'0';
j--;
temp /= 2;
}
while(j > (i-1)*4){
s[j--] = '0';
}
}
// printf("%s", &s[1]);
memset(ans, 0, sizeof(ans));
int tot = 0;
for(i = (len-1)*4; i-3 >= 0; i -= 3){
ans[tot++] = (s[i]-'0')+(s[i-1]-'0')*2+(s[i-2]-'0')*4;
}
temp = 1;
if(s[i] != '\0'){
++tot;
while(i > 0&&s[i]!='\0'){
ans[tot-1] += (s[i]-'0')*temp;
temp *= 2;
--i;
}
}
while(tot>0&&ans[tot] == 0) --tot;
for(i = tot; i >= 0; -- i) printf("%d", ans[i]);
puts("");
//
//for(i = ())
}
return 0;
}
原文:http://blog.csdn.net/shengweisong/article/details/42616041