Training Land of Fury
here the solution for NFURY spoj or training land of fury spoj .
#include <iostream> #include <cstdlib> #include <cmath> using namespace std; int main() { int *list=(int*)calloc(1001,sizeof (int)); list[1]=1; list[2]=2; list[3]=3; int i,max,j; for (i=4; i<=1000; i++) { max=1000000; if ((int)sqrt(i)==sqrt(i)) { list[i]=1; } else { for (j=1; j<=i/2; j++) { if (list[j]+list[i-j] < max) max=list[j]+list[i-j]; } list[i]=max; } } int t,n; cin>>t; while(t--) { cin>>n; cout<<list[n]<<endl; } return 0; }
good application of dynamic programming.
ReplyDeletecan u explane this please
ReplyDelete