金山訓(xùn)練營(yíng)入學(xué)考試題
1 編寫(xiě)函數(shù)實(shí)現(xiàn)十進(jìn)制正整數(shù)到十四進(jìn)制數(shù)的轉(zhuǎn)換,在屏幕輸出轉(zhuǎn)換結(jié)果。
說(shuō) 明:用0, 1, 2, 3,....., 8, 9, A, B, C, D表示十四進(jìn)制的基本的14個(gè)數(shù)。
例:鍵盤(pán)輸入14,屏幕輸出10。 比較好的解法:
void fun( int input)
{
if (input >= 14)
fun(input/14);
printf("%c","0123456789ABCD"[input%14]);
} 很明顯考的不用庫(kù)函數(shù),其他實(shí)現(xiàn)這里就不貼了
3 文件名:3.cpp
功 能:編程實(shí)現(xiàn)猜詞游戲
說(shuō) 明:對(duì)于單詞“hello”,程序提示輸出:?????,等待用戶(hù)輸入。
用戶(hù)輸入時(shí),若單詞包含該字母,如“l”,則程序顯示輸出“??ll?”;
若單詞不含該字母,如“a”,則程序提示用戶(hù)猜錯(cuò)。
繼續(xù)等待用戶(hù)輸入,直到用戶(hù)猜出全部字母,或輸入錯(cuò)誤次數(shù)超過(guò)最大允許出錯(cuò)次數(shù),游戲結(jié)束。
條 件:1) 單詞由程序內(nèi)定,由全小寫(xiě)字母組成
2) 提示輸出問(wèn)號(hào)數(shù)量等于單詞長(zhǎng)度
3) 最大允許出錯(cuò)次數(shù)等于單詞長(zhǎng)度
看我的實(shí)現(xiàn)
int main()


{
string str="hello",temp="?????",ss;
int num=0;
while (temp.find_first_of('?')!=-1&&num<str.size())


{
cin>>ss;
++num;
for (int i=0;i<ss.size();++i)

{
for (int j=0;j<str.size();++j)

{
if (ss.at(i)==str.at(j))

{
temp.at(j)=str.at(j);
}
}
}
cout<<temp<<endl;
}
return 0;
}topic.csdn.net\u\20080517\21\8606a5d6-9c07-4ebc-a7bb-243af402e20b4bc0.html
【某公司C++筆試題】1.編寫(xiě)函數(shù),檢查給定字符串是否整數(shù),如果是,返回其整數(shù)值(注:不允許使用某種特定的庫(kù)函數(shù))。
也算經(jīng)典的atoi問(wèn)題了吧,看我的實(shí)現(xiàn)
int my_atoi(char* str)


{
int i=0,num=0;
while (*str)

{
if (*str<='9'&&*str>='0')

{
num=num*10;
num+=(*str-'0');
}
else

{
cout<<"ileigg word\n";
return -1;
}
str++;
}

return num;
}上課的時(shí)候我曾用java寫(xiě)過(guò)一個(gè)類(lèi)似的,當(dāng)時(shí)用的兩層循環(huán)來(lái)計(jì)算,后來(lái)看了一個(gè)帖子,很慚愧。
看下面淘寶網(wǎng)一道面試題
讓我寫(xiě)出atol的實(shí)現(xiàn)代碼,我記得微軟的源碼,當(dāng)場(chǎng)寫(xiě)出來(lái)了,如下:
long __cdecl atol(
const char *nptr
)


{

int c; /**//* current char */

long total; /**//* current total */

int sign; /**//* if '-', then negative, otherwise positive */


/**//* skip whitespace */
while ( isspace((int)(unsigned char)*nptr) )
++nptr;


c = (int)(unsigned char)*nptr++; sign = c; /**//* save sign indication */
if (c == '-' || c == '+')

c = (int)(unsigned char)*nptr++; /**//* skip sign */

total = 0;


while (isdigit(c))
{

total = 10 * total + (c - '0'); /**//* accumulate digit */

c = (int)(unsigned char)*nptr++; /**//* get next char */
}

if (sign == '-')
return -total;
else

return total; /**//* return result, negated if necessary */
}

