Homework-10 BASIC
对于本次作业:
我的整体思路如下:
1、首先修改二维数组求最大子数组和的C语言代码,加入分步骤的当前最优解边界值,局部最优解的记录,使之支持分步执行,连续执行,回滚等功能。
2、将程序改写为Javascript嵌入到HTML中,并进行呈现。
1、依据原有搜索和动态规划算法,记录每一步骤选定的line和column边界值,当前最大子数组和的值。
line和column边界值由linea,lineb,linex,liney 记录
max记录每一步的局部最优解。
输入时加入mode选择,控制连续执行还是单步执行。
1 #include <stdio.h> 2 #include <dos.h> 3 //Part_define 4 int n,m; 5 long long max=-999999,data[1001][1001]; 6 int sum=0; 7 void Part_intput(); 8 void Part_function(int mode); 9 void Part_output(); 10 int main() 11 { 12 int mode; 13 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"); 14 printf("First comes the input part:\n"); 15 Part_intput(); 16 17 printf("You can choose different modes, \nMode 1 for viewing the process step by step~! \nMode 2 for viewing it uninterrupted~!\n"); 18 while(1) 19 { 20 printf("Please choose your mode 1/2?"); 21 scanf("%d",&mode); 22 if(mode!=1 && mode !=2)printf("Your mode number is illegal.\n"); 23 else break; 24 } 25 printf("\n"); 26 27 Part_function(mode); 28 Part_output(); 29 return 0; 30 } 31 void Part_intput() 32 { 33 int i,j; 34 printf("Please enter the number of lines: "); 35 scanf("%lld",&m); 36 printf("and the number of columns: "); 37 scanf("%lld",&n); 38 printf("then the elements of the array:\n"); 39 40 for(i=1;i<=m;i++) 41 { 42 for(j=1;j<=n;j++) 43 { 44 scanf("%lld",&data[i][j]); 45 } 46 } 47 } 48 void Part_function(int mode) 49 { 50 int i,j,k,l,linex,liney,linea,lineb; 51 char o; 52 long long f[2][1001]={0},columnsum[1001]={0}; 53 linex=1;liney=1,linea=1,lineb=1; 54 for(k=1;k<=m;k++) 55 { 56 for(l=k;l<=m;l++) 57 { 58 59 for(i=1;i<=n;i++){columnsum[i]=0;f[0][i]=0;f[1][i]=0;} 60 61 for(i=1;i<=n;i++){ 62 for(j=k;j<=l;j++){ 63 columnsum[i]+=data[j][i]; 64 } 65 } 66 f[0][1]=columnsum[1];f[1][1]=columnsum[1]; 67 68 for(i=2;i<=n;i++){ 69 if(f[1][i-1]+columnsum[i]>=f[0][i-1] && f[1][i-1]+columnsum[i]>=columnsum[i]){f[0][i]=f[1][i-1]+columnsum[i];liney=i;} 70 if(f[1][i-1]+columnsum[i]<=f[0][i-1] && f[0][i-1]>=columnsum[i])f[0][i]=f[0][i-1]; 71 if(f[1][i-1]+columnsum[i]<=columnsum[i] && f[0][i-1]<=columnsum[i]){f[0][i]=columnsum[i];linex=i;liney=i;} 72 if(f[1][i-1]>=0)f[1][i]=f[1][i-1]+columnsum[i]; 73 else {f[1][i]=columnsum[i];} 74 } 75 if(max<=f[0][n]){max=f[0][n];linea=k,lineb=l;} 76 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); 77 printf("The current maximum value of sub-array is :%d\n",max); 78 if(mode==1) 79 { 80 printf("Next Step? Y/N\n"); 81 while(1) 82 { 83 84 scanf("%c",&o); 85 if(o=='Y')break; 86 87 } 88 } 89 else {sleep(1000);} 90 91 } 92 } 93 //f[0][i]=f[1][i-1]+a[i] f[0][i-1] a[i]; 94 //f[1][i]=f[1][i-1]+a[i] a[i] 95 } 96 97 void Part_output() 98 { 99 printf("The maximum value of sub-array is :%d\n",max); 100 system("pause"); 101 102 } 103 /* 104 3 105 6 106 5 6 -3 8 -9 2 107 1 -12 20 0 -3 -5 108 -9 -7 -3 6 7 -1 109 */
运行结果如下:
顺利完成。
2、由于之前对于HTML,JAVASCRIPT之前了解不是很多。正在学习中,预计这两天将程序改善并更新,届时再给出总结所花费的时间和估计,以及总结等等。