Homework-10 BASIC

时间:2021-06-26 02:45:23

对于本次作业:

我的整体思路如下:

1、首先修改二维数组求最大子数组和的C语言代码,加入分步骤的当前最优解边界值,局部最优解的记录,使之支持分步执行,连续执行,回滚等功能。

2、将程序改写为Javascript嵌入到HTML中,并进行呈现。

1、依据原有搜索和动态规划算法,记录每一步骤选定的line和column边界值,当前最大子数组和的值。

  

line和column边界值由linea,lineb,linex,liney 记录

max记录每一步的局部最优解。

输入时加入mode选择,控制连续执行还是单步执行。

 #include <stdio.h>
#include <dos.h>
//Part_define
int n,m;
long long max=-,data[][];
int sum=;
void Part_intput();
void Part_function(int mode);
void Part_output();
int main()
{
int mode;
printf("Welcome to our programme! Here I gonna show you this,for a two-dimensional array,how the maximum of sub-array's sum is calculated!\n");
printf("First comes the input part:\n");
Part_intput(); printf("You can choose different modes, \nMode 1 for viewing the process step by step~! \nMode 2 for viewing it uninterrupted~!\n");
while()
{
printf("Please choose your mode 1/2?");
scanf("%d",&mode);
if(mode!= && mode !=)printf("Your mode number is illegal.\n");
else break;
}
printf("\n"); Part_function(mode);
Part_output();
return ;
}
void Part_intput()
{
int i,j;
printf("Please enter the number of lines: ");
scanf("%lld",&m);
printf("and the number of columns: ");
scanf("%lld",&n);
printf("then the elements of the array:\n"); for(i=;i<=m;i++)
{
for(j=;j<=n;j++)
{
scanf("%lld",&data[i][j]);
}
}
}
void Part_function(int mode)
{
int i,j,k,l,linex,liney,linea,lineb;
char o;
long long f[][]={},columnsum[]={};
linex=;liney=,linea=,lineb=;
for(k=;k<=m;k++)
{
for(l=k;l<=m;l++)
{ for(i=;i<=n;i++){columnsum[i]=;f[][i]=;f[][i]=;} for(i=;i<=n;i++){
for(j=k;j<=l;j++){
columnsum[i]+=data[j][i];
}
}
f[][]=columnsum[];f[][]=columnsum[]; for(i=;i<=n;i++){
if(f[][i-]+columnsum[i]>=f[][i-] && f[][i-]+columnsum[i]>=columnsum[i]){f[][i]=f[][i-]+columnsum[i];liney=i;}
if(f[][i-]+columnsum[i]<=f[][i-] && f[][i-]>=columnsum[i])f[][i]=f[][i-];
if(f[][i-]+columnsum[i]<=columnsum[i] && f[][i-]<=columnsum[i]){f[][i]=columnsum[i];linex=i;liney=i;}
if(f[][i-]>=)f[][i]=f[][i-]+columnsum[i];
else {f[][i]=columnsum[i];}
}
if(max<=f[][n]){max=f[][n];linea=k,lineb=l;}
printf("The current maximum domain of sub-array is from Line %d to Line %d, from Column %d to Column %d\n",linea,lineb,linex,liney);
printf("The current maximum value of sub-array is :%d\n",max);
if(mode==)
{
printf("Next Step? Y/N\n");
while()
{ scanf("%c",&o);
if(o=='Y')break; }
}
else {sleep();} }
}
//f[0][i]=f[1][i-1]+a[i] f[0][i-1] a[i];
//f[1][i]=f[1][i-1]+a[i] a[i]
} void Part_output()
{
printf("The maximum value of sub-array is :%d\n",max);
system("pause"); }
/*
3
6
5 6 -3 8 -9 2
1 -12 20 0 -3 -5
-9 -7 -3 6 7 -1
*/

运行结果如下:

Homework-10 BASIC

顺利完成。

2、由于之前对于HTML,JAVASCRIPT之前了解不是很多。正在学习中,预计这两天将程序改善并更新,届时再给出总结所花费的时间和估计,以及总结等等。