Super Jumping! Jumping! Jumping!
The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path.
Your task is to output the maximum value according to the given chessmen list.#include<stdio.h>#include<string.h>#include<stdlib.h>int main(){int n,i,j;int number[1005];int sum[1005],max,max1;while(scanf("%d",&n)!=EOF){if(n==0) break;memset(number,0,sizeof(number));for(i=1;i<=n;i++){ scanf("%d",&number[i]); sum[i]=number[i];}//printf("\n");//for(i=1;i<=n;i++)// printf("%d ",number[i]);//printf("\n");//记得初始化 max=1; for(i=2;i<=n;i++) {max1=0; //这个循环是为了找i前面的最大加和值用的 for(j=1;j<i;j++){ if(number[i]>number[j]) { if(sum[j]>max1) max1=sum[j]; } } //找到后就加上这个值成为符合条件下的自身最大加和值 sum[i]+=max1;//对比查找最大用的,直接写在同一个循环内,就不用再另外写一个来找里面的最大值输出了 if(sum[i]>max) max=sum[i];//printf("\nmax1=%d\n",max1);// printf("sum[%d]=%d\n",i,sum[i]);// printf("max=%d\n",max);// system("pause");//测试用}printf("%d\n",max);}return 0;}
?Compare:
? ? 最长递增子序列:
for(i=1;i<length;i++) { for(j=0;j<i;j++) if(s[i]>s[j]) max[i]=max[i]>max[j]+1?max[i]:max[j]+1; //直接对比更新值 if(max[i]>m) m=max[i]; }? ?最大和递增子序列:
for(i=2;i<=n;i++) { max1=0; for(j=1;j<i;j++) { if(number[i]>number[j]) {if(sum[j]>max1) max1=sum[j]; } }//这里不可以直接对比更新值,必须j循环完才可以 sum[i]+=max1; if(sum[i]>max) max=sum[i]; }? ?总结:
? ?还是需要提高一下实现能力,不能急,得慢慢来……
?