第五周学习笔记

关于栈:类似于后来的反而先处理

根据来自啊哈算法栈的例题,判断是否是回文字符串:

#include<stdio.h>
#include<string.h>
int main()
{
	char a[101],s[101];
	int i,l,m,next,top=0;
	gets(a);
	l=strlen (a);
	m=l/2-1;
	for(i=0;i<=m;i++)
	    s[++top]=a[i];
	if(l=a%2==0)
	    next=m+1;
	else next=m+2;
	for(i=next;i<l;i++)
	{
		if(a[i]!=s[top])
		break;
	    top--;
	}
	if(top==0)
	printf("YES");
	else printf("NO");
	return 0;
}

归并排序:

#include <stdio.h>
#define maxn 100100
int a[maxn],n,b[maxn];
void memory_sort(int s1,int e1,int s2,int e2){
	int i=s1,j=s2,k=s1;
	while(i<=e1&&j<=e2){
		if(a[i]<a[j])b[k++]=a[i++];
		else b[k++]=a[j++];
	}
	while(i<=e1)b[k++]=a[i++];
	while(j<=e2)b[k++]=a[j++];
	for(i=s1;i<=e2;i++)a[i]=b[i];
}
void merge_sort(int left,int right){
	int mid=(left+right)/2;
	if(left>=right)return;
	merge_sort(left,mid);
	merge_sort(mid+1,right);
	memory_sort(left,mid,mid+1,right);
}
int main(){
	int i;
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	merge_sort(1,n);
	for(i=1;i<=n;i++)printf("%d ",a[i]);
	return 0;
}

插入排序:将待排序的无序数列看成是一个仅含有一个元素的有序数列和一个无序数列,将无序数列中的元素逐次插入到有序数列中,从而获得最终的有序数列。

#include <stdio.h>
void insertSort(int arry[], int len)
{
    int i;
    int temp;//保存要插入的元素
    int j;//从当前要要比较插入的元素的前面一个开始
    for (i=1;i<len;i++)//第一个元素视为有序,把后面的元素一个一个的插入到前面
    {
        temp = arry[i];
        j=i-1;
        while (j>=0&&arry[j]>temp)
        {
            arry[j+1]=arry[j];//前面的元素往后面移动
            j--;
        }
        arry[j+1]=temp;//把要插入的元素,插入进对应的位置
    }
}
void print(int arry[], int len)
{ 
    int i;
    for (i=0;i<len;i++)
    {
        printf("%d ",arry[i]);
    }
}
int main()
{
	int n,i;
	scanf("%d",&n);
	int arry[n];
	for(i=0;i<n;i++)
	 scanf("%d",&arry[i]);
    insertSort(arry,n);
    print(arry,n);
    printf("\n");
    return 0;
}

再随便加一个选择排序:

#include <stdio.h>
int main()
{
	int i,j,min,temp,a[10]={0};
	for(i=0;i<10;i++)
	scanf("%d",&a[i]);
	for(i=0;i<10;i++)
	{
	 min=i;
     for(j=i+1;j<10;j++)
	 {
     if(a[min]>a[j])
      min=j;
	 }
	  temp=a[min];
	  a[min]=a[i];
	  a[i]=temp;
	}
	 for(i=0;i<10;i++)
	printf("%d ",a[i]);
}

例题如下: 

题目描述

cjf君想调查学校OI组每个同学的生日,并按照从大到小的顺序排序。但cjf君最近作业很多,没有时间,所以请你帮她排序。

输入格式

有2行,

第1行为OI组总人数n;

第2行至第n+1行分别是每人的姓名ss、出生年y、月m、日d。

输出格式

有n行,

即n个生日从大到小同学的姓名。(如果有两个同学生日相同,输入靠后的同学先输出)

输入输出样例

输入 #1

3
Yangchu 1992 4 23
Qiujingya 1993 10 13
Luowen 1991 8 1

输出 #1

Luowen
Yangchu
Qiujingya

说明/提示

数据规模

1<n<100

length(s)<20

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 20
typedef struct Person
{
	 char name[N];
	 int year;
	 int month;
	 int day;
}Person;
int i,m,n;
int main()
{
	Person temp;
	int num;
	Person birthday[101];
	scanf("%d",&num);
	for(i=0;i<num;i++)
	{
		scanf("%s",birthday[i].name );
		scanf("%d",&birthday[i].year );
		scanf("%d",&birthday[i].month );
		scanf("%d",&birthday[i].day );
	}
	if(num>1)
	{
		for(m=0;m<num-1;m++)
		{
			for(n=m+1;n<num;n++)
			{
				if(birthday[m].year >birthday[n].year ||(birthday[m].year ==birthday[n].year && birthday[m].month >birthday[n].month)||(birthday[m].year ==birthday[n].year&& birthday[m].month ==birthday[n].month&& birthday[m].day >=birthday[n].day))
				{
					temp=birthday[m];
					birthday[m]=birthday[n];
					birthday[n]=temp;
				}
			}
		}
	}
	for(i=0;i<num;i++)
	{
		printf("%s\n",birthday[i].name);
	}
}

