acm学习笔记

1.生成一个1到5的随机数

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
    srand(time(0));         //srand()初始化随机数种子在stdlib.h里,time()在time.h
    printf("%d\n",rand()%5+1);
    return 0;
}

2.零起点学算法100——夹角有多大II

在一个平面内有两个点,求两个点分别和原点的连线的夹角的大小。

#include<stdio.h>
#include<math.h>
#define pi 3.1415926
int main()
{
	int n;
	double x1,x2,y1,y2,a,b,c;
	scanf("%d",&n);
	getchar();
	while(n--)
	{
		scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
		a=sqrt(x1*x1+y1*y1);
		b=sqrt(x2*x2+y2*y2);
		c=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
		//printf("%lf%lf%lf\n",a,b,c);
		printf("%0.2lf\n",acos((a*a+b*b-c*c)/(2*a*b))*180/pi);
	}
	return 0;
}

3.迭代法求平方根

用迭代法求平方根。

求平方根的迭代公式为: X[n+1]=1/2(X[n]+a/X[n])

要求前后两次求出的得差的绝对值少于0.00001。

输出保留3位小数

#include <stdio.h>
#include<math.h>
int main()
{
	float x;
	float a,b;
	scanf("%f",&x);
	b = 1.0;
	while(fabs(b-a) > 0.00001)
	{
	a = b;
	b = (a+x/a)/2;
	}
	printf("%0.3f\n",b);
	return 0;
}

4.已有a、b两个链表,每个链表中的结点包括学号、成绩。要求把两个链表合并,按学号升序排列。

#include <stdio.h>
#include <stdlib.h>
struct Student{
    int num;
    float score;
    struct Student *next;
};
void input(struct Student *stu, int n); 
struct Student *merge(struct Student *stu1,struct Student *stu2, int m);
void print(struct Student *stu); 
int main()
{
	int n,m;
    struct Student a[100], b[100];
	scanf("%d %d",&n,&m);
    input(a, n);
    input(b, m);      
	print(merge(a,b,n));
    return 0;
}
//输入函数
void input(struct Student *stu, int n)
{
    int i;
    struct Student *p;
    for (p=stu, i=0; p<stu+n; p++, i++){
        scanf("%d %f", &p->num, &p->score);
        if(i==n-1) p->next=NULL;
		else p->next=&stu[i+1];
    }
}
//合并函数
struct Student *merge(struct Student *stu1,struct Student *stu2, int m)
{
	struct Student *p, *q, temp;;
    stu1[m-1].next=stu2;
	for (p=stu1; p!=NULL; p=p->next)
        for (q=p->next; q!=NULL; q=q->next)
            if (p->num>q->num){
                temp.num=p->num;
                p->num=q->num;
                q->num=temp.num;
                temp.score=p->score;
                p->score=q->score;
                q->score=temp.score;
            }
	return stu1;
}
//输出函数
void print(struct Student *stu)
{
    struct Student *p;
    for (p=stu; p!=NULL; p=p->next)
        printf("%d %.0f\n", p->num, p->score);
}

5.零起点学算法96——折线分割平面

要求的是n条折线分割平面的最大数目

#include<stdio.h>
int linecut(int m)
{
    int i,ans,s=0;
    if(m==2)s=0;
    for(i=1;i<=m-2;i++)
    {
        s+=i;
    }
    ans=s+2*m;
    return ans;//定义函数求出2m条直线切割的区域数目
}
int main()
{
    int n,m,i;
    scanf("%d",&n);
    for(i=0;i<n;i++)//n组测试数据
    {
        scanf("%d",&m);
        printf("%d\n",linecut(2*m)-2*m);//调用函数并在输出的时候直接减去2*m求得m条折线切割的区域数目
    }
    return 0;
}

6.零起点学算法24——求正弦和余弦

#include<stdio.h>
#include<math.h>
#define PI 3.1415
int main(){
	int m,n;
	while(scanf("%d",&n)!=EOF){
		printf("%.2f\n",sin(n*PI/180.0));
		printf("%.2f\n",cos(n*PI/180.0));
	}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ACM Codebook(ACM代码手册)是一本整理了算法竞赛常用算法和数据结构的参考手册。它是为了帮助算法竞赛选手快速查阅各种算法和数据结构的实现而编写的。 ACM Codebook的内容非常丰富,包括了各种常用算法,如排序算法、图论算法、动态规划算法等。同时,它还包含了各种常用数据结构的实现,如链表、栈、队列、堆等。此外,ACM Codebook还介绍了一些常见的算法设计技巧和优化技巧,帮助选手更好地解决问题。 ACM Codebook的作用非常明显,首先它提供了各种算法和数据结构的实现代码,方便选手直接复制粘贴使用,节省了编写代码的时间。其次,ACM Codebook提供了详细的算法和数据结构的说明和示例,帮助选手理解和掌握这些算法和数据结构的原理和用法。最后,ACM Codebook还提供了一些常见问题的解决方案,帮助选手快速解决问题。 ACM Codebook的编写并不容易,需要作者具备扎实的算法和数据结构基础,并且对算法竞赛有深入的了解。编写ACM Codebook需要不断地修改和更新,以适应算法竞赛中不断变化的需求。 总之,ACM Codebook是一本非常有用的参考手册,它不仅提供了丰富的算法和数据结构的实现,还提供了对应的说明和示例,帮助算法竞赛选手快速掌握和应用这些算法和数据结构。它是算法竞赛选手在比赛中必备的工具之一。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值