
C学习
猿来如此~
心中所想,便是远方
展开
-
星际密码
星际密码题目描述:将一个1110的矩阵,求出这个矩阵的n次方,然后取得到的矩阵的左上角的数字,如果小于4位的话,就前面补0,补到4位,超过四位的话就输出最后的四位解题方法:本题如果去分析这个矩阵的规律的话,就会发现这个数是斐波那契数列的变形,我们平常的斐波那契数列都是用递归的方法写的,但是由于本题的用例的数字过大,产生的数据冗余比较大,因此这里要使用的是数组的方法,将每个斐波那契数列放到数组中...原创 2019-12-04 23:34:05 · 561 阅读 · 0 评论 -
C语言程序的标识符、大端小端、
首先标识符:由字母、数字、下划线组成,开头必须是字母或者下划线,但是在C语言的标识符中下划线只能是由编译系统专用的,所以在C语言中,标识符不用下划线开头。大端小端:大端:低位存在高地址,高位存在低地址小端:低位存在低地址,高位存在高地址...原创 2019-07-29 13:53:08 · 237 阅读 · 0 评论 -
猴子摘桃
#include <stdio.h>#include <stdlib.h>int main(){ int day, x1, x2 = 1;//定义天数、最后一天剩下的和倒数第二天剩下的,以此来递推 day = 9; while (day > 0) { x1 = (x2 + 1) * 2;//每一天的数量等于前一天+1然后整体*2个 x2 = x...原创 2019-07-18 17:28:38 · 271 阅读 · 0 评论 -
计算某一天是一年的第几天
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>int leap(int a){ if (a % 4 == 0 && a % 100 != 0 || a % 400 == 0) { return 1; } else return 0;}int...原创 2019-07-18 17:27:29 · 886 阅读 · 1 评论 -
用数组实现循环队列
#include "dequeue.h"//用数组写循环队列void dequeueInit(Dequeue * qu)//初始化循环队列{ qu->_head = qu->_tail = qu->_data;//初始位置,队列为空,队首==队尾==数组的首位置 qu->_size = 0;//此时队列的大小为0}void dequeueEmpty(D...原创 2019-07-07 14:38:01 · 774 阅读 · 0 评论 -
用无头的单链表实现队列
#ifndef QUEUE_H_#define QUEUE_H_#include <stdio.h>#include <stdlib.h>typedef int QUDataType;typedef struct QueueNode{ struct QueueNode* _next; QUDataType _data;}QueueNode;type...原创 2019-07-11 16:42:33 · 259 阅读 · 0 评论 -
单向无头单链表的一些特殊的问题及其解决方法
//判断链表里面是否有环int SListHasCycle(SList* plist){ SListNode * fast = plist->_head; SListNode * slow = plist->_head; while (slow && fast && fast->_next) { slow = slow->_n...原创 2019-06-29 22:20:23 · 194 阅读 · 0 评论 -
双向循环带头结点的基本操作
#ifndef _LIST_H_#define _LIST_H_#include <stdio.h>#include <stdlib.h>#include <string.h>typedef int LTDataType;typedef struct ListNode{ LTDataType _data; struct ListNode* ...原创 2019-06-29 22:18:51 · 107 阅读 · 0 评论 -
回溯算法
一般的回溯算法通常用来检验我们的递归的能力,看递归强不强,就看你的回溯算法玩的怎么样基本思想:回溯算法主要就是通过访问一层,然后在这一层里面访问,如果找到你想要的就结束,如果没有找到你想要的,就返回上一级,从上一级的下一个选项去继续找,这样写下来特别想二叉树的遍历一样,学过数据结构的都知道二叉树的遍历分为三种:前序,中序,后序。自己觉得和回溯算法有点相似。代码后期补给大家...原创 2019-07-10 10:39:12 · 134 阅读 · 0 评论 -
暴力查找
#include <stdio.h>#include <stdlib.h>#include <string.h>int BF(int *x, int *y, int m, int n)//暴力查找{ int i, j; for (j = 0; j <= n - m; ++j) //首先遍历一遍文本串 { for (i = 0; i &l...原创 2019-07-10 09:37:40 · 337 阅读 · 0 评论 -
C语言中函数传参--传指针(地址)、结构体的定义
//这里的参数是指针的原因是:需要改变的是主函数里面的a,b的值,只有把地址传过来之后才可以真正//的改变这里的值,因为这里的swap函数需要在空间里面寻找a,b的位置,从而达到修改的结果//就相当于如果你想要在电脑上修改你的成绩,你不仅要修改一个表面的成绩,而且要在电脑上面把你的//卷子的成绩修改了,这样才能真正的改变你的成绩。//swap(int *x, int *y)//{// ...原创 2019-06-21 15:26:13 · 2090 阅读 · 0 评论 -
C语言---文件操作
文件操作:fopen:open a file(返回值:FILE *fopen)(const char *,打开方式)fclose:close a filefgetc(读一个字符)、fputc(写一个字符):fgets(把一个文件里面的字符的前n个给另外一个文件)、fputs(写一个 字符串到屏幕上):rewind:把文件的指针附到文件的开头fscanf:比scanf多一个F...原创 2019-06-16 18:23:59 · 175 阅读 · 0 评论 -
结构体的定义、赋值相关
//#include <stdio.h>//#include <stdlib.h>//#include <string.h>//struct MyStruct//{// char a[20];//前二十个位置,因为char类型的数据占一个字节,任意数都是一的整数倍// int n;//因为20是4的整数倍,所以直接从20 的位置开始存4个字节// ...原创 2019-06-09 22:29:24 · 2013 阅读 · 0 评论 -
C语言指针概念的一些见解
一级的指针:首先说一下指针的概念:拿int p来说,就是定义一个指针,p指向一段内存空间,p的值为这块内存空间的地址,这块空间里面存的是int类型的空间,也就是从地址的位置开始的四个字节的内存上会存放int 类型的数据,而p的值永远是地址,并且这个地址不会改变,使用p就可以访问这块内存空间上面存放的值.(自己的理解,可能不全对,初学望理解!)我们平常使用的int * 、char * 等等都算是...原创 2019-05-29 20:14:03 · 147 阅读 · 0 评论 -
C语言 strncpy \strncat\strncmp 的模拟实现
#include <stdio.h>#include <stdlib.h>#include <string.h>//strncpy:从str2中复制前n个字符串到str1void my_cpy(char * str1, const char * str2, int n){ int i; for (i = 0; i < n; ++i){ st...原创 2019-05-31 17:45:24 · 131 阅读 · 0 评论 -
c语言中栈的相关:入栈、出栈、销毁栈、打印栈、初始化栈
#ifndef STACK_H_#define STACK_H_#include <stdio.h>#include <stdlib.h>// 支持动态增长的栈typedef int STDataType;typedef struct StackNode{ struct StackNode * _next; STDataType _data;}Sta...原创 2019-07-12 16:02:28 · 392 阅读 · 0 评论 -
找出字符串里面第一个只出现一次的字符,时间复杂度为O(n)
char first_single(const char *str)//查找字符串中第一个只出现一次的字符{ int asc[255] = { 0 };//char类型的范围一共0-255 int i; for (i = 0; str[i] != '\0'; i++) { asc[str[i]]++;//统计各个字符出现的次数,出现一次就++一次 } for (i = 0; st...原创 2019-07-19 12:26:39 · 1318 阅读 · 0 评论 -
二叉树的七种遍历(三种递归,三种非递归)
二叉树的头文件…不包含里面所用的栈和队列的头文件在非递归里面会用到,非递归只阐述思想,不写代码,递归代码如下:#ifndef _BTREE_H_#define _BTREE_H_typedef char BTDataType;typedef struct BinaryTreeNode{ BTDataType _data; struct BinaryTreeNode* lchi...原创 2019-08-21 14:45:02 · 247 阅读 · 0 评论 -
排序---快速排序(非递归)
非递归算法的可以使用栈和队列来实现,两者的区别就在于相反的进出,除此之外都一样//void QuickSortNonR(int *src, int n)快速排序的非递归算法//{// int start = 0, end = n - 1;// Queue qu;// QueuePush(&qu, start);// QueuePush(&qu, end);//// ...原创 2019-08-24 16:01:57 · 258 阅读 · 0 评论 -
排序---快速排序(递归)
快速排序的递归写法,总共写了四种方法:两个双指针法、挖坑法、hoare法快速排序的时间复杂度为O(nlogn),表现为不稳定的算法快速排序的数组在交换或者覆盖的过程中,相同的数字可能会出现位置的变换//快排相当于二叉树的前序遍历void swapArgs(int *pa, int *pb){ int tmp; tmp = *pa; *pa = *pb; *pb = tmp;}...原创 2019-08-24 15:59:32 · 304 阅读 · 0 评论 -
排序---归并排序
归并排序要借助一个临时的空间(相当于二叉树的后序),归并排序比较特殊,它的算法的时间复杂度虽然是O(nlogn),但是它是稳定的,在分开在合并的过程中各个数字的位置不会发生其他变化void dealMergeSort(int *src, int *tmp, int start, int end)//里面自己的函数实现{ if (start >= end) { return; }...原创 2019-08-24 15:55:30 · 124 阅读 · 0 评论 -
排序---希尔排序
希尔排序可以说是插入排序的优化,算法的时间复杂度为O(nlogn),算法不稳定在数字比较多的时候,希尔排序在不同的插入排序过程中,相同的元素可能在各自的插入排序中移动void ShelSort(int *src, int n){ int i, j, k; int gap, tmp; for (gap = n / 2; gap; gap /= 2) { for (k = 0; k...原创 2019-08-24 15:52:32 · 255 阅读 · 0 评论 -
排序---直接插入排序
直接插入排序的算法的时间复杂度为O(n^2),算法比较稳定算法稳定的原因:直接插入排序是将每一个数字进行插入排序,不会出现问题void InsertSort(int *src, int n)//直接插入排序(数组足够小时最优,数组越有序插排越快)(稳定){ int i, j; int tmp; for (i = 1; i < n; i++) { tmp = src[i];...原创 2019-08-24 15:47:45 · 175 阅读 · 0 评论 -
杨辉三角
#include <stdio.h>#include <stdlib.h>int main(){ int i, j, a[11][11]; for (i = 1; i < 11; i++) { a[i][i] = 1; a[i][1] = 1; } for (i = 3; i < 11; i++) for (j = 2; j <...原创 2019-08-09 19:30:05 · 158 阅读 · 0 评论 -
约瑟夫环
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#define N 9#define OVERFLOW 0#define OK 1int keyW[N] = { 4, 7, 5, 9, 3, 2, 6, 1, 8 };typedef struct LNode{ int...原创 2019-08-07 13:51:25 · 99 阅读 · 1 评论 -
使用指针实现整数排序
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>swap(int *p1, int *p2){ int tmp; tmp = *p1; *p1 = *p2; *p2 = tmp;}exchange(int *pt1, int *pt2, int *pt3){ i...原创 2019-08-05 20:27:54 · 1927 阅读 · 0 评论 -
删除首尾的空格字符、中间留一个、字符顺序不变
#include <stdio.h>#include <stdlib.h>#include <string.h>char * strim(char *str)//去除首尾的空格{ char *end, *sp, *ep; int len; sp = str; end = str + strlen(str) - 1; ep = end; w...原创 2019-08-04 17:25:15 · 180 阅读 · 0 评论 -
爱因斯坦阶梯问题
著名的爱因斯坦阶梯问题:有一条长长的阶梯。如果你每步2阶,最后剩1阶;每步3阶,剩2阶…只有每步7阶才能走完,问阶梯至少多少阶?#include <stdio.h>#include <stdlib.h>int main(){ int i; for (i = 100; i < 1000; i++) { if (i % 2 == 1 &&...原创 2019-07-22 12:44:28 · 872 阅读 · 0 评论 -
左值的理解
int a[4] = { 0 }; a++;地址不能作为左值,左值要求的是可以存储结果值的地点,而地址则相当于刚刚把你带到房间的门口,并不能把你带到房间里面来存值。这个是我自己的理解...原创 2019-07-22 12:03:22 · 785 阅读 · 1 评论 -
模拟比赛打分
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>//模拟比赛打分int main(){ int i, j = 1, n; float a[100], b[100], sum = 0; printf("\n输入选手数量:\n"); scanf("%d", &n)...原创 2019-07-20 11:13:45 · 867 阅读 · 0 评论 -
模拟实现strcpy、strcat、strstr、strcmp、memcpy、memmove、strchr
1.实现strcpy2.实现strcat3.实现strstr4.实现strchr5.实现strcmp6.实现memcpy7.实现memmove#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <string.h>#include <...原创 2019-06-05 13:27:33 · 158 阅读 · 0 评论 -
C进阶 部分内存 指针数组 数组指针
#include <stdio.h>#include <stdlib.h>//内存#if 0int main(){ char a= - 128; char b = -127; char c = -126; printf("%d %d %d\n", a, b, c); printf("%u %u %u\n", a, b, c); system("paus...原创 2019-05-18 14:18:51 · 107 阅读 · 0 评论 -
C语言进阶练习代码一
#include <stdio.h>#include <stdlib.h>#if 0int main(){ char ch = 'w'; //定义一个字符串 char *pc = &ch; //定义一个指针用来指向字符串的地址 *pc = 'w'; //把字符串赋给一个指针 system("pause"); return...原创 2019-05-17 23:06:08 · 433 阅读 · 0 评论 -
数组部分
#include <stdio.h>#include <stdlib.h>int main(){//数组的创建//int arr[10];//下面的方式不行,方括号里面必须存放常量值//int count = 10;//int arr2[count];//下面的代码表示数组的初始化有很多方式//char arr3[10];//float arr4[1];...原创 2019-04-13 17:26:38 · 198 阅读 · 0 评论 -
函数递归部分
1.递归和非递归分别实现求第n个斐波那契数非递归:#include <stdio.h>#include <stdlib.h>int fib(int n){ int an, an_1 = 1, an_2 = 1; int i; if (n < 2){ return 1; } for (i = 2; i < n; ++i){ an = an...原创 2019-04-12 22:02:14 · 323 阅读 · 0 评论 -
函数的嵌套,链式访问,递归调用的练习
函数的嵌套:把一个函数调用在另外一个函数里面使用#include <stdio.h>#include <stdlib.h>void new_line(){ printf("hehe\n");}void three_line(){ int i = 0; for (i = 0; i < 3; ++i){ new_line(); }}int ma...原创 2019-04-08 20:08:00 · 278 阅读 · 0 评论 -
扫雷小程序
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <time.h>int Menu(){ printf("---------------------\n"); printf("1.开始游戏\n"); printf("0.结束游戏\n"); pr...原创 2019-04-08 19:44:56 · 901 阅读 · 0 评论 -
C语言函数的练习
1.实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定,输入9,输出99口诀表,输入12,输出1212的乘法口诀表。#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>void print(int n){ int i; int j; for (i = 1; i <...原创 2019-04-06 22:58:49 · 1590 阅读 · 0 评论 -
C语言函数部分的练习
2.写代码可以在整型有序数组中查找想要的数字,找到了返回下标,找不到返回-1.(折半查找)#include <stdio.h>#include <stdlib.h>int findArgs(int a[], int n, int f){ int i; for (i = 0; i < n; i++) { if (a[i] == f) { ...原创 2019-04-01 16:35:10 · 162 阅读 · 0 评论 -
三子棋
三子棋的练习:#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>int Menu(){ printf("***********************************\n"); printf(" 1.开始游戏 \n"); ...原创 2019-04-05 17:53:12 · 90 阅读 · 0 评论