Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

难道用函数调用走不通?硬要换成循环?

Posted by jxdz166705 at 2006-08-29 17:08:55 on Problem 1163
算法应该没什么问题,PKU里好多题目用函数调用都TLE晕
这题用不带函数调用的动态规划方法该怎么解呢?




#include "stdio.h"
int a[100][100];


int dp(int i,int j,int n)
{int q,left,right;
   if(i==n)return a[i][j];
   else 
     {left=dp(i+1,j,n)+a[i][j];
      right=dp(i+1,j+1,n)+a[i][j];
      if(left>right)return left;
      else return right;
     } 
}


int main()
{int  i,n,j,max;
 char ch;  
   while(scanf("%d",&n)!=EOF)
   {for(i=1;i<=n;i++)
      for(j=1;j<=i;j++)
       {scanf("%d",&a[i][j]);
       ch=getchar();}
       i=1;
       j=1;
    max=dp(i,j,n);     
    printf("%d\n",max);
   } 
return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator