金山訓練營入學考試題
1 編寫函數實現十進制正整數到十四進制數的轉換,在屏幕輸出轉換結果。
說 明:用0, 1, 2, 3,....., 8, 9, A, B, C, D表示十四進制的基本的14個數。
例:鍵盤輸入14,屏幕輸出10。 比較好的解法:
void fun( int input)
{
if (input >= 14)
fun(input/14);
printf("%c","0123456789ABCD"[input%14]);
} 很明顯考的不用庫函數,其他實現這里就不貼了
3 文件名:3.cpp
功 能:編程實現猜詞游戲
說 明:對于單詞“hello”,程序提示輸出:?????,等待用戶輸入。
用戶輸入時,若單詞包含該字母,如“l”,則程序顯示輸出“??ll?”;
若單詞不含該字母,如“a”,則程序提示用戶猜錯。
繼續等待用戶輸入,直到用戶猜出全部字母,或輸入錯誤次數超過最大允許出錯次數,游戲結束。
條 件:1) 單詞由程序內定,由全小寫字母組成
2) 提示輸出問號數量等于單詞長度
3) 最大允許出錯次數等于單詞長度
看我的實現
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.編寫函數,檢查給定字符串是否整數,如果是,返回其整數值(注:不允許使用某種特定的庫函數)。
也算經典的atoi問題了吧,看我的實現
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;
}上課的時候我曾用java寫過一個類似的,當時用的兩層循環來計算,后來看了一個帖子,很慚愧。
看下面淘寶網一道面試題
讓我寫出atol的實現代碼,我記得微軟的源碼,當場寫出來了,如下:
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 */
}

接著面試官問我,為什么要在程序中做(int)(unsigned char)*nptr的強轉?我沒答出來,哪位能說說為什么要強轉啊????看正解:
因為isspace(),isdigit()這類函數接受一個int 參數,參數的值必須是一個可以用unsigned char 表示的值或者是EOF,以下是 man 手冊的原文:

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++,來確保從 *nptr++ 到 c 是進行零擴展而不是符號擴展,保證 c 中存放的是一個unsigned char 所能表示的值。 還有一類題是經典的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.有兩個無序鏈表lsit1和list2,編寫函數把list1和list2合并成一個遞增的鏈表。
這道題考的是基礎知識,學過數據結構都應該能寫出來

已折疊
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
憤怒的考拉 閱讀(55)
評論(0) 編輯 收藏