int fibonacci(int positon){ if(position==1||position==2){ return 1; } return fibonacci(position-1)+fibonacci(position-2);}void test(){ int result=fibonacci(5);//查看斐波那契数列中第五个数的值 printf("%d\n",result);}原文:https://www.cnblogs.com/yyslif/p/11559855.html
———————————————————————————————————————————— 如果使用顺序表作为队列的话,当处于右图状态则不能继续插入新的队尾元素,否则会因为数组越界而导致程序代码被破坏。 由此产生了由链表实现的循环队列,只有队列未满时才可以插入新的队尾元素。 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -...
原理:异或运算加密:#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define KEY ‘@‘
int main(int argc, char * argv[]){FILE *fp;FILE *coded;char codedname[40];char ch;if(argc < 2){printf("param error!\n");exit(1);}if((fp = fopen(argv[1], "rb")) == NULL){printf("error when open file %s.\n", argv[1]);exit(1);}strcpy(codedname, argv[1]);strcat(codedname, ".coded");if((coded = fopen(codedname...
#include "STC12C5A.H"
#define TIMER_RELOAD() {TL0=0x00;TH0=0xC4;}//使能T/C 初始10ms
#define MAX_TASKS 2 //任务槽最大个数.
unsigned char idata task_stack[MAX_TASKS][2];//任务堆栈. PC指针为16位,需2个字节task_stack[][0]L task_stack[][1]H。
unsigned char idata task_time[MAX_TASKS];//定时时间
unsigned char task_id=0; //当前活动任务号
void rtos_wait(unsigned char time)
{static unsigned char i;//保存...
代码如下:#include<stdio.h>
#include<stdlib.h>typedef char ElemType;
#define MAXQUEUE 100typedef struct
{ElemType *base;int front;int rear;
}cycleQueue;/////////////////////////////////
//创建一个循环队列
void initqueue(cycleQueue *q)
{q->base = (ElemType*)malloc(sizeof(cycleQueue) * MAXQUEUE);//为循环队列申请连续空间if (!q->base){exit(0);}q->front = q->rear = 0;//初始换队首队尾位置为0
}//////////...
// Zeller公式确定星期几,w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1// y是年份取最后两位,c是世纪减1,m是月份,如果m是1月和2月,则m换成13和14,d是日期,[]表示取整,w除以7之后的余数就是星期几,如果余数为0,就是星期日#include <stdio.h>int main(){int year, month, y;printf("请输入年:\n");scanf("%d", &year);if (year < 1900){printf("年必须大于1900\n");exit(-1);}y = year;int c = y;y %= 100;c /= 100;int m;printf...
队列是一种先进先出的的数据结构,我们同样可以使用数组、链表等来实现。我们可以在队列的尾部进行插入元素,在队列的头部取出元素。普通的队列由于空间利用率不高,所以我们一般都用循环队列。循环队列中最重要的的两个操作就是判断是否为空和是否已满。当head==tail时,表示队列为空。当(tail+1)%MAX_SIZE == head,表示队列已满。 我判断队满的方法:牺牲一个单元来区分对空和队满,入队时少用一个队列单元,相当于浪费一...
#include <stdio.h>/**要求:输入圆的半径,输出圆的面积和周长
*/int main(void)
{/**声明一个float类型的变量 radius 来获取半径的值;area来表示面积,perimeter来表示周长*/float radius;float area;float perimeter;printf("----输入半径以计算圆的面积和周长----\n");printf("请输入圆的半径:");scanf("%f",&radius);//计算圆的面积area = 3.1415926*(radius*radius);//计算圆的周长perimeter = 2*radius*3.1415926;printf("...
问题实现如下转换的最佳算法是什么?0010 0000 => 0000 0100
具体的转换是从MSB->LSB到LSB->MSB, 所有的Bit都必须反转,那意味着,这并不是字节顺序的交换。最佳答案注意: 下面的算法都用C实现,但应该可以迁移到其它语言(只是不那么快的时候可别找我)。可选方案内存占用少(32位int,32位机器)(来源于这里)unsigned int
reverse(register unsigned int x)
{x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));x = (((x & 0x...
前面我们学习二叉搜索树的时候发现在一些情况下其高度不是很均匀,甚至有时候会退化成一条长链,所以我们引用一些”平衡”的二叉搜索树。红黑树就是一种”平衡”的二叉搜索树,它通过在每个结点附加颜色位和路径上的一些约束条件可以保证在最坏的情况下基本动态集合操作的时间复杂度为O(nlgn).下面会总结红黑树的性质,然后分析红黑树的插入操作,并给出一份完整代码。先给出红黑树的结点定义:#define RED 1#define BLACK 0///红黑...
#include "stdio.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h"#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0#define MAXSIZE 100 /* 存储空间初始分配量 */#define MAX_TREE_SIZE 100 /* 二叉树的最大结点数 */typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */typedef int TElemType; /* 树结点的数据类型,目前暂定为整型 */typedef TElemType ...
对链表进行增删改查是最基本的操作。我在上一篇博客《C语言实现链表节点的删除》实现了删除链表中的某个节点。这里我们要来实现在某个位置插入节点。示例代码上传至https://github.com/chenyufeng1991/InsertList 。核心代码如下:Node *InsertToPosition(Node *pNode,int pos,int x){if (pos < 0 || pos > sizeList(pNode) ) {printf("%s函数执行,pos=%d非法,插入数据失败\n",__FUNCTION__,pos);return pNode;}Node *pMove;Nod...
这几天做东西用到了文件的读取简单的整理了下需要的头文件是:#include <stdio.h>1.需要打开文件:用fopen_s(FILE ** _File,const char * _Filename,const char * _Mode);也可以用fopen函数参数类型一样,fopen_s比fopen多了些安全机制,后面的函数都可以使用 函数_s,eg:fprintf_s(FILE * _File, const char * _Format, ...);2.写入数据到文件:fprintf(FILE * _File, const char * _Format, ...);第一个就是文件,后面的和printf...
// 不用库函数实现strcmp#include <stdio.h>
#include <assert.h>int my_strcmp( char const *p,char const *q )
{assert( ( *p != NULL ) && ( *q != NULL ) );while( *p == *q ){if( *p == '\0'){return 0;}p++;q++;}if( *p > *q ){return 1;}else{return -1;}}int main()
{char *p = "aebh";char *q = "addfg";printf("%d\n",my_strcmp( p,q ));return 0;
}<img src="http://img.blog.csdn.net/20150330230907026?watermark/2/t...
POS终端MAC算法-C语言实现原文:http://www.cnblogs.com/LittleTiger/p/6388813.html