每周题目杂记(1)

本文探讨了链表操作中的合并算法和排序技巧,特别关注了提高代码效率和减少时间复杂度的方法,包括快速链表合并、Josephus问题和最大角度计算。通过实例演示和代码重写,分享了如何避免常见错误并提升编程速度。
摘要由CSDN通过智能技术生成

1、1400

#include<string.h>
#include<stdio.h>
int main()
{
	int n,m;
	int i,j,c=0,t=0;
	int a[2000]={0};
	scanf("%d%d",&n,&m);
	for(i=1;i<n+1;i++)
	{
		if(a[i]==0)
		{
			c++;
		}
		if(c==m)
		{
			printf("%d ",i);
			a[i]=1;
			t++;
			c=0;
		}
		
		if(t<n&&i==n)  //这一步很重要,统计已出队人数
		{
			i=0;
		}
		else if(t==n)
		{
			break;
		}
		
	}
    return 0;
}

链表做法

#include <stdio.h>
#include <stdlib.h>
 
struct Node
{
    int number;
    struct Node *next;
};
struct Node *head, *tail;
 
void create(int n)
{
    struct Node *p;
    int i;
 
    head = (struct Node *)malloc(sizeof(struct Node));
    head->number = 1;
    head->next = head;
    tail = head;
    for (i = 2; i <= n; ++i)
    {
        p = (struct Node *)malloc(sizeof(struct Node));
        p->number = i;
        p->next = head;
        tail->next = p;
        tail = p;
    }
}
 
void joseph(int n, int m)
{
    struct Node *p, *q;
    int i;
 
    p = head;
    q = tail;
    while (n--)
    {
        for (i = 0; i < m-1; ++i)
        {
            q = p;
            p = p->next;
        }
        printf("%d ", p->number);
        q->next = p->next;
        free(p);
        p = q->next;
    }
    printf("\n");
}
 
int main()
{
    int n, m;
 
    scanf("%d %d", &n, &m);
    create(n);
    joseph(n, m);
 
    return 0;
}

2、1402

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct G
{
	char s[30];
	struct G *next;
};
struct G *f(int n)
{
	struct G*he=NULL,*p1,*p2;
	int t;
	t=sizeof(struct G);
	p1=p2=(struct G*)malloc(t);
	he=p1;
	while(n--)
	{
		p2=p1;
		scanf("%s",p1->s);
		p1=(struct G*)malloc(t);
		p2->next=p1;
	}
	p2->next=NULL;
	return he;
}
void print(struct G *h,int n,int k)
{
	int i,j;
	struct G*p=h,*q,*t;
	char a[30];
	for(p=h;p->next!=NULL;p=p->next)
	{
		for(q=p;q->next!=0;q=q->next)
		{
			
			t=q->next;
			if(strcmp(p->s,t->s)>0)
			{
				strcpy(a,p->s);
				strcpy(p->s,t->s);
				strcpy(t->s,a);
			}
		}
	}
	p=h;
	for(i=0;i<k-1;i++)
	p=p->next;
	printf("%s",p->s);
}
int main()
{
	int n,k;
	scanf("%d",&n);
	struct G *head;
	head=f(n);
	scanf("%d",&k);	
	print(head,n,k);
	return 0;
}

解题过程中仍然存在目前频繁出现的问题,首先是赶鸭子上架,没有牢固掌握就上手,导致链表设置生疏应用缓慢,其次,整体的各知识点整合练习与算法练习不足,再次,这几天由于事情,投入时间少,目前需要在这一块集中练习,举一反三,记录典型程序,重写一遍,至少目前遇到的习题要全部解决,积累经验,运用经验!

3、1401

