1.这周补题的青蛙跳,测试时我用结构体做且没开long long且数据范围太小了,然后我放弃结构体这种做法因为怕内存超限,然后常规去循环增加距离就造成了我时间超限,用这块板子的长度/距离再加一个距离保证我们跳出去。
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
long long q,h;
long long n,d,m,l,ans=0;
cin >>n>>d>>m>>l;
long long tpq,tph;
long long coun=1;
for(long long k=1;k<=n;k++)
{
if(ans<(k-1)*m)
{
break;;
}
while(ans<=(k-1)*m+l)
{
ans=(((k-1)*m+l)/d)*d+d;
}
}
cout << ans;
}
2.补题cf比赛
此题要注意的是可以交换位置但是要考虑交换后的合法性,不知道那种是可以的所以我么把每种情况算进去,特别注意二月份如果那年是闰年则二月多一天
#include<iostream>
#include <algorithm>
using namespace std;
int d[3],b[3];
int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int solve (int d1,int m, int y)
{
if(d1<1||m<1||m>12||d1>month[m]+(m==2&&y%4==0))
return 0;
return(y+18<d[2]||(y+18)==d[2]&&m<d[1]||(y+18)==d[2]&&m==d[1]&&d1<=d[0]);
}
int main()
{
scanf("%d.%d.%d",&d,d+1,d+2);
scanf("%d.%d.%d",b,b+1,b+2);
if(solve(b[0],b[1],b[2])||solve(b[0],b[2],b[1])||solve(b[1],b[0],b[2])||solve(b[2],b[1],b[0])||solve(b[1],b[2],b[0])||solve(b[2],b[0],b[1]))
cout <<"YES"<<endl;
else cout <<"NO"<<endl;
}
3.二叉树
通过二叉树的前序遍历和中序遍历确定一棵树,
通过前序遍历可以知道第一个字符为根节点
而中序遍历是左父右,所以在中序里面找到根节点分成左右两部分
再利用递归找出他们的左右节点
#include <iostream>
#include<string>
using namespace std;
struct node
{
char c,l,r;
}tree[10000];
string s,t;
int coun=-1;
int build(int start,int end)
{
if(start>end)return -1;//已经不可以在分
int i;
coun++;
for(i=start;i<=end;i++)
if(s[i]==t[coun])break;
if(i>end)return -1;//找不到节点
tree[i].c=t[coun];
tree[i].l=build(start,i-1);//继续找左右节点
tree[i].r=build(i+1,end);
return i;
}
void postorder(int n)
{
if(n==-1)return;
postorder(tree[n].l);
postorder(tree[n].r);
cout << tree[n].c;
}
int main()
{
cin >> s>> t;
int root=build(0,t.length()-1);//得到根节点
postorder(root);
}
4此题是利用中序和后序确定一棵树,与上题很像,只要改变下顺序,注意的是由于是在后序里面找中序,由于我是倒过来,所以要先建右枝
#include <iostream>
#include<string>
using namespace std;
struct node
{
char c,l,r;
}tree[10000];
string s,t;
int coun;
int build(int start,int end)
{
if(end>start)return -1;
int i;
coun--;
for(i=start;i>=end;i--)
if(s[i]==t[coun])break;
if(i<end)return -1;
tree[i].c=t[coun];
tree[i].r=build(start,i+1);
tree[i].l=build(i-1,end);
return i;
}
void postorder(int n)
{
if(n==-1)return;
cout << tree[n].c;
postorder(tree[n].l);
postorder(tree[n].r);
}
int main()
{
cin >> s>> t;
coun=t.length();
int root=build(t.length()-1,0);
postorder(root);
}