接著面試官問(wèn)我,為什么要在程序中做(int)(unsigned char)*nptr的強(qiáng)轉(zhuǎn)?我沒(méi)答出來(lái),哪位能說(shuō)說(shuō)為什么要強(qiáng)轉(zhuǎn)?。浚浚??看正解:
因?yàn)閕sspace(),isdigit()這類(lèi)函數(shù)接受一個(gè)int 參數(shù),參數(shù)的值必須是一個(gè)可以用unsigned char 表示的值或者是EOF,以下是 man 手冊(cè)的原文:

int isspace(int c);
int isdigit(int c);

These functions check whether c, which must have the value of anunsigned char or EOF,falls into a cetern charcter class according to the current locale.

所以需要用c = (int)(unsigned char)*nptr++,來(lái)確保從 *nptr++ 到 c 是進(jìn)行零擴(kuò)展而不是符號(hào)擴(kuò)展,保證 c 中存放的是一個(gè)unsigned char 所能表示的值。 還有一類(lèi)題是經(jīng)典的itoa,下面給出正解:

void my_itoa(int s, char str[])
{
int i, t;

for (i = 0, t = s; t > 0;)
{
str[i++] = t / 10 + '0';
t %= 10;
}
}

2.有兩個(gè)無(wú)序鏈表lsit1和list2,編寫(xiě)函數(shù)把list1和list2合并成一個(gè)遞增的鏈表。
這道題考的是基礎(chǔ)知識(shí),學(xué)過(guò)數(shù)據(jù)結(jié)構(gòu)都應(yīng)該能寫(xiě)出來(lái)

已折疊
struct Node


{
int value;
Node* next;
};
void creat_list(Node*& head)


{
int num=0;

while (1)

{
cin>>num;
if (num==-1)

{
break;
}
Node* p=new Node;
p->value=num;
p->next=head->next;
head->next=p;
}
}
void print_all(Node* head)


{
Node* q=head->next;
while(q)

{
cout<<q->value<<' ';
q=q->next;
}
cout<<endl;
}
void list_sort(Node* head)


{
Node* p=head,*q=NULL;
while(1)

{
p=head;
for(;p->next!=q;p=p->next)

{
if (p->value>p->next->value)

{
int temp=p->value;
p->value=p->next->value;
p->next->value=temp;
}
}
q=p;
if (head->next==q)

{
break;
}
}
}
void two_list_sort(Node* head,Node* head1,Node*& new_list)


{
list_sort(head);
list_sort(head1);

print_all(head);

print_all(head1);

Node* p=head->next;
Node* p1=head1->next;
while (p&&p1)

{
if (p->value<=p1->value)

{
Node* m=new Node;
m->value=p->value;
m->next=new_list->next;
new_list->next=m;
p=p->next;
}
else

{
Node* m=new Node;
m->value=p1->value;
m->next=new_list->next;
new_list->next=m;
p1=p1->next;
}
}
while (p)

{
Node* m=new Node;
m->value=p->value;
m->next=new_list->next;
new_list->next=m;
p=p->next;
}
while (p1)

{
Node* m=new Node;
m->value=p1->value;
m->next=new_list->next;
new_list->next=m;
p1=p1->next;
}

list_sort(new_list);
}
int main()


{
Node* head=new Node;
Node* head1=new Node;
head->next=NULL;
head->value=0;
head1->next=NULL;
head1->value=1;
creat_list(head);
creat_list(head1);
print_all(head);

print_all(head1);


Node* new_list=new Node;
new_list->next=NULL;
new_list->value=0;
two_list_sort(head,head1,new_list);
print_all(new_list);
return 0;
}topic.csdn.net\u\20081011\15\9ee842e0-9c0d-4804-8376-42abdfe80698.html
posted on 2012-06-05 09:41
憤怒的考拉 閱讀(57)
評(píng)論(0) 編輯 收藏