1.19今日总结

上午学习3个半小时

补了一个补题,学习了二叉树。

CF18B Platforms

题目描述

In one one-dimensional world there are nn platforms. Platform with index kk (platforms are numbered from 1) is a segment with coordinates [(k-1)m,(k-1)m+l][(k−1)m,(k−1)m+l] , and l<ml<m . Grasshopper Bob starts to jump along the platforms from point 00 , with each jump he moves exactly dd units right. Find out the coordinate of the point, where Bob will fall down. The grasshopper falls down, if he finds himself not on the platform, but if he finds himself on the edge of the platform, he doesn't fall down.

输入格式

The first input line contains 4 integer numbers nn , dd , mm , ll ( 1<=n,d,m,l<=10^{6},l<m1<=n,d,m,l<=106,l<m ) — respectively: amount of platforms, length of the grasshopper Bob's jump, and numbers mm and ll needed to find coordinates of the kk -th platform: [(k-1)m,(k-1)m+l][(k−1)m,(k−1)m+l] .

输出格式

Output the coordinates of the point, where the grosshopper will fall down. Don't forget that if Bob finds himself on the platform edge, he doesn't fall down.

题意翻译

题目描述:在一坐标轴上给出n块板子,每个板子所占的空间为[(k-1)m,(k-1)m+l](l<m),一个青蛙从原点0起跳,每次跳d距离远,问最后青蛙会落在哪里(没落在板子上就结束跳跃) 输入:一行四个整数n,d,m,l 输出:一个整数,即青蛙最后的落点 1<=n,d,m,l<=10^6 l<m

Translated by 稀神探女

输入输出样例

输入 #1复制

2 2 5 3

输出 #1复制

4

输入 #2复制

5 4 11 8

输出 #2复制

20

这个之前我有一种写法,但是时间超限了,之前的写法是,定一个死循环,每次循环都跳一次,然后再模拟跳一次,判断青蛙是否超过现在的盘子,然后没在下一个盘子上,如果是这样的话就输出

模拟跳一次的结果,结束循环。如果在下一个盘子上,k就加一。但是时间超限了。

之后看了看题解,他们是用盘子的末端除以跳跃的长度,然后再乘跳跃的长度,最后加一个长度来表示现在青蛙所在之地,再判断青蛙是否在下一个盘子上。他这个就是取最接近末端的一步然后再判断下一步。

代码实现:

#include<stdio.h>
int main()
{
	long long n,d,m,l;
	scanf("%lld%lld%lld%lld",&n,&d,&m,&l);
    long long s=0;
   for(int i=1;i<=n;i++)
   {
   	if(s<(i-1)*m)//判断青蛙现在是否在盘子上 
   	{
   		break;
	   }
	   while(s<=(i-1)*m+l)
	   {
	   	s=(((i-1)*m+l)/d)*d+d;//跳到接近盘子末端的时候再跳一次 
	   }
   }
   printf("%lld",s);
	}

P1229 遍历问题

题目描述

我们都很熟悉二叉树的前序、中序、后序遍历,在数据结构中常提出这样的问题:已知一棵二叉树的前序和中序遍历,求它的后序遍历,相应的,已知一棵二叉树的后序遍历和中序遍历序列你也能求出它的前序遍历。然而给定一棵二叉树的前序和后序遍历,你却不能确定其中序遍历序列,考虑如下图中的几棵二叉树:

所有这些二叉树都有着相同的前序遍历和后序遍历,但中序遍历却不相同。

输入格式

输A数据共两行,第一行表示该二叉树的前序遍历结果s1,第二行表示该二叉树的后序遍历结果s2。

输出格式

输出可能的中序遍历序列的总数,结果不超过长整型数。

输入输出样例

输入 #1复制

abc                           
cba

输出 #1复制

4

说明/提示

无提示

知道前序遍历和中序遍历可以得出唯一的二叉树,知道后序遍历和中序遍历也可以得出唯一的二叉树,但是只知道前序遍历和后序遍历,并且只有一个子节点的时候,就有多种情况的中序遍历,前序遍历为父左右,后序遍历为左右父,即每找到一个值,如果前序遍历后面一个值与后序遍历前面一个值相同的话,则表明这个值只有一个子节点,所以就有左右之分。

代码实现:

#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
	char a[1000];
	char b[1000];
	gets(a);
	gets(b);
	int m=strlen(a);
	int n=strlen(b);
	int cnt=0;
	for(int i=0;i<m;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(a[i]==b[j]&&a[i+1]==b[j-1])//即找到只有一个子节点的,根据前序父左右,后序左右父,可知前序父在前,后续子在前,所以只需比对前序后面一个,后序前面一个即可 
			cnt++;
		}
	}
	int s=pow(2,cnt);
	printf("%d",s);
 } 

下午3个半小时

学习了前序遍历、后序遍历和中序遍历的知识。

P4913 【深基16.例3】二叉树深度 

题目描述

给出每个节点的两个儿子节点,建立一棵二叉树(根节点为 11),如果是叶子节点,则输入0 0。建好树后希望知道这棵二叉树的深度。二叉树的深度是指从根节点到叶子结点时,最多经过了几层。

最多有 10^6106 个结点。

输入格式

输出格式

输入输出样例

输入 #1复制

7
2 7
3 6
4 5
0 0
0 0
0 0
0 0

输出 #1复制

4

思路:这个题只需要遍历左右端点的时候判断步数是否比现在的最大值步数大即可。

代码实现:

#include<bits/stdc++.h>
using namespace std;
struct node{
	int l;//左节点 
	int r;//右节点 
};
int step;//步数 
node tree[1000000];//建树 
int maxm=0;//最大值 
void print(int a,int step)
{
	maxm=max(maxm,step);
	if(a!=0)
	print(tree[a].l,step+1);//遍历左节点 
    if(a!=0)
 	print(tree[a].r,step+1);//遍历右节点 
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<n;i++)
	scanf("%d%d",&tree[i].l,&tree[i].r);
	print(1,0);
	printf("%d",maxm);
 } 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值