C语言
C语言学习笔记
晶凌玄夜
这个作者很懒,什么都没留下…
展开
-
malloc、calloc、realloc的区别
1.malloc1.1 函数原型:void* malloc(size_t size);1.2 功能:这个函数向内存申请一段连续可用的空间,并返回指向这段空间的指针。 ①如果开辟成功,则返回一个指向开辟好空间的指针。 ②如果开辟失败,则返回一个NULL指针,因此malloc的返回值一定要做检查。...原创 2020-01-15 21:27:23 · 123 阅读 · 0 评论 -
一个数组中只有两个数字是出现一次, 其他所有数字都出现了两次。
#include<stdio.h>#include<stdlib.h>int func(int* arr, int sz){ int i = 0; int temp=0; int pet = 0; int x=0, y=0; for (i = 0; i < sz; i++){ temp = temp^arr[i]; // 将所有的数据异或 }...原创 2019-02-05 19:53:53 · 229 阅读 · 0 评论 -
喝汽水,1瓶汽水1元,2个空瓶可以换一瓶汽水, 给20元,可以多少汽水。 编程实现。
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>int fun_1(int n){ //n为钱数 int count = 0; //可乐数 int empty = 0; //空瓶数 while (n){ count++; empty++; ...原创 2019-02-02 20:29:01 · 145 阅读 · 0 评论 -
判断一个字符串是否为另外一个字符串旋转之后的字符串。 例如:给定s1 =AABCD和s2 = BCDAA,返回1,给定s1=abcd和s2=ACBD,返回0. AABCD左旋一个字符得到ABCD
#include<stdio.h>#include<stdlib.h>#include<string.h>int judge(char* str_1, int n,char* str_2){ int i=0,temp,num=0; while (1){ temp = str_1[0]; //提取出首位 for原创 2019-01-29 21:01:09 · 124 阅读 · 0 评论 -
实现一个函数,可以左旋字符串中的k个字符。 ABCD左旋一个字符得到BCDA ABCD左旋两个字符得到CDAB
#include<stdio.h>#include<stdlib.h>#include<string.h>void left_shift(char *arr, int n, int num){ int temp; int i; while (num%n > 0){ /原创 2019-01-29 20:52:38 · 92 阅读 · 0 评论 -
输入一个整数数组,实现一个函数, 来调整该数组中数字的顺序使得数组中所有的奇数位于数组的前半部分, 所有偶数位于数组的后半部分。
#include<stdio.h>#include<stdlib.h>void swap(int* str, int size){ int temp; int i = 0; int j = 0; for (i = 0; i < size; i++){ for (j = i; j < size; j++){ if (*(str + i) %原创 2019-01-17 21:08:15 · 129 阅读 · 0 评论 -
有一个二维数组. 数组的每行从左到右是递增的,每列从上到下是递增的. 在这样的数组中查找一个数字是否存在。 时间复杂度小于O(N);
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#define row 3#define col 3void func(int arr[row][col], int num){ int i = 0; int j = col - 1; int temp = arr[i][j];...原创 2019-01-17 21:05:18 · 156 阅读 · 0 评论 -
//有一个字符数组的内容为:"student a am i", 请你将数组的内容改为"i am a student"
#include <stdio.h>#include <stdlib.h>#include <assert.h>#include <string.h>void swap(char *left, char *right){ assert(left != NULL); assert(right != NULL)原创 2019-01-09 21:59:17 · 297 阅读 · 0 评论 -
3.编程实现: 一组数据中只有一个数字出现了一次。 其他所有数字都是成对出现的。 请找出这个数字。(使用位运算)
#include<stdio.h>#include<stdlib.h>int main(){ int arr[] = { 2, 45, 6, 2, 5, 8, 8, 45, 5 }; int num = sizeof(arr) / sizeof(arr[0]); int t = arr[0]; for (int i = 1; i < num; i++){...原创 2019-01-08 22:52:41 · 300 阅读 · 0 评论 -
不使用(a + b) / 2这种方式,求两个数的平均值
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>//第一种int func_1(int x, int y){ int aver; aver = x + (y - x) / 2; return aver;}//第二种,使用位操作符int func_2(int x, int ...原创 2019-01-08 15:59:30 · 101 阅读 · 0 评论 -
模拟实现strcat
#include<stdio.h>#include<stdlib.h>#include<assert.h>char *my_strcat(char* arr1, char* arr2){ char* arr3 = arr1; assert(arr1); assert(arr2); while (*arr1){ arr1++; } whi原创 2019-02-11 21:33:31 · 120 阅读 · 0 评论 -
模拟实现strcpy
#include<stdio.h>#include<stdlib.h>char* copy(char* arr1, char* arr2){ if (*arr2 == NULL){ return NULL; } char* arr3 = arr2; while ((*arr1++ = *arr2++) != '\0'){ ; } return ar...原创 2019-02-05 22:39:38 · 102 阅读 · 0 评论 -
模拟实现strstr
strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL形式:const char * strstr ( const char * str1, const char * str2 )整体思路:start——>str1, substart——>str2address用来存储...原创 2019-03-04 18:06:17 · 176 阅读 · 0 评论 -
动态栈的实现
栈的概念及结构栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。出栈:栈的删除操作叫做出栈。出数据也在栈顶。#include<stdio.h>#include<ass...原创 2019-05-01 01:17:52 · 201 阅读 · 0 评论 -
链队列的基本操作
队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低基本结构如图代码:#include<stdio.h>#include<malloc.h>#include<assert.h>typedef int QDataType;// 链式结构:表示队列 typedef struct ...原创 2019-05-04 01:43:05 · 880 阅读 · 0 评论 -
单链表的创建与部分功能的实现
概念:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的 。实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:单向、双向带头、不带头循环、非循环下面是带头单向非循环链表的代码://头结点初始化void init(Node* link){ assert(link != NULL); link->head = N...原创 2019-04-18 20:51:45 · 113 阅读 · 0 评论 -
动态通讯录(用顺序表实现)
利用顺序表实现动态通讯录,实质就是一个动态顺序表,基本代码和顺序表大致相同,涉及到结构体的创建,初始化,插入,删除,查找等功能。#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<assert.h>#include<string.h>//定...原创 2019-04-13 21:59:42 · 1653 阅读 · 0 评论 -
模拟实现memmove
void * memmove ( void * destination, const void * source, size_t num );memmove可以避免在复制时源指向的位置与目标位置重叠的问题当有重叠时,从源位置的后方 向前方复制#include<stdio.h>#include<stdlib.h>#include<assert.h>v...原创 2019-03-26 21:50:13 · 130 阅读 · 0 评论 -
顺序表
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。顺序表一般可以分为:静态顺序表:使用定长数组存储。动态顺序表:使用动态开辟的数组存储。问题:中间/头部的插入删除,时间复杂度为O(N)增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。增容一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满...原创 2019-03-26 18:47:32 · 859 阅读 · 0 评论 -
模拟实现memcpy
结构形式 void * memcpy ( void * destination, const void * source, size_t num );代码如下#include<stdio.h>#include<stdlib.h>#include<assert.h>void* Memcpy(char* arr, const void* source, s...原创 2019-03-20 23:01:14 · 102 阅读 · 0 评论 -
模拟实现strcmp
strcmp函数是C/C++中基本的函数,它对两个字符串进行比较,然后返回比较结果,函数形式如下:int strcmp(const char* str1, const char* str2);其中str1和str2可以是字符串常量或者字符串变量,返回值为整形。返回结果如下规定:① str1小于str2,返回负值或者-1(VC返回-1); by wgene...原创 2019-03-07 22:18:44 · 194 阅读 · 0 评论 -
1.编写函数: unsigned int reverse_bit(unsigned int value); 这个函数的返回值value的二进制位模式从左到右翻转后的值。 如:在32位机器上25这个值包
第一种#include&lt;stdio.h&gt;#include&lt;stdlib.h&gt;unsigned int reverse_bit(unsigned int value){ int ret = 0; int bit = 0; int i; for (i = 0; i &lt; 32; i++) { ret = ret &lt;&lt; 1;原创 2019-01-07 18:51:12 · 118 阅读 · 0 评论 -
在屏幕上打印杨辉三角。
#include&lt;stdio.h&gt;#include&lt;stdlib.h&gt;int main(){ int triangle[10][10] = { 0 },col=0,row=0,i,j; for (col = 0; col &lt; 10; col++){ for (row = 0; row &lt; 10; row++){ triangle[row][0.原创 2019-01-02 21:10:19 · 91 阅读 · 0 评论 -
日本某地发生了一件谋杀案,警察通过排查确定杀人凶手必为4个 嫌疑犯的一个。以下为4个嫌疑犯的供词。 A说:不是我。 B说:是C。 C说:是D。 D说:C在胡说 已知3个人说了真话,1个人说
#include&lt;stdio.h&gt;#include&lt;stdlib.h&gt;//1是凶手,0不是凶手int main(){ int a = 0, b = 0, c = 0, d = 0; for (a = 0; a &lt; 2; a++) for (b = 0; b &lt; 2; b++) for (c = 0; c &lt; 2; c++)原创 2019-01-02 19:11:14 · 208 阅读 · 0 评论 -
实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定, 输入9,输出9*9口诀表,输入12,输出12*12的乘法口诀表
#define _CRT_SECURE_NO_WARNINGS#include&lt;stdio.h&gt;#include&lt;stdlib.h&gt;void mul_table(){ int i, j,t, num; scanf("%d", &amp;t); for (j = 1; j &lt;= t; j++){ for (i = 1;原创 2018-12-22 21:43:35 · 111 阅读 · 0 评论 -
敲代码(五)
编写代码模拟三次密码输入的场景。要求:最多能输入三次密码,密码正确,提示“登录成功”, 密码错误,可以重新输入,最多输入三次,三次均错,则提示退出程序。#define _CRT_SECURE_NO_WARNINGS#include&amp;amp;amp;lt;stdio.h&amp;amp;amp;gt;#include&amp;amp;amp;lt;stdlib.h&amp;amp;amp;gt;#include&a原创 2018-12-09 23:31:10 · 123 阅读 · 0 评论 -
猜字游戏
#define _CRT_SECURE_NO_WARNINGS#include&lt;stdio.h&gt;#include&lt;stdlib.h&gt;#include&lt;time.h&gt;//猜字游戏void Crossword(){ int i; srand(time(0)); //以起始时间为基准生成随机数 int j = rand() % 1原创 2018-12-05 21:24:40 · 339 阅读 · 0 评论 -
敲代码(四)
输出*号图案:#include&amp;lt;stdio.h&amp;gt;#include&amp;lt;stdlib.h&amp;gt;int main(){ int i, j, k; for (i = 0; i &amp;lt; 6; i++){ for (j = 0; j &amp;lt;= i; j++){ printf(&quot;*&am原创 2018-12-02 14:56:26 · 114 阅读 · 0 评论 -
整数1到n之间一共出现多少次9(对1到100的延伸)
#define _CRT_SECURE_NO_WARNINGS#include&amp;lt;stdio.h&amp;gt;#include&amp;lt;stdlib.h&amp;gt;int main(){ int i, j=0, n, m,t; scanf(&quot;%d&quot;, &amp;amp;t); for (i = 1; i &am原创 2018-11-28 17:02:46 · 227 阅读 · 0 评论 -
敲代码(三)
交换两个数组的内容:#include&amp;lt;stdio.h&amp;gt;#include&amp;lt;stdlib.h&amp;gt;int main(){ int a[5] = { 1, 2, 3, 4, 5 }; int b[5] = { 6, 7, 8, 9, 0 }; int i, t; printf(&quot;原数组:\n&quot;); for (i原创 2018-11-27 17:52:21 · 190 阅读 · 0 评论 -
敲代码(二)
交换两个变量:#include&amp;amp;amp;lt;stdio.h&amp;amp;amp;gt;#include&amp;amp;amp;lt;stdlib.h&amp;amp;amp;gt;int main(){ int a=8, b=6, c; /*第一种用中间变量 c = a; a = b; b = c;*/ /*第二种不用中间变量 a = a^b; //a的二进制1000,b的原创 2018-11-26 20:41:18 · 133 阅读 · 0 评论 -
敲代码(一)
输出100到200的素数:#include&amp;lt;stdio.h&amp;gt;#include&amp;lt;stdlib.h&amp;gt;int main(){ int a, b,t; for (a =100; a &amp;lt; 201; a++) { for (b = 2; b &amp;lt;= a-1 ; b++) if (a%b ==0)brea原创 2018-11-24 21:05:43 · 228 阅读 · 0 评论 -
实现一个函数,判断一个数是不是素数
#define _CRT_SECURE_NO_WARNINGS#include&lt;stdio.h&gt;#include&lt;stdlib.h&gt;int Is_prime(int n){ int t; for (t = 2; t &lt; n; t++){ if (n%t == 0){ return(0); } else{ return(1); }...原创 2018-12-22 22:31:38 · 225 阅读 · 0 评论 -
实现一个函数判断year是不是闰年
#define _CRT_SECURE_NO_WARNINGS#include&lt;stdio.h&gt;#include&lt;stdlib.h&gt;int Is_leapyear(){ int year; scanf("%d", &amp;year); if (year % 4 == 0 &amp;&amp; year % 100原创 2018-12-23 22:50:07 · 1257 阅读 · 0 评论 -
5位运动员参加了10米台跳水比赛,有人让他们预测比赛结果 A选手说:B第二,我第三; B选手说:我第二,E第四; C选手说:我第一,D第二; D选手说:C最后,我第三; E选手说:我第四,A
#include&lt;stdio.h&gt;#include&lt;stdlib.h&gt;int main(){ int a=1, b=1, c=1, d=1, e=1,x; for (a = 1; a &lt; 6; a++) for (b = 1; b &lt; 6; b++) for (c = 1; c &lt; 6; c++) for (d = 1; d &原创 2019-01-01 22:50:27 · 153 阅读 · 0 评论 -
递归和非递归分别实现strlen
递归:#include&lt;stdio.h&gt;#include&lt;stdlib.h&gt;int string_num(char *string){ int num = 0; if ('\0' != *(string++)){ num=1+string_num(string); } return num;}int main(){ char *a="qwer&quo原创 2018-12-31 22:07:42 · 73 阅读 · 0 评论 -
编写一个函数 reverse_string(char * string)(递归实现) 实现:将参数字符串中的字符反向排列。
#include&lt;stdio.h&gt;#include&lt;stdlib.h&gt;void reverse_string(char* string){ if ('\0' != *(++string)){ reverse_string(string); } printf("%c", *(string - 1));}int main(){ char *a =&a原创 2018-12-31 21:25:41 · 478 阅读 · 0 评论 -
写一个递归函数DigitSum(n),输入一个非负整数,返回组成它的数字之和, 例如,调用DigitSum(1729),则应该返回1+7+2+9,它的和是19
在这里插入代码片原创 2018-12-31 19:03:25 · 87 阅读 · 0 评论 -
递归方式实现打印一个整数的每一位
#define _CRT_SECURE_NO_WARNINGS#include&amp;amp;lt;stdio.h&amp;amp;gt;#include&amp;amp;lt;stdlib.h&amp;amp;gt;void DigitSum(int n){ int t; if (n &amp;amp;lt; 10){ t = n; printf(&amp;quot;%d&am原创 2018-12-31 18:18:27 · 108 阅读 · 0 评论 -
编写一个函数实现n^k,使用递归实现
#define _CRT_SECURE_NO_WARNINGS#include&lt;stdio.h&gt;#include&lt;stdlib.h&gt;int func(int n,int k ){ int t,i; if (k == 1){ t = n; }else{ t = func(n, k - 1)*n; } return(t);}int main(){ ...原创 2018-12-28 13:35:10 · 192 阅读 · 0 评论