头插法建立单链表
#include <stdio.h>
#include <stdlib.h>struct Node //定义结构体
{char data; //数据域struct Node * next; //指针域
};/* 请在这里填写答案 */
struct Node * CreateList (struct Node * head)
{struct Node *p;char ch;scanf("%c",&ch);while(ch!='\n'){p=(struct Node *)malloc(sizeof(struct Node));p->data=ch;p->next=head;head=p;scanf("%c",&ch);}return head;
}void PrintList (struct Node * head)
{struct Node * s;if(head == NULL){printf("None");return;}for(s=head;s!=NULL;s = s->next){printf("%c ",s->data);}
}int main()
{struct Node * head = NULL;head = CreateList(head);PrintList(head);return 0;
}
字符串排序
#include <stdio.h>
#include <stdlib.h>#define MAXSIZE 20
struct wInfor
{char *word;//每个单词最大长度为10 int l;
} ;
void input(struct wInfor w[MAXSIZE] ,int n);
void wordSort(struct wInfor w[MAXSIZE] ,int n);
int main()
{int n,i;struct wInfor word[MAXSIZE];scanf("%d",&n);getchar();input(word,n);wordSort(word,n);for(i=0;i<n;i++)printf("%s ",word[i].word );return 0;}/* 请在这里填写答案 */
#include<string.h>
void input(struct wInfor w[MAXSIZE] ,int n)
{int i;for(i=0;i<n;i++){w[i].word=(char *)malloc(sizeof(char));scanf("%s",w[i].word);w[i].l=strlen(w[i].word);}
}
void wordSort(struct wInfor w[MAXSIZE] ,int n)
{int i,j;struct wInfor c;for(i=0;i<n;i++){for(j=0;j<n-i-1;j++){if(w[j].l>w[j+1].l){c=w[j];w[j]=w[j+1];w[j+1]=c;}}}
}
一定要malloc,否则输出颜色字符串的时候它只让你输出一个
函数题也可以加头文件strlen------include<string.h>
用递归实现
#include<stdio.h>int fib(int n)
{if(n==1) return 1;else if(n==2) return 2;else return fib(n-1)+2*fib(n-2);
}
int main()
{int T,n;scanf("%d",&T);for(int i=0;i<T;i++){scanf("%d",&n);printf("%d\n",fib(n));}return 0;
}
用常规实现