#include<stdio.h>
#include<math.h>
struct Point
{
	int x;
	int y;
};
double max_angle(struct Point A,struct Point B,struct Point C)
{
	double max=0,a,b,c,ab,bc,ac;
	ab=sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
	bc=sqrt((C.x-B.x)*(C.x-B.x)+(C.y-B.y)*(C.y-B.y));
	ac=sqrt((C.x-A.x)*(C.x-A.x)+(C.y-A.y)*(C.y-A.y));
	a=(ab*ab+ac*ac-bc*bc)/(2*ab*ac);
	b=(ab*ab+bc*bc-ac*ac)/(2*ab*bc);
	c=(bc*bc+ac*ac-ab*ab)/(2*bc*ac);
	a=acos(a);
	b=acos(b);
	c=acos(c);
	max=a;
	if(max-b<1e-8)max=b;
	if(max-c<1e-8)max=c;
	return max;
}
double getmaxangle(struct Point pts[],int n)
{
	double max,t=0;
	int i,j,k,m;
	max=0; 
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			for(k=0;k<n;k++)
			{
				t=max_angle(pts[i],pts[j],pts[k]);
				if(max-t<1e-8)
				max=t;
			}
		}
	}
	return max;
}
int main()
{
	int i,n;
	struct Point pts[50];
	scanf("%d",&n);
	for(i=0;i<n;i++)
	scanf("%d%d",&pts[i].x,&pts[i].y);
	printf("%.6lf",getmaxangle(pts,n));
	return 0;
}

速度要快!连改错共28min,还是慢!

4、链表合并(确实频繁使用函数)

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
typedef struct s
{
	int a;
	struct s*next;
}stu;
void print(stu*head)
{
	stu*p;
	p=head;
	while(p!=0)
	{
		printf("%d",p->a);
		p=p->next;
	}
}
stu*creat()
{
	stu*head=0,*p1,*p2;
	int t;
	t=sizeof(stu);
	p1=p2=(stu*)malloc(t);
	head=p1;
	scanf("%d",&p1->a);
	while(p1->a!=0)
	{
		p2->next=p1;
		p2=p1;
		p1=(stu*)malloc(t);
		scanf("%d",&p1->a);
	}
	p2->next=0;
	return(head);
}
stu*insert(stu*ah,stu*bh)
{
	stu*abh,*p1=ah;
	while(p1->next!=0)
	{
		p1=p1->next;
	}
	p1->next=bh;
	abh=ah;
	return(abh);
}
int main()
{
	stu*ah,*bh,*abh;
	ah=creat();
	bh=creat();
	abh=insert(ah,bh);
	print(abh);
	return 0;
}

5、链表合并与排序  学习练习

6、链表和

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
typedef struct s
{
	int a;
	struct s*next;
}stu;
typedef struct sa
{
	int p;
	int q;
	struct sa*next;
}st;
void print(st*head)
{
	st*pq;
	pq=head;
	while(pq!=0)
	{
		printf("%d %d\n",pq->p,pq->q);
		pq=pq->next;
	}
}
stu*creat()
{
	stu*head=0,*p1,*p2;
	int t;
	t=sizeof(stu);
	p1=p2=(stu*)malloc(t);
	head=p1;
	scanf("%d",&p1->a);
	while(p1->a!=0)
	{
		p2->next=p1;
		p2=p1;
		p1=(stu*)malloc(t);
		scanf("%d",&p1->a);
	}
	p2->next=0;
	return(head);
}
st* SUM(stu*ph,stu*qh,int n)
{
	stu*p1=ph,*p2=qh;
	st *r ,*q1,*q2;
	int t=sizeof(st);
	r=0;
	q1=q2=(st*)malloc(t);
	while(p1!=0)
	{
		p2=qh;
		while(p2!=0)
		{
			if((p1->a)+(p2->a)==n)
			{
				q1->p=p1->a;
				q1->q=p2->a;
				if(r==0)r=q1; 
				else q2->next=q1;
				q1=(st*)malloc(t);
			}
			p2=p2->next;
		}
		p1=p1->next;
	}
	q2->next=0;
	return r;
}
int main()
{
	stu*ph,*qh;
	st*sum;
	ph=creat();
	qh=creat();
	int n,i;
	scanf("%d",&n);
	sum=SUM(ph,qh,n);
	print(sum);
	return 0;
}

没有难度,主要是作为熟练度练习,这是必要的

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值