昨晚在微信群看到一個讀者發(fā)的面試題目,從網(wǎng)上截圖出來的,我百思不得其解,題目如圖。
幸好,我學(xué)過棧
然后我寫了個小程序
第一個方法比較笨,當(dāng)我寫完自己的代碼后,看到有同學(xué)發(fā)了自己的代碼,我趕緊就發(fā)了個紅包,一個是為了鼓勵大家多討論問題,一個是為了贊揚這樣的行為。
第一個方法就是取出每一個bit,壓棧,然后再出棧,你想怎么排序都沒有問題。
#include "stdio.h"
#include "stdlib.h"
struct List{
int data;
struct List * next;
};
struct Stack{
struct List *head;
int size;
};
struct Stack * StackInit(void)
{
struct Stack *stack = NULL;
stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->head = (struct List *)malloc(sizeof(struct List));
stack->head->next = NULL;
stack->size = 0;
return stack;
}
int StackPush(struct Stack *stack,int data)
{
struct List *tmp = (struct List *)malloc(sizeof(struct List));
tmp->data = data;
tmp->next = stack->head->next;
stack->head->next = tmp;
stack->size++;
//printf("push:%d \n",data);
return 0;
}
int IsStackEmpty(struct Stack *stack)
{
if(stack->head->next == NULL){
//printf("stack null\n");
return 1;
}else{
//printf("stack is not null\n");
return 0;
}
}
int StackPop(struct Stack *stack,int *data)
{
struct List *tmp = NULL;
if(IsStackEmpty(stack))
return -1;
tmp = stack->head->next;
*data = tmp->data;
stack->head->next = tmp->next;
stack->size--;
if(tmp!=NULL)
free(tmp);
//printf("pop:%d \n",*data);
return 0;
}
int main(void)
{
int i = 0;
unsigned char a = 0x71;
unsigned char b = 0;
struct Stack *stack = NULL;
stack = StackInit();
printf("a:0x%x\n",a);
for(i = 0;i<8;i++)
{
if(a&0x10) StackPush(stack,1);
else StackPush(stack,0);
if(a&0x20) StackPush(stack,1);
else StackPush(stack,0);
if(a&0x40) StackPush(stack,1);
else StackPush(stack,0);
if(a&0x80) StackPush(stack,1);
else StackPush(stack,0);
if(a&0x01) StackPush(stack,1);
else StackPush(stack,0);
if(a&0x02) StackPush(stack,1);
else StackPush(stack,0);
if(a&0x04) StackPush(stack,1);
else StackPush(stack,0);
if(a&0x08) StackPush(stack,1);
else StackPush(stack,0);
}
for(i = 0;i<8;i++)
{
int d = 0;
StackPop(stack,&d);
if(i == 0)
if(d == 1) b |= 0x80;
if(i == 1)
if(d == 1) b |= 0x40;
if(i == 2)
if(d == 1) b |= 0x20;
if(i == 3)
if(d == 1) b |= 0x10;
if(i == 4)
if(d == 1) b |= 0x08;
if(i == 5)
if(d == 1) b |= 0x04;
if(i == 6)
if(d == 1) b |= 0x02;
if(i == 7)
if(d == 1) b |= 0x01;
}
printf("\nb:0x%x\n",b);
return 0;
}
執(zhí)行如下:
除了用棧來實現(xiàn),群里有位同學(xué)的答案讓我眼前一亮
#include <stdio.h>
struct charbits{
unsigned char data1 : 4;
unsigned char data2 : 4;
};
union dataChange{
struct charbits charBits;
unsigned char data;
}datchange,*p;
int main(){
unsigned char temp;
p = &datchange;
datchange.data = 0x34;
temp = p->charBits.data1;
p->charBits.data1 = p->charBits.data2;
p->charBits.data2 = temp;
printf("data is %x", datchange.data);
return 0;
}
完美演示了位域、共用體、結(jié)構(gòu)體、指針的幾個知識點~
測試下位域的作用
#include <stdio.h>
struct charbits{
unsigned char data1 : 1;
unsigned char data2 : 3;
};
struct charbits1{
unsigned char data1;
unsigned char data2;
};
int main(){
struct charbits a;
struct charbits1 b;
printf("%d %d\n",sizeof(a),sizeof(b));
/*賦值1看看*/
a.data1 = 1;
printf("%d\n",a.data1);
/*賦值3看看*/
a.data1 = 3;
printf("%d\n",a.data1);
/*賦值3看看*/
a.data2 = 3;
printf("%d\n",a.data2);
/*賦值7看看*/
a.data2 = 7;
printf("%d\n",a.data2);
/*賦值8看看*/
a.data2 = 8;
printf("%d\n",a.data2);
return 0;
}
data1只能存 0和1,如果存其他值的話就會溢出變成0。
data2是 3bits ,只能存 0~7,我們存 8 進去,就溢出變成 0。
PS:想加入技術(shù)群的同學(xué),加了我好友后,就給我發(fā)「籃球的大肚子」這句話,有可能機器人打瞌睡,可以多發(fā)幾次,不要發(fā)與技術(shù)無關(guān)的消息或者推廣。
免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺僅提供信息存儲服務(wù)。文章僅代表作者個人觀點,不代表本平臺立場,如有問題,請聯(lián)系我們,謝謝!