输入若干个学生信息(包括学号、姓名和成绩),输入学号为0时输入结束,建立一个单向链表,再输入一个成绩值,将成绩大于等于该值的学生信息输出,用链表解决该问题

#include<stdio.h>
#include<stdlib.h>
struct student{
    int num;
    char name[10];
    int score;
    struct student *next;
};
int main()
{    int data;
     struct student *head,*p,*q;
     head=(struct student *)malloc(sizeof(struct student));
     head->next=NULL;    p=head;
     scanf("%d",&data);
     while(data!=0)
     {       q=(struct student *)malloc(sizeof(struct student));
             q->next=NULL;        q->num=data;
             scanf("%s",&q->name);        scanf("%d",&q->score);
             p->next=q;        p=q;        scanf("%d",&data);    }
      struct student *t;
      t=head->next;    int x;
      scanf("%d",&x);
      while(t!=NULL)
      {        if(t->score>=x)
              {            printf("%d %s %d\n",t->num,t->name,t->score);
                      }
               t=t->next;    
       }
  }

6-1 单链表结点删除 (25 分)

本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中所有存储了某给定值的结点删除。链表结点定义如下:

struct ListNode {
    int data;
    ListNode *next;
};

函数接口定义:

struct ListNode *readlist();
struct ListNode *deletem( struct ListNode *L, int m );

函数readlist从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。

函数deletem将单链表L中所有存储了m的结点删除。返回指向结果链表头结点的指针。

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>

struct ListNode {
    int data;
    struct ListNode *next;
};

struct ListNode *readlist();
struct ListNode *deletem( struct ListNode *L, int m );
void printlist( struct ListNode *L )
{
     struct ListNode *p = L;
     while (p) {
           printf("%d ", p->data);
           p = p->next;
     }
     printf("\n");
}

int main()
{
    int m;
    struct ListNode *L = readlist();
    scanf("%d", &m);
    L = deletem(L, m);
    printlist(L);

    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

10 11 10 12 10 -1
10

输出样例:

11 12 

 代码实现

struct ListNode *readlist()
{
    struct ListNode *L,*r,*pNew;
	int x; 
	L=(struct ListNode*)malloc(sizeof(struct ListNode));
	L->next=NULL;  r=L;
	scanf("%d",&x);
	while(x!=-1)
	{
        pNew=(struct ListNode*)malloc(sizeof(struct ListNode));
	    pNew->data=x;
	    r->next=pNew;
	    r=pNew;
	    scanf("%d",&x);
    }
	r->next=NULL;
	return L;
}
struct ListNode *deletem( struct ListNode *L, int m )
{
    struct ListNode *pre,*p;
	pre=L->next;
	p=L;
	while(pre!=NULL)
    {
        if(pre->data==m)
        {
            p->next=pre->next;
            free(pre);
            pre=p->next;
        }
        else
		{
        	pre=pre->next;
        	p=p->next;
		}
    }
    L=L->next;
    return L;
}

枚举:烤鸡

题目背景

猪猪 Hanke 得到了一只鸡。

题目描述

猪猪 Hanke 特别喜欢吃烤鸡(本是同畜牲,相煎何太急!)Hanke 吃鸡很特别,为什么特别呢?因为他有 10 种配料(芥末、孜然等),每种配料可以放 1 到 3 克,任意烤鸡的美味程度为所有配料质量之和。

现在, Hanke 想要知道,如果给你一个美味程度 nn ,请输出这 1010 种配料的所有搭配方案。

输入格式

一个正整数 n,表示美味程度。

输出格式

第一行,方案总数。

第二行至结束,10 个数,表示每种配料所放的质量,按字典序排列。

如果没有符合要求的方法,就只要在第一行输出一个 0。

输入输出样例

输入 #1

11

输出 #1

10
1 1 1 1 1 1 1 1 1 2 
1 1 1 1 1 1 1 1 2 1 
1 1 1 1 1 1 1 2 1 1 
1 1 1 1 1 1 2 1 1 1 
1 1 1 1 1 2 1 1 1 1 
1 1 1 1 2 1 1 1 1 1 
1 1 1 2 1 1 1 1 1 1 
1 1 2 1 1 1 1 1 1 1 
1 2 1 1 1 1 1 1 1 1 
2 1 1 1 1 1 1 1 1 1 

说明/提示

对于 100% 的数据,n≤5000。

#include<stdio.h>
int n,ans;
int f[300000][12],s[12];
void find(int x,int y){
	int i;
    if(x==10){
        if(y==n){
            for(i=0;i<10;i++) f[ans][i]=s[i];
            ++ans;
        }
        return;
    }
    for(i=1;i<=3;i++){
        s[x]=i;
        find(x+1,y+i);
    }
}
int main(){
	int i,j;
    scanf("%d",&n);
    if(n>=10&&n<=30) find(0,0);
    else ans=0;
    printf("%d\n",ans);
    for(i=0;i<ans;i++){
        for(j=0;j<10;j++){
            printf("%d",f[i][j]);
            if(j!=9) printf(" ");
        }
        printf("\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值