[nefu]算法设计与分析-锐格实验
谈点个人感想:锐格这个题目和数据要是再不维护,估计直接就裂开了,跪求学校升级改进一下OJ系统和题目Orz
实验一 递归与分治
6104
#include<bits/stdc++.h>
using namespace std;
int max_num(int a[], int m,int n)
{
int mid = (m+n)/2;
if(m==n) return a[m];
else
{
int maxa = max_num(a, m, mid);
int maxb = max_num(a, mid+1, n);
return max(maxa, maxb);
}
}
int main()
{
int num;
cin>>num;
int a[num];
for(int i=0;i<num;i++)
cin>>a[i];
cout<<max_num(a, 0, num-1);
return 0;
}
6103(归并排序模板题)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N], tmp[N];
void merge_sort(int q[], int l,int r)
{
if(l >= r) return;
int mid = l+r>>1;
merge_sort(q, l, mid);
merge_sort(q, mid+1, r);
int k = 0,i = l, j = mid +1;
while(i<=mid && j<=r)
{
if(q[i] <= q[j]) tmp[k++] = q[i++];
else tmp[k++] = q[j++];
}
while(i<=mid) tmp[k++] = q[i++];
while(j<=r) tmp[k++] = q[j++];
for(i = l, j = 0; i<=r; i++, j++)
q[i] = tmp[j];
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
merge_sort(a, 0, n-1);
for(int i=0;i<n;i++)
printf("%d ", a[i]);
return 0;
}
6102
锐格显示没有输出,我:???
#include <iostream>
#include <cstring>
using namespace std;
int tile=1;
int Board[100][100];
/*
* tr : 棋盘左上角格子的行号
* tc : 棋盘左上角格子的列号
* dr : 特殊方格左上角顶点的行号
* dc特殊方格左上角顶点的列号
* size :棋盘规格 n*n
* title: 骨牌的型号
*/
void ChessBoard(int tr,int tc,int dr,int dc,int size)
{
if(size==1)
return;
int t=tile++;
int s=size/2;
if(dr<tr+s&&dc<tc+s) //特殊方格位于左上角的棋盘时
ChessBoard(tr,tc,dr,dc,s); //对左上角棋盘继续进行递归划分覆盖
else
{
Board[tr+s-1][tc+s-1]=t; //将t号L型骨牌覆盖当前子棋盘右下角的方格
ChessBoard(tr,tc,tr+s-1,tc+s-1,s); //对左上角棋盘继续进行递归划分覆盖
}
if(dr<tr+s&&dc>=tc+s) //覆盖右上角棋盘
ChessBoard(tr,tc+s,dr,dc,s); //对右上角棋盘继续进行递归划分覆盖
else
{
Board[tr+s-1][tc+s]=t; //将t号L型骨牌覆盖当前子棋盘左下角的方格
ChessBoard(tr,tc+s,tr+s-1,tc+s,s); //对右上角棋盘继续进行递归划分覆盖
}
if(dr>=tr+s&&dc<tc+s) //特殊方格位于左下角的棋盘时
ChessBoard(tr+s,tc,dr,dc,s); //对左下角棋盘继续进行递归划分覆盖
else
{
Board[tr+s][tc+s-1]=t; //将t号L型骨牌覆盖当前子棋盘左下角的方格
ChessBoard(tr+s,tc,tr+s,tc+s-1,s); //对右上角棋盘继续进行递归划分覆盖
}
if(dr>=tr+s&&dc>=tc+s) //特殊方格位于右下角的棋盘时
ChessBoard(tr+s,tc+s,dr,dc,s); //对右下角棋盘继续进行递归划分覆盖
else
{
Board[tr+s][tc+s]=t; //将t号L型骨牌覆盖当前子棋盘左上角的方格
ChessBoard(tr+s,tc+s,tr+s,tc+s,s); //对右下角棋盘继续进行递归划分覆盖
}
}
int main()
{
int size,x,y;
cin>>size;
cin>>x>>y;
memset(Board,0,sizeof(Board));
ChessBoard(1,1,x,y,size);
for(int i=1;i<=size;i++)
{
for(int j=1;j<=size;j++)
printf("%2d ",Board[i][j]);
cout<<endl;
}
return 0;
}
动态规划
6107
锐格报数组越界,爆内存,绝了数组改到两位数好使了,锐格的内存管理太精致了- -
#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#define CRT_SECURE_NO_WARNINGS
int dp[11][11];
int path[11][11];
using namespace std;
void LCSFindLongest(char X[], char Y[], int m, int n);
void LCS(int i, int j, char X[]);
int main() {
char X[100];
char Y[100];
X[0] = '!';
Y[0] = '!';
int m;
int n;
cin >> m >> n;
for (int i = 1; i <= m; i++) {
cin >> X[i];
}
for (int i = 1; i <= n; i++) {
cin >> Y[i];
}
X[m + 1] = '\0';
Y[n + 1] = '\0';
LCSFindLongest(X, Y, m, n);
LCS(m, n, X);
return 0;
}
void LCSFindLongest(char X[], char Y[], int m, int n) {
for (int i = 1; i <= m; i++) {
dp[i][0] = 0;
}
for (int i = 1; i <= n; i++) {
dp[0][i] = 0;
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (X[i] == Y[j]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
path[i][j] = 1;
}
else if (dp[i - 1][j] > dp[i][j - 1]) {
dp[i][j] = dp[i - 1][j];
path[i][j] = 2;
}
else if (dp[i - 1][j] < dp[i][j - 1]) {
dp[i][j] = dp[i][j - 1];
path[i][j] = 3;
}
else {
dp[i][j] = dp[i - 1][j];
path[i][j] = 4;
}
}
}
}
void LCS(int i, int j, char X[]) {
if (i == 0 || j == 0) {
return;
}
if (path[i][j] == 1) {
LCS(i - 1, j - 1, X);
cout << X[i];
}
else if (path[i][j] == 2 || path[i][j] == 4) {
LCS(i - 1, j, X);
}
else if (path[i][j] == 3) {
LCS(i, j - 1, X);
}
}
6106
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int orc[101][101]; // 原来的数。
int dp[101][101]; // 动态规划。
int path[101][101]; // 用来记录路径。
int number[101]; // 用来记录走过的数字。
memset(orc, 0, sizeof(orc));
memset(dp, 0, sizeof(dp));
memset(path, 0, sizeof(path));
memset(number, 0, sizeof(number));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cin >> orc[i][j];
}
}
dp[1][1] = orc[1][1];
for (int i = 1; i <= n; i++) { // 进行递归,就是判断该位置的最大的情况有可能是从上方或者左上方来的。
for (int j = 1; j <= i; j++) {
if (dp[i - 1][j] > dp[i - 1][j - 1]) {
dp[i][j] = dp[i - 1][j] + orc[i][j];
path[i][j] = 1; // 用来记录是从正上方来的。
}
else {
dp[i][j] = dp[i - 1][j - 1] + orc[i][j];
path[i][j] = 2; // 用来记录是从左上方来的。
}
}
}
int mm = 0;
int maxn = 0; // 用来记录最大的出口的位置。
for (int i = 0; i <= n; i++) {
if (mm < dp[n][i]) {
mm = dp[n][i];
maxn = i;
}
}
for (int i = n; i > 0; i--) { // 从path路径中提取出来经过的数字放在number中。
if (path[i][maxn] == 1) {
number[i] = orc[i][maxn];
maxn = maxn;
}
else if (path[i][maxn] == 2) {
number[i] = orc[i][maxn];
maxn = maxn - 1;
}
}
cout << mm << endl; // 找出最后一行的最大的数。
for (int i = 1; i <= n; i++) {
if (i != n) {
cout << number[i] << " ";
}
else cout << number[i] << endl;
}
return 0;
}
空谈误国,实干兴邦
个人博客: https://www.0error.net/