2

反復的で再帰的な組み合わせ関数を書くように求める宿題の問題があります。それらを特定のプログラムに配置して、どちらがより長くかかるかを確認します。

反復関数に問題があります。私はそれを何度か調べましたが、mach-o-linker エラーが発生し続けています。さまざまな方法で変数を特定しようとしましたが、まだうまくいきません。

このトピックに関するヘルプをいただければ幸いです。イテレータ関数またはおそらく階乗関数に問題があると思いますが、今のところそれを見ることができません。

ありがとうございます

#include <iostream>
#include <sys/time.h>
#include <cstdlib>
using std::cout;
using std::endl;

double iR;
double iN;

typedef unsigned int uint;

uint Factorial(uint n)
  {
  if (n == 0) return 1;
   if (n <= 2) return n;
    else return n * Factorial(n - 1);
  }

  double combination_recursive(double iN, double iR);
  double combination_iterative(int iN, int iR);



  int main(int argc, const char * argv[]) {


typedef struct timeval time;
time stop, start;
gettimeofday(&start, NULL);

iN = 20.0;
iR = 3.0;


  combination_iterative(iN, iR);


gettimeofday(&stop, NULL);
if(stop.tv_sec > start.tv_sec)
    cout << "Seconds: " << stop.tv_sec-start.tv_sec << endl;
  else
    cout << "Micro: " << stop.tv_usec-start.tv_usec << endl;
  return 0;
}

double comination_iterative(int, int) {

  if (iN == iR) { return 1;}
   if (iR == 0 && iN!= 0) { return 1;}
  else return (iN * Factorial(iN-1))/Factorial(iN-1)*Factorial(iN-iR);

 }

double combination_recursive(double iN, double iR) {
  if (iR < 0 || iR > iN) {
      return 0;
}
  if (iR < 1) {
    return 1;
}
  if (iN == iR) {
    return 1;
}
return combination_recursive(iN - 1, iR) + combination_recursive(iN - 1, iR - 1);
}
4

2 に答える 2