
C/C++笔试题
KanoRan
这个作者很懒,什么都没留下…
展开
-
C语言笔试题(3)——查找子串出现的次数
#include #include int count_str(char *str, char *s){ char *s1, *s2; int count = 0; while(*str != '\0') { s1 = str; s2 = s;原创 2012-03-24 21:48:53 · 1247 阅读 · 1 评论 -
C语言笔试题(15)——atoi函数实现
功 能: 把字符串转换成整型数. 原型: int atoi(const char *nptr); 函数说明: 参数nptr字符串,如果第一个非空格字符不存在或者不是数字也不是正负号则返回零,否则开始做类型转换,#include #include int atoi(const char *str){ int num = 0; int sign = 0;原创 2012-09-07 16:29:53 · 1091 阅读 · 0 评论 -
C语言笔试题(12)——猴子吃桃问题
猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少?分析:到第九天吃完桃子后,还剩下一个桃子,假设第一天摘下x1个,吃完后剩下x2个即第二天的桃子数量,x1与x2符合 x1 - ((x1 / 2) + 1) = x原创 2012-09-05 20:17:10 · 1954 阅读 · 0 评论 -
C语言笔试题(17)——三种常见的排序
1、直接插入排序#include #include void insert_sort(int a[], int n){ int i, j; for (i = 2; i < n; i++) //第二个纪录开始插入n - 1趟 { a[0] = a[i]; //设置监视哨 j = i - 1;原创 2012-09-09 11:44:08 · 1232 阅读 · 0 评论 -
C语言笔试题(16)——二叉树基本算法
#include #include struct node { char data; struct node *lchild, *rchild;};//根据前序遍历创建二叉树('.'代表为空的结点)//输入124...35...验证 struct node *create_tree(void) { struct node *root; c原创 2012-09-09 09:50:48 · 1489 阅读 · 0 评论 -
C语言笔试题(14)——strcpy函数实现
原型声明:extern char *strcpy(char *dest,const char *src); 头文件:string.h 功能:把从src地址开始且含有NULL结束符的字符串赋值到以dest开始的地址空间 说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。 返回指向dest的指针#inclu原创 2012-09-05 21:42:00 · 1144 阅读 · 0 评论 -
C语言笔试题(9)——strcpy函数与strlen函数
1、关于strcpy的找错题:void test1(void){ char string[10]; char* str1 = "0123456789"; strcpy( string, str1 );} 答:字符串str1需要11个字节才能存放下(包括末尾的’\0’),而string只有10个字节的空间,strcpy会导致数组越界; void test2()转载 2012-03-27 22:55:11 · 1266 阅读 · 0 评论 -
C语言笔试题(19)——判断字符串回文
#include #include int str_test(char *str){ assert(str != NULL); char *p_top = str; char *p_end; while (*++str != '\0') ; p_end = --str; while (p_top <= p_end)原创 2012-09-10 15:49:48 · 1930 阅读 · 0 评论 -
C语言笔试题(18)——strcat函数实现
原型 extern char *strcat(char *dest,char *src);用法 #include 在C++中,则存在于头文件中。功能 把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'。说明 src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串, 返回指向d原创 2012-09-09 22:06:24 · 1567 阅读 · 0 评论 -
C语言笔试题(13)——报数退出
有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位?分析:可以构造一个数组进行操作,声明一个变量来计数,将报到3的置0, 然后最后将数组中不为0的元素输出即为剩下的;#include #include int main(int argc, const char *argv[]){ int i, n原创 2012-09-05 20:33:04 · 2691 阅读 · 0 评论 -
C语言笔试题(11)——关于与零值比较的
分别给出BOOL,int,float,指针变量 与“零值”比较的 if 语句(假设变量名为var) BOOL型变量: if(!var) int型变量: if(var==0) float型变量: const float EPSINON = 0.00001;转载 2012-04-05 21:55:42 · 767 阅读 · 2 评论 -
C语言笔试题(2)
有一个数组a[1000]存放0--1000;要求每隔二个数删掉一个数,到末尾时循环至开头继续进行,求最后一个被删掉的数的原始下标位置。 以7个数为例: {0,1,2,3,4,5,6,7} 0-->1-->2(删除)-->3-->4-->5(删除)-->6-->7-->0(删除),如 此循环直到最后一个数被删除。转载 2011-10-22 10:16:38 · 954 阅读 · 1 评论 -
C语言笔试题(5)——查找第一个匹配子串位置
#include int find_str(char *str, char *s){ int str_len = strlen(str); int s_len = strlen(s); char *s1, *s2; int val = 0; while(*str != '\0') { s1 = str; s2 = s; while(*s1 == *s2 && *s原创 2012-03-24 22:32:04 · 1033 阅读 · 0 评论 -
C语言笔试题(1)——将字符串对调显示
#include #include #include #include void reverse(char *str){ char len = 0; char *p_top, *p_end; char temp; assert(str != NULL); len = strlen(str);原创 2011-10-22 09:50:19 · 1972 阅读 · 0 评论 -
C语言笔试题(8)——链表逆序
#include #include struct node{ int num; struct node *next;};struct node *create_list(void){ struct node *head; head = (struct node *)malloc(sizeof(struct node)); head->next = NULL; re原创 2012-03-27 22:13:02 · 883 阅读 · 0 评论 -
C语言笔试题(6)——strcmp函数的实现
原型:extern int strcmp(const char *s1,const char * s2); 用法:#include 功能:比较字符串s1和s2。 一般形式:strcmp(字符串1,字符串2) 说明: 当s1s2时,返回值>0 即:两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇'\0'为止。#inclu原创 2012-03-25 16:47:12 · 900 阅读 · 0 评论 -
C语言笔试题(7)——实现字符串的翻转
实现字符串翻转有两种方法可以使用:1、传参只有一个,即待翻转的字符串;#include char *reverse_string(char *string){ char *new_string = string; char temp; char *s1 = string; while(*new_string++) ;原创 2012-03-25 21:37:45 · 878 阅读 · 0 评论 -
C语言笔试题(10)——static的作用
在C语言中,关键字static有三个明显的作用:1)在函数体,一个被声明为静态的变量在这一函数被调用过程中维持其值不变。2) 在模块内(但在函数体外),一个被声明为静态的变量可以被模块内所用函数访问,但不能被模块外其它函数访问。它是一个本地的全局变量。3) 在模块内,一个被声明为静态的函数只可被这一模块内的其它函数调用。那就是,这个函数被限制在声明它的模块的本地范围内使用。简单的来转载 2012-03-28 22:47:42 · 837 阅读 · 0 评论 -
C语言笔试题(4)——二分法查找
#include int bfind(int *a, int len, int num){ int min = 0; int max = len - 1; int mid; while(min <= max) { mid = (min + max) / 2; if (a[mid] > num)原创 2012-03-24 21:58:09 · 984 阅读 · 0 评论