Study notes for week 5

About stack: similar to later, it is processed first

Judge whether it is a palindrome string according to the example from AHA algorithm stack:

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

Merge sort:

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

Insert sort: regard the unordered sequence to be sorted as an ordered sequence containing only one element and an unordered sequence, and insert the elements in the unordered sequence into the ordered sequence one by one, so as to obtain the final ordered sequence.

#include <stdio.h>
void insertSort(int arry[], int len)
{
    int i;
    int temp;//Save the element to insert
    int j;//Start with the previous one of the currently inserted elements to compare
    for (i=1;i<len;i++)//The first element is regarded as ordered, and the subsequent elements are inserted into the front one by one
    {
        temp = arry[i];
        j=i-1;
        while (j>=0&&arry[j]>temp)
        {
            arry[j+1]=arry[j];//The front element moves back
            j--;
        }
        arry[j+1]=temp;//Insert the element to be inserted into the corresponding position
    }
}
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;
}

Add another option to sort:

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

Examples are as follows:

Title Description

cjf Jun wants to investigate the birthdays of each student in the OI group of the school and sort them in order from big to small. But cjf Jun has a lot of homework recently and has no time, so please help her sort.

Input format

There are two lines,

The total number of people in the first behavior OI group n;

Lines 2 to n+1 are each person's name ss, year of birth y, month m and day d.

Output format

There are n lines,

That is, the names of n students whose birthdays are from big to small. (if two students have the same birthday, input the later student to output first)

Input and output samples

Enter #1

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

Output #1

Luowen
Yangchu
Qiujingya

Description / tips

Data scale

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

Input several student information (including student number, name and grade). When the student number is 0, the input ends, establish a one-way linked list, and then enter a grade value to output the student information whose grade is greater than or equal to the value. Use the linked list to solve the problem

#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 single linked list node deletion (25 points)

This problem requires two functions to store the read data as a single linked list and delete all nodes in the linked list that store a given value. The linked list node is defined as follows:

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

Function interface definition:

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

The function readlist reads a series of positive integers from the standard input, and establishes a single linked list according to the reading order. When reading − 1, it indicates the end of input, and the function should return a pointer to the single chain header node.

The function deletem deletes all nodes in the single linked list L where m is stored. Returns a pointer to the header node of the result chain.

Example of referee test procedure:

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

/* Your code will be embedded here */

Input example:

10 11 10 12 10 -1
10

Output example:

11 12 

Code implementation

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

Enumeration: roast chicken

Topic background

Pig Hanke got a chicken.

Title Description

Pig Hanke likes roast chicken very much (it's the same animal, so it's too urgent to fry each other!) Hanke eats chicken very special. Why is it special? Because it has 10 kinds of ingredients (mustard, cumin, etc.), each of which can put 1 to 3 grams. The delicious degree of any roast chicken is the sum of the quality of all ingredients.

Now, Hanke wants to know that if you are given a taste degree , nn, please output all the matching schemes of these , 1010 , ingredients.

Input format

A positive integer n, indicating the degree of delicacy.

Output format

The first line is the total number of schemes.

From the second line to the end, 10} numbers represent the quality of each ingredient, arranged in dictionary order.

If there is no method that meets the requirements, just output a {0 on the first line.

Input and output samples

Enter #1

11

Output #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 

Description / tips

For 100% data, 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;
}

Keywords: C

Added by bruceleejr on Sat, 11 Dec 2021 06:12:55 +0200