簡體   English   中英

在C中經過一段時間后如何停止遞歸

[英]How to stop recursion after some time in C

我需要在C語言中停留30秒后停止遞歸。我的嘗試之一是使用goto盡管建議不要使用goto ,但是我不能在不同的函數之間使用它。 代碼如下:

void func_t(int n, int a, int b, int c, int d, int e, int f, int g, int height, Data *data, time_t start_time){        
    int cont;
    data->level_recursions[height] =  data->level_recursions[height] + 1;

    if ( n<= 1) return; 

    for (cont = 1; cont <= a; cont++){
        data->height = height + 1;
        func_t( (n/b) - c, a, b, c, d, e, f, g, 1 + height, data );
    }

    for (cont = 1; cont <= d; cont++) {
        data->height = height + 1;
        func_t( (n/e) - f, a, b, c, d, e, f, g, 1 + height, data );
    }

    clock_t begin = clock();
    for (cont = 1; cont <= fn(n, g); cont++);
    clock_t end = clock();
    data->level_work[height] = data->level_work[height] + ((double)(end - begin) / CLOCKS_PER_SEC);
}
  1. 創建一個用於首次運行時間的全局const參數(當前時間)。
  2. 如果30秒+全局<當前時間->點擊返回的值或執行您想做的一切)

也許這是要輸入遞歸的if語句

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM