期末模拟题---期末复习3

 头插法建立单链表

#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;
}

用常规实现

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值