题目链接:点击打开链接
不足之处:我这样字写的速度太慢了,占用的内存也特别大
解决方案:
//大概50分钟
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int cnt;
short v[1000000];
} BIGINT;
typedef struct{
int cnt;
int v[1000000];
} Stack;
//将这个大整数定义为全局变量。
BIGINT bigint;
void solve();
// 除以16,返回余数
int DIV16(BIGINT * bigint);
void push(Stack * stack,int d){
stack->v[stack->cnt++] = d;
}
int pop(Stack * stack){
if(stack->cnt > 0){
return (stack->v[(stack->cnt--) - 1]);
} else{
return -1;
}
}
char record2SMALL[8][5]={
"0",
"1",
"10",
"11",
"100",
"101",
"110",
"111"
};
char record2[16][5] = {
"0000",
"0001",
"0010",
"0011",
"0100",
"0101",
"0110",
"0111",
"1000",
"1001",
"1010",
"1011",
"1100",
"1101",
"1110",
"1111"
};
char record16[17] = "0123456789ABCDEF";
int main()
{
int T;
int k;
scanf("%d",&T);
k = 0;
while(k<T){
printf("case #%d:\n",k);
solve();
k++;
}
}
//1. 读入数据
//2. 先转化为16进制数字,保存在stack里面,然后从stack里面取出来,同时转化2进制的数字
void solve(){
static BIGINT bigint;
static char str[1000001];
static Stack stack;
static Stack stackCOPY;
int i,k;
int tmp;
scanf("%s",str);
bigint.cnt = 0;
stack.cnt = 0;
stackCOPY.cnt = 0;
//按照100进制存到BIGINT里面
for(k = strlen(str) - 1; k >= 0; k = k-2){
if(k==0){
bigint.v[bigint.cnt++]=str[k] - '0';
} else {
bigint.v[bigint.cnt++] = str[k]-'0' + (str[k-1] - '0') * 10;
}
}
while(bigint.cnt>0){
push(&stack,DIV16(&bigint));
}
stackCOPY = stack;
//输出2进制
tmp = pop(&stack);
if(tmp<8){
printf("%s",record2SMALL[tmp]);
}
else printf("%s",record2[tmp]);
while(stack.cnt > 0){
printf("%s",record2[pop(&stack)]);
}
printf(" ");
while(stackCOPY.cnt>0){
printf("%c",record16[pop(&stackCOPY)]);
}
printf("\n");
}
int DIV16(BIGINT * bigint){
int carry;
int t;
int k;
carry = 0;
if(bigint->v[bigint->cnt-1] < 16){
carry = bigint->v[bigint->cnt-1];
bigint->cnt--;
}
for(k = bigint->cnt-1;k>=0;k--){
t = (bigint->v[k] + 100*carry);
bigint->v[k] = t / 16;
carry = t % 16;
}
return carry;
}