posts - 1,  comments - 0,  trackbacks - 0
金山训练营入学考试题
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 愤怒的考拉 阅读(48) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿

随笔档案

文章档案

搜索

  •  

最新评论