前幾天面試,有一題想不明白,請(qǐng)教大家!
typedef struct
{
int a:2;
int b:2;
int c:1;
}test;
test t;
t.a = 1;
t.b = 3;
t.c = 1;
printf("%d",t.a);
printf("%d",t.b);
printf("%d",t.c);
謝謝!
t.a為01,輸出就是1
t.b為11,輸出就是-1
t.c為1,輸出也是-1
3個(gè)都是有符號(hào)數(shù)int嘛。
這是位擴(kuò)展問題
01
11
1
編譯器進(jìn)行符號(hào)擴(kuò)展
求組合數(shù): 求n個(gè)數(shù)(1....n)中k個(gè)數(shù)的組合....
如:combination(5,3)
要求輸出:543,542,541,532,531,521,432,431,421,321,
#include<stdio.h>
int pop(int *);
int push(int );
void combination(int ,int );
int stack[3]={0};
top=-1;
int main()
{
int n,m;
printf("Input two numbers:\n");
while( (2!=scanf("%d%*c%d",&n,&m)) )
{
fflush(stdin);
printf("Input error! Again:\n");
}
combination(n,m);
printf("\n");
}
void combination(int m,int n)
{
int temp=m;
push(temp);
while(1)
{
if(1==temp)
{
if(pop(&temp)&&stack[0]==n) //當(dāng)棧底元素彈出&&為可能取的最小值,循環(huán)退出
break;
}
else if( push(--temp))
{
printf("%d%d%d ",stack[0],stack[1],stack[2]);//§ä¨ì¤@?
pop(&temp);
}
}
}
int push(int i)
{
stack[++top]=i;
if(top<2)
return 0;
else
return 1;
}
int pop(int *i)
{
*i=stack[top--];
if(top>=0)
return 0;
else
return 1;
}
1、用指針的方法,將字符串“ABCD1234efgh”前后對(duì)調(diào)顯示
#include <stdio.h>
#include <string.h>
#include <dos.h>
int main()
{
char str[] = "ABCD1234efgh";
int length = strlen(str);
char * p1 = str;
char * p2 = str + length - 1;
while(p1 < p2)
{
char c = *p1;
*p1 = *p2;
*p2 = c;
++p1;
--p2;
}
printf("str now is %s\n",str);
system("pause");
return 0;
}
2、有一分?jǐn)?shù)序列:1/2,1/4,1/6,1/8……,用函數(shù)調(diào)用的方法,求此數(shù)列前20項(xiàng)的和
#include <stdio.h>
double getValue()
{
double result = 0;
int i = 2;
while(i < 42)
{
result += 1.0 / i;//一定要使用1.0做除數(shù),不能用1,否則結(jié)果將自動(dòng)轉(zhuǎn)化成整數(shù),即0.000000
i += 2;
}
return result;
}
int main()
{
printf("result is %f\n", getValue());
system("pause");
return 0;
}
]]>
</Content>
<PostDateTime>2006-4-17 10:17:34</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>白日?做夢(mèng)!</PostUserNickName>
<rank>一級(jí)(初級(jí))</rank>
<ranknum>user1</ranknum>
<credit>100</credit>
<ReplyID>34231324</ReplyID>
<TopicID>4691482</TopicID>
<PostUserId>695883</PostUserId>
<PostUserName>free131</PostUserName>
<Point>0</Point>
<Content>
<![CDATA[
有一個(gè)數(shù)組a[1000]存放0--1000;要求每隔二個(gè)數(shù)刪掉一個(gè)數(shù),到末尾時(shí)循環(huán)至開頭繼續(xù)進(jìn)行,求最后一個(gè)被刪掉的數(shù)的原始下標(biāo)位置。
以7個(gè)數(shù)為例:
{0,1,2,3,4,5,6,7} 0-->1-->2(刪除)-->3-->4-->5(刪除)-->6-->7-->0(刪除),如此循環(huán)直到最后一個(gè)數(shù)被刪除。
方法1:數(shù)組
#include <iostream>
using namespace std;
#define null 1000
int main()
{
int arr[1000];
for (int i=0;i<1000;++i)
arr[i]=i;
int j=0;
int count=0;
while(count<999)
{
while(arr[j%1000]==null)
j=(++j)%1000;
j=(++j)%1000;
while(arr[j%1000]==null)
j=(++j)%1000;
j=(++j)%1000;
while(arr[j%1000]==null)
j=(++j)%1000;
arr[j]=null;
++count;
}
while(arr[j]==null)
j=(++j)%1000;
cout<<j<<endl;
return 0;
}方法2:鏈表
#include<iostream>
using namespace std;
#define null 0
struct node
{
int data;
node* next;
};
int main()
{
node* head=new node;
head->data=0;
head->next=null;
node* p=head;
for(int i=1;i<1000;i++)
{
node* tmp=new node;
tmp->data=i;
tmp->next=null;
head->next=tmp;
head=head->next;
}
head->next=p;
while(p!=p->next)
{
p->next->next=p->next->next->next;
p=p->next->next;
}
cout<<p->data;
return 0;
}
方法3:通用算法
#include <stdio.h>
#define MAXLINE 1000 //元素個(gè)數(shù)
/*
MAXLINE 元素個(gè)數(shù)
a[] 元素?cái)?shù)組
R[] 指針場(chǎng)
suffix 下標(biāo)
index 返回最后的下標(biāo)序號(hào)
values 返回最后的下標(biāo)對(duì)應(yīng)的值
start 從第幾個(gè)開始
K 間隔
*/
int find_n(int a[],int R[],int K,int& index,int& values,int s=0) {
int suffix;
int front_node,current_node;
suffix=0;
if(s==0) {
current_node=0;
front_node=MAXLINE-1;
}
else {
current_node=s;
front_node=s-1;
}
while(R[front_node]!=front_node) {
printf("%d\n",a[current_node]);
R[front_node]=R[current_node];
if(K==1) {
current_node=R[front_node];
continue;
}
for(int i=0;i<K;i++){
front_node=R[front_node];
}
current_node=R[front_node];
}
index=front_node;
values=a[front_node];
return 0;
}
int main(void) {
int a[MAXLINE],R[MAXLINE],suffix,index,values,start,i,K;
suffix=index=values=start=0;
K=2;
for(i=0;i<MAXLINE;i++) {
a[i]=i;
R[i]=i+1;
}
R[i-1]=0;
find_n(a,R,K,index,values,2);
printf("the value is %d,%d\n",index,values);
return 0;
}
試題:
void test2()
{
char string[10], str1[10];
int i;
for(i=0; i<10; i++)
{
str1[i] = 'a';
}
strcpy( string, str1 );
}
解答:對(duì)試題2,如果面試者指出字符數(shù)組str1不能在數(shù)組內(nèi)結(jié)束可以給3分;如果面試者指出strcpy(string, str1)調(diào)用使得從str1內(nèi)存起復(fù)制到string內(nèi)存起所復(fù)制的字節(jié)數(shù)具有不確定性可以給7分,在此基礎(chǔ)上指出庫(kù)函數(shù)strcpy工作方式的給10分;
str1不能在數(shù)組內(nèi)結(jié)束:因?yàn)閟tr1的存儲(chǔ)為:{a,a,a,a,a,a,a,a,a,a},沒有'\0'(字符串結(jié)束符),所以不能結(jié)束
strcpy( char *s1,char *s2)他的工作原理是,掃描s2指向的內(nèi)存,逐個(gè)字符付到s1所指向的內(nèi)存,直到碰到'\0',因?yàn)閟tr1結(jié)尾沒有'\0',所以具有不確定性,不知道他后面還會(huì)付什么東東。
正確應(yīng)如下
void test2()
{
char string[10], str1[10];
int i;
for(i=0; i<9; i++)
{
str1[i] = 'a'+i; //把a(bǔ)bcdefghi賦值給字符數(shù)組
}
str[i]='\0';//加上結(jié)束符
strcpy( string, str1 );
}
第二個(gè)code題是實(shí)現(xiàn)strcmp
int StrCmp(const char *str1, const char *str2)
做是做對(duì)了,沒有抄搞,比較亂
int StrCmp(const char *str1, const char *str2)
{
assert(str1 && srt2);
while (*str1 && *str2 && *str1 == *str2) {
str1++, str2++;
}
if (*str1 && *str2)
return (*str1-*str2);
elseif (*str1 && *str2==0)
return 1;
elseif (*str1 = = 0 && *str2)
return -1;
else
return 0;
}
int StrCmp(const char *str1, const char *str2)
{
//省略判斷空指針(自己保證)
while(*str1 && *str1++ = = *str2++);
return *str1-*str2;
}
第三個(gè)code題是實(shí)現(xiàn)子串定位
int FindSubStr(const char *MainStr, const char *SubStr)
做是做對(duì)了,沒有抄搞,比較亂
int MyStrstr(const char* MainStr, const char* SubStr)
{
const char *p;
const char *q;
const char * u = MainStr;
//assert((MainStr!=NULL)&&( SubStr!=NULL));//用斷言對(duì)輸入進(jìn)行判斷
while(*MainStr) //內(nèi)部進(jìn)行遞增
{
p = MainStr;
q = SubStr;
while(*q && *p && *p++ == *q++);
if(!*q )
{
return MainStr - u +1 ;//MainStr指向當(dāng)前起始位,u指向
}
MainStr ++;
}
return -1;
}
分析:
int arr[] = {6,7,8,9,10};
int *ptr = arr;
*(ptr++)+=123;
printf(“ %d %d ”, *ptr, *(++ptr));
輸出:8 8
過程:對(duì)于*(ptr++)+=123;先做加法6+123,然后++,指針指向7;對(duì)于printf(“ %d %d ”, *ptr, *(++ptr));從后往前執(zhí)行,指針先++,指向8,然后輸出8,緊接著再輸出8
華為全套完整試題
高級(jí)題
6、已知一個(gè)單向鏈表的頭,請(qǐng)寫出刪除其某一個(gè)結(jié)點(diǎn)的算法,要求,先找到此結(jié)點(diǎn),然后刪除。
slnodetype *Delete(slnodetype *Head,int key){}中if(Head->number==key)
{
Head=Pointer->next;
free(Pointer);
break;
}
Back = Pointer;
Pointer=Pointer->next;
if(Pointer->number==key)
{
Back->next=Pointer->next;
free(Pointer);
break;
}
void delete(Node* p)
{
if(Head = Node)
while(p)
}
有一個(gè)16位的整數(shù),每4位為一個(gè)數(shù),寫函數(shù)求他們的和。
解釋:
整數(shù)1101010110110111
和 1101+0101+1011+0111
感覺應(yīng)該不難,當(dāng)時(shí)對(duì)題理解的不是很清楚,所以寫了一個(gè)函數(shù),也不知道對(duì)不對(duì)。
疑問:
既然是16位的整數(shù),1101010110110111是2進(jìn)制的,那么函數(shù)參數(shù)怎么定義呢,請(qǐng)大蝦指教。
答案:用十進(jìn)制做參數(shù),計(jì)算時(shí)按二進(jìn)制考慮。
/* n就是16位的數(shù),函數(shù)返回它的四個(gè)部分之和 */
char SumOfQuaters(unsigned short n)
{
char c = 0;
int i = 4;
do
{
c += n & 15;
n = n >> 4;
} while (--i);
return c;
}
有1,2,....一直到n的無序數(shù)組,求排序算法,并且要求時(shí)間復(fù)雜度為O(n),空間復(fù)雜度O(1),使用交換,而且一次只能交換兩個(gè)數(shù).(華為)
#include<iostream.h>
int main()
{
int a[] = {10,6,9,5,2,8,4,7,1,3};
int len = sizeof(a) / sizeof(int);
int temp;
for(int i = 0; i < len; )
{
temp = a[a[i] - 1];
a[a[i] - 1] = a[i];
a[i] = temp;
if ( a[i] == i + 1)
i++;
}
for (int j = 0; j < len; j++)
cout<<a[j]<<",";
return 0;
}
(慧通)
1 寫出程序把一個(gè)鏈表中的接點(diǎn)順序倒排
typedef struct linknode
{
int data;
struct linknode *next;
}node;
//將一個(gè)鏈表逆置
node *reverse(node *head)
{
node *p,*q,*r;
p=head;
q=p->next;
while(q!=NULL)
{
r=q->next;
q->next=p;
p=q;
q=r;
}
head->next=NULL;
head=p;
return head;
}
2 寫出程序刪除鏈表中的所有接點(diǎn)
void del_all(node *head)
{
node *p;
while(head!=NULL)
{
p=head->next;
free(head);
head=p;
}
cout<<"釋放空間成功!"<<endl;
}
3兩個(gè)字符串,s,t;把t字符串插入到s字符串中,s字符串有足夠的空間存放t字符串
void insert(char *s, char *t, int i)
{
char *q = t;
char *p =s;
if(q == NULL)return;
while(*p!='\0')
{
p++;
}
while(*q!=0)
{
*p=*q;
p++;
q++;
}
*p = '\0';
}
分析下面的代碼:
char *a = "hello";
char *b = "hello";
if(a= =b)
printf("YES");
else
printf("NO");
這個(gè)簡(jiǎn)單的面試題目,我選輸出 no(對(duì)比的應(yīng)該是指針地址吧),可在VC是YES 在C是NO
lz的呢,是一個(gè)常量字符串。位于靜態(tài)存儲(chǔ)區(qū),它在程序生命期內(nèi)恒定不變。如果編譯器優(yōu)化的話,會(huì)有可能a和b同時(shí)指向同一個(gè)hello的。則地址相同。如果編譯器沒有優(yōu)化,那么就是兩個(gè)不同的地址,則不同謝謝!
寫一個(gè)函數(shù),功能:完成內(nèi)存之間的拷貝
memcpy source code:
270 void* memcpy( void *dst, const void *src, unsigned int len )
271 {
272 register char *d;
273 register char *s;
27
275 if (len == 0)
276 return dst;
277
278 if (is_overlap(dst, src, len, len))
279 complain3("memcpy", dst, src, len);
280
281 if ( dst > src ) {
282 d = (char *)dst + len - 1;
283 s = (char *)src + len - 1;
284 while ( len >= 4 ) {
285 *d-- = *s--;
286 *d-- = *s--;
287 *d-- = *s--;
288 *d-- = *s--;
289 len -= 4;
290 }
291 while ( len-- ) {
292 *d-- = *s--;
293 }
294 } else if ( dst < src ) {
295 d = (char *)dst;
296 s = (char *)src;
297 while ( len >= 4 ) {
298 *d++ = *s++;
299 *d++ = *s++;
300 *d++ = *s++;
301 *d++ = *s++;
302 len -= 4;
303 }
304 while ( len-- ) {
305 *d++ = *s++;
306 }
307 }
308 return dst;
309 }
公司考試這種題目主要考你編寫的代碼是否考慮到各種情況,是否安全(不會(huì)溢出)
各種情況包括:
1、參數(shù)是指針,檢查指針是否有效
2、檢查復(fù)制的源目標(biāo)和目的地是否為同一個(gè),若為同一個(gè),則直接跳出
3、讀寫權(quán)限檢查
4、安全檢查,是否會(huì)溢出
memcpy拷貝一塊內(nèi)存,內(nèi)存的大小你告訴它
strcpy是字符串拷貝,遇到'\0'結(jié)束
/* memcpy ─── 拷貝不重疊的內(nèi)存塊 */
void memcpy(void* pvTo, void* pvFrom, size_t size)
{
void* pbTo = (byte*)pvTo;
void* pbFrom = (byte*)pvFrom;
ASSERT(pvTo != NULL && pvFrom != NULL); //檢查輸入指針的有效性
ASSERT(pbTo>=pbFrom+size || pbFrom>=pbTo+size);//檢查兩個(gè)指針指向的內(nèi)存是否重疊
while(size-->0)
*pbTo++ == *pbFrom++;
return(pvTo);
}
華為面試題:怎么判斷鏈表中是否有環(huán)?
bool CircleInList(Link* pHead)
{
if(pHead = = NULL || pHead->next = = NULL)//無節(jié)點(diǎn)或只有一個(gè)節(jié)點(diǎn)并且無自環(huán)
return (false);
if(pHead->next = = pHead)//自環(huán)
return (true);
Link *pTemp1 = pHead;//step 1
Link *pTemp = pHead->next;//step 2
while(pTemp != pTemp1 && pTemp != NULL && pTemp->next != NULL)
{
pTemp1 = pTemp1->next;
pTemp = pTemp->next->next;
}
if(pTemp = = pTemp1)
return (true);
return (false);
}
兩個(gè)字符串,s,t;把t字符串插入到s字符串中,s字符串有足夠的空間存放t字符串
void insert(char *s, char *t, int i)
{
memcpy(&s[strlen(t)+i],&s[i],strlen(s)-i);
memcpy(&s[i],t,strlen(t));
s[strlen(s)+strlen(t)]='\0';
}