dimanche 28 mai 2017

Why Does This Code Freeze When N>=10?

This is a program written in C++11.

When text.in has a value of N<10 this program works fine. However, when N is increased to say 11, it freezes and seems to go on forever. Why could this be?

#include <cstdio>
using namespace std;

int dp[40][391]={0};

int main() {
    FILE* in = fopen("text.in","r");
    FILE* out = fopen("text.out","w");
    int N; 
    fscanf(in,"%d",&N); 
    int sum = N*(N+1)/2;
    for (int i=0; i<=N; i++) dp[0][i]=1;
    if (sum%2==1) {fprintf(out,"0"); return 0;}
    for (int n=1; n<=sum; n++) {
        for (int k=1; k<=N; k++) {
            if (n-k>=0) dp[n][k]=dp[n-k][k-1];
            dp[n][k]+=dp[n][k-1];
        }
    }
    fprintf(out,"%d",dp[sum/2][N]/2);
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire