2020-2-11赛

递归+记忆化(不然超时。。。)

问题 C: 桐桐的递归函数

时间限制: 1 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

桐桐经常找一些很有趣的数学书来阅读以增长自己的数学知识。
一天,他偶然发现一个递归函数w(a,b,c)有以下性质:
如果a<=0 or b<=0 or c<=0就返回值1;
如果a>20 or b>20 or c>20就返回w(20,20,20);
如果a<b并且b<c就返回w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
其它别的情况就返回w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1)。
桐桐想通过编程求出这个简单的递归函数的值,可是他在编程的时候遇到了一些困难。你能帮助他吗?

 

输入

a,b,c三个整数, -50≤a,b,c≤50。

输出

a,b,c三个整数所对应的w(a,b,c)函数的值。要求以w(a, b, c)=函数的值来输出。

样例输入 Copy

1 1 1

样例输出 Copy

w(1, 1, 1) = 2

提示

注:下面用下画线表示空格以让你更清楚输出格式。
w(1, 1, 1) = 2

#include <bits/stdc++.h>
using namespace std;
const int mod=2e5+5;
typedef long long ll;
int A[150][150][150];
int w(int a,int b,int c)
{
    if(A[a+50][b+50][c+50]) return A[a+50][b+50][c+50];//超时就加记忆化
     else if(a<=0||b<=0||c<=0) A[a+50][b+50][c+50]=1;
     else if(a>20||b>20||c>20) A[a+50][b+50][c+50]=w(20,20,20);
     else  if((a<b)&&(b<c)) A[a+50][b+50][c+50]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
     else A[a+50][b+50][c+50]=w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1);
     return A[a+50][b+50][c+50];
}
int main()
{
 
     int x,y,z;
     scanf("%d %d %d",&x,&y,&z);
     printf("w(%d, %d, %d) = %d",x,y,z,w(x,y,z));
    return 0;
}

这道题理解题意很关键。。。

问题 G: Lower

时间限制: 1 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

There are N squares arranged in a row from left to right.
The height of the i-th square from the left is Hi.
You will land on a square of your choice, then repeat moving to the adjacent square on the right as long as the height of the next square is not greater than that of the current square.
Find the maximum number of times you can move.

Constraints
·All values in input are integers.
·1≤N≤105
·1≤Hi≤109

输入

Input is given from Standard Input in the following format:

N
H1 H2 ... HN

输出

Print the maximum number of times you can move.

样例输入 Copy

【样例1】
5
10 4 8 7 3
【样例2】
7
4 4 5 6 6 5 5
【样例3】
4
1 2 3 4

样例输出 Copy

【样例1】
2
【样例2】
3
【样例3】
0

提示

样例1解释
By landing on the third square from the left, you can move to the right twice.
样例2解释
By landing on the fourth square from the left, you can move to the right three times.

用递归的思想来写。个人觉得很棒

#include <bits/stdc++.h>
using namespace std;
const int mod=2e5+5;
typedef long long ll;
int A[mod],dp[mod];
int main()
{
    int n,ans=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&A[i]);
    }
    for(int i=2;i<=n;i++)
    {
      if(A[i]<=A[i-1]) dp[i]=dp[i-1]+1;
      else dp[i]=0;
      ans=max(ans,dp[i]);
    }
printf("%d",ans);
    return 0;
}

 

问题 N: 密码

时间限制: 1 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

在浩浩茫茫的苍穹深处,住着玉帝和他的神仆们,他们闲谈着下界的凡人俗事, 对人世间表现的聪明,大加赞赏。今天他们正在观赏大地风光人情之际,忽然从遥远 的东海之滨传来一阵欢笑声,玉帝纵目望去,只见镇海中学内聚集了全宁波的中小学 精英学生,他们要干什么呢? 原来他们都在做一种破译密码的游戏,人们发现:一根密码棒上的数字加上另一 根密码棒上的数字,就是开启天庭粮库密码乌锁的密码。 如:1233+67122=68355,则68355就是一组就效的密码。 “太简单了!”人们高呼起来,继续探索着。 “这不过是早期的密码系统而已。”玉帝轻蔑地环顾神仆们说道。 可是,当人们演算了139513+3344=142857后,玉帝的神色愈来愈不对了,要知 道,142857是一个特别的数字,这可是天庭的机密,是谁将这些机密泄露给世人呢? 于是,玉帝搬出一张牌,对司粮库主管神农氏说:“将这张牌打出去,看看他们 还逞能不?” 这是天庭中一张王牌,但平凡的很,只不过将密码的位数增大到不超过200位而已, 可是难就难在你看到文件:输入两个数后,必须在1秒钟内将密码求出,否则这组密码就失效了。 玉帝还是仁慈的,没有将更难的牌打出来,他想到天庭的粮食恩赐人间,但他绝 不会给那些不动脑子的人。 现在请你解开天庭司粮库密码锁的密码,帮助人们获得天庭恩赐的粮食。

输入

共有两行,每行一个整数

输出

只有一行,该行只有一个整数,为输入的两个正整数之和

样例输入 Copy

1233
67122

样例输出 Copy

68355
#include <bits/stdc++.h>
using namespace std;
const int mod=2e5+5;
typedef long long ll;
char s[205],t[205];
int sum[500];
int main()
{
  scanf("%s",s);
  int len1=strlen(s);
  scanf("%s",t);
  int len2=strlen(t);
  int len=max(len1,len2);
  int yu=0,a,b;
  for(int i=len-1,j=len1-1,k=len2-1;i>=0;j--,k--,i--)
  {
     if(j>=0) a=s[j]-'0';
     else a=0;
     if(k>=0) b=t[k]-'0';
     else b=0;
     sum[i]=(a+b+yu)%10;
     yu=(a+b+yu)/10;
  }
  sum[0]+=yu*10;
  int f=1;
  for(int i=0;i<=len-1;i++)
  {
      if(sum[i]!=0&&f==1)
      {
          printf("%d",sum[i]);
          f=0;
      }
      else if(f==0)
      {
           printf("%d",sum[i]);
      }
  }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值