大号在练葵花宝典 发表于 2024-7-19 09:03:31

基础动态规划题目基础动态规划题目

目录
题目1: P1216 数字三角形 Number Triangles
 代码示例:
题目2: Common Subsequence
代码示例
题目3 :最长上升子序列
最长不下降子序列
最长上升子序列oj答案
题目1: P1216 数字三角形 Number Triangles

P1216 数字三角形 Number Triangles - 洛谷 | 盘算机科学教育新生态 (luogu.com.cn)https://csdnimg.cn/release/blog_editor_html/release2.3.6/ckeditor/plugins/CsdnLink/icons/icon-default.png?t=N7T8https://www.luogu.com.cn/problem/P1216https://i-blog.csdnimg.cn/direct/40f26514ebe6431e9a6ca12afb945afb.png
 代码示例:

// c++ 代码示例
#include <algorithm>
#include <iostream>

using namespace std ;

int n,a,f ;

int dfs(int x, int y)
{
    if (x == n) return a ;
    if (f != -1) return f ;
    return f = max(dfs(x + 1, y), dfs(x + 1, y + 1)) + a ;
}

int main()
{
    int n ;
    cin >> n ;
    for (int i = 1 ; i <= n ; i++)
    {
      for (int j = 1 ; j <= i ; j++)
      {
            cin >> a ;
      }
    }
    for (int i = 1 ; i <= n ; i++)
    {
      for (int j = 1 ; j <= i ; j++)
      {
            f = -1 ;
      }
    }
    cout << dfs(1, 1) ;
    return 0 ;
} // c++ 代码示例

#include <algorithm>
#include <iostream>
using namespace std ;

long long n, a ;
int main()
{
    cin >> n ;
    for (int i = 1 ; i <= n ; i++)
    {
      for (int j = 1 ; j <= i ; j++)
      {
            cin >> a ;
      }
    }
    for (int i = n ; i >= 1 ; i--)
    {
      for (int j = 1 ; j <= i ; j++)
      {
            a = a + max(a, a);      
            
      }
    }
    cout << a ;
    return 0 ;
   
}  
// c++ 代码示例

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
using namespace std;

#define rint register int
inline void read(int &x)
{
    x = 0;
    int w = 1;
    char ch = getchar();
    while (!isdigit(ch) && ch != '-')
    {
      ch = getchar();
    }
    if (ch == '-')
    {
      w = -1;
      ch = getchar();
    }
    while (isdigit(ch))
    {
      x = (x << 3) + (x << 1) + (ch ^ '0');
      ch = getchar();
    }
    x = x * w;
}

const int maxn = 1000 + 10;

int n, a, ans;

int main()
{
    read(n);
    for (rint i = 1; i <= n; i++)
    {
      for (rint j = 1; j <= i; j++)
      {
            read(a);
            if (i == 1 && j == 1)
            {
                // The top of the triangle
                continue;
            }
            if (j == 1)
            {
                // Left boundary
                a += a;
            }
            else if (j == i)
            {
                // Right boundary
                a += a;
            }
            else
            {
                // Middle elements
                a += max(a, a);
            }
            ans = max(ans, a);
      }
    }
    cout << ans << endl;
    return 0;
} https://i-blog.csdnimg.cn/direct/143ab111e9ca4e7b9251b0aeeeae0f7c.png
题目2: Common Subsequence

Common Subsequence - HDU 1159 - Virtual Judge (vjudge.net)https://csdnimg.cn/release/blog_editor_html/release2.3.6/ckeditor/plugins/CsdnLink/icons/icon-default.png?t=N7T8https://vjudge.net/problem/HDU-1159
代码示例

// c++ 代码示例
int a, b, f ;

int dp()
{
    for (int i = 1 ; i <= n ; i++)
    {
      for (int j = 1 ; j <= m ; j++)
      {
            if (a == b)
            {
                f = f + 1 ;
            }
            else
            {
                f = std::max(f, f) ;
            }
      }
    }
    return f ;
} // c++ 代码示例

#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>

using namespace std;

char a, b;
int dp, len1, len2;

void lcs(int i,int j)
{
    for(i=1; i<=len1; i++)
    {
      for(j=1; j<=len2; j++)
      {
            if(a == b)
                dp = dp + 1;
            else if(dp > dp)
                dp = dp;
            else
                dp = dp;
      }
    }
}

int main()
{
    while(~scanf(" %s",a))
    {
      scanf(" %s", b);
      memset(dp, 0, sizeof(dp));
      len1 = strlen(a);
      len2 = strlen(b);
      lcs(len1, len2);
      printf("%d\n", dp);
    }
    return 0;
} 题目3 :最长上升子序列

信息学奥赛一本通(C++版)在线评测体系 (ssoier.cn)https://csdnimg.cn/release/blog_editor_html/release2.3.6/ckeditor/plugins/CsdnLink/icons/icon-default.png?t=N7T8http://ybt.ssoier.cn:8088/problem_show.php?pid=1281
最长不下降子序列

//c++代码示例
# include <iostream>
# include <cstdio>
using namespace std ;
int n ;
int a ;
int f ;
int mx = -1 ;
int main()
{
    scanf("%d", &n) ;
    for (int i = 1 ; i <= n ; i++)
    {
      scanf("%d", &a) ;
      f = 1 ;
    }
    for (int i = 2 ; i <= n ; i++)
    {
      for (int j = i - 1 ; j >= 1 ; j--)
      {
            if (a >= a)
            {
                f = max(f, f + 1) ;
            }
      }
    }
    for (int i = 1 ; i <= n ; i++)
    {
      mx = max(mx, f) ;
    }
    printf("%d", mx) ;
    return 0 ;
   
} //c++代码示例
# include <iostream>
# include <cstdio>
using namespace std ;
int n ;
int a ;
int f ;
int mx = -1 ;
int main()
{
    scanf("%d", &n) ;
    for (int i = 1 ; i <= n ; i++)
    {
      scanf("%d", &a) ;
      f = 1 ;
    }
    for (int i = n - 1 ; i >= 1 ; i--)
    {
      for (int j = i + 1 ; j <= n ; j++)
      {
            if (a <= a)
            {
                f = max(f, f + 1) ;
            }
      }
    }
    for (int i = 1 ; i <= n ; i++)
    {
      mx = max(mx, f) ;
    }
    printf("%d", mx) ;
    return 0 ;
   
} 最长上升子序列oj答案

//c++代码示例
# include <iostream>
# include <cstdio>
using namespace std ;
int n ;
int a ;
int f ;
int mx = -1 ;
int main()
{
    scanf("%d", &n) ;
    for (int i = 1 ; i <= n ; i++)
    {
      scanf("%d", &a) ;
      f = 1 ;
    }
    for (int i = n - 1 ; i >= 1 ; i--)
    {
      for (int j = i + 1 ; j <= n ; j++)
      {
            if (a < a)
            {
                f = max(f, f + 1) ;
            }
      }
    }
    for (int i = 1 ; i <= n ; i++)
    {
      mx = max(mx, f) ;
    }
    printf("%d", mx) ;
    return 0 ;
   
}
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 基础动态规划题目基础动态规划题目