Google Search Box

自訂搜尋

Wednesday, July 14, 2010

Re: [閒聊] 炮灰Day1 程式設計概要

作者: squallz (God Knows) 看板: Examination
標題: Re: [閒聊] 炮灰Day1 程式設計概要
時間: Wed Jul 14 21:53:41 2010

第一題
#include
#include
int main()
{
int most_significant_set_bit(int num){
int i,j;
long k=0;
if(num==0)
return (-1);

for(i=0;i<32;i++){
k=num & 0x80000000;
if(k==0x80000000)
return (31-i);
else
num=num<<1;
}
}

int bit;
bit=most_significant_set_bit(444);
printf("bit %d\n",bit);
system("pause");
}


第二題
#include
#include
typedef struct node
{
int d;
struct node *next;
}node;

int main()
{
void split(node *h,node **h1,node **h2)
{
struct node *odd=NULL,*even=NULL;
while(h!=NULL){
if(h->d%2!=0){
(odd==NULL)?(*h1=odd=h):(odd->next=h);
odd=h;
}
else{
(even==NULL)?(*h2=even=h):(even->next=h);
even=h;
}
h=h->next;
}
odd->next=NULL;
even->next=NULL;
odd=*h1;
even=*h2;
while(odd!=NULL){
printf("oddnode=%2d\n",odd->d);
odd=odd->next;
}
while(even!=NULL){
printf("evennode=%2d\n",even->d);
even=even->next;
}
}

struct node *head,*ptr,*newnode,*h1,*h2;
int i,a[4]={4,5,7,8};

head=(struct node *) malloc(sizeof(struct node));
ptr=head;
head->d=3;
printf("node_1=%2d\n",head->d);
for (i=0;i<4;i++){
newnode=(struct node *) malloc(sizeof(struct node));
newnode->d=a[i];
newnode->next=NULL;
ptr->next=newnode;
ptr=newnode;
printf("node_%d=%2d\n",i+2,newnode->d);
}
split(head,&h1,&h2);
system("pause");
}




第三題
#include
#include
#include
int main()
{
int string_seach(char str[],char pat[]){
int i,j,k,strlength=0,patlength=0;
strlength=strlen(str);
patlength=strlen(pat);
if(strlength && patlength==0)
printf("Error");
else{
for(i=0;i k=strncmp(str+i,pat,patlength);
if(k==0)
return i;
}
}
}
int position;
char str1[50]="I will pass this exam.";
char pat1[50]="pass";
position=string_seach(str1,pat1);
printf("%d\n",position);
system ("pause");
}



第四題就不會了
今天也是去當炮灰

No comments:

Post a Comment