学习了树的建立以及输出(三种遍历方法)
写了三道补题
#include<iostream>
#include<cstdlib>
#include<queue>
using namespace std;
struct node//定义结构体
{
char date;
struct node *child1;//左子树
struct node*child2;//右子树
};
void creat(struct node *&t)//先序遍历创建树,传入t指针的地址
{
char a;
cin>>a;
if(a=='#')
t=NULL;
else
{
t=(struct node *)malloc(sizeof(struct node));//创建空间
t->date=a;
creat(t->child1);
//先遍历完左子树再遍历右子树
creat(t->child2);
}
}
void output1(struct node *t)//先序遍历
{
if(t!=NULL)
{
cout<<t->date;
output1(t->child1);
output1(t->child2);
}
}
void output2(struct node *t)//中序遍历
{
if(t!=NULL)
{
output2(t->child1);
cout<<t->date;
output2(t->child2);
}
}void output3(struct node *t)//后序遍历
{
if(t!=NULL)
{
output3(t->child1);
output3(t->child2);
cout<<t->date;
}
}
int main()
{
struct node *t;
creat(t);
output1(t);
cout<<endl;
output2(t);
cout<<endl;
output3(t);
cout<<endl;
}
层次遍历写不出 ,明天学习bfs,写层次遍历
题目描述
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
#include<stdio.h>
int main()
{
long long int n,d,m,l,s,x=0,y;//n板子个数,d跳的步数
scanf("%lld%lld%lld%lld",&n,&d,&m,&l);
for(int i=1; i<=n; i++)
{
s=(i-1)*m;//前一块板子的后端
y=(i-1)*m+l;//后一块板子的前端
//printf("%d %d\n",s,y);
if(x<s)//没有跳到后一块板子上
break;
else
x=(y/d+1)*d;//保证跳过该板子的后端
}
printf("%lld\n",x);
}