Пример #1
0
int main( void )
{
    printf( "%d\n", factorial( 5 ) );
    return 0;
}
Пример #2
0
void vals_long(const char *str, long int *value)
{
	puts(str);
	/*     Elementry Evaluator (long)
	 *   ==============================
	 *	This function evaluates, the long
	 *  int value of an exprssion element.
	 *  Operations ~,!,!! and factorial are carried
	 *  out here. Function calls are made and
	 *  values of variables are retrieved too.
	 *  ---------------------------------------
	 *   Note: *str sould be trimmed of spaces
	 *   before being passed here.
	 */
	*value = 0;

	/* Out the 4 operations (!,!!,~,fact),
	 * only 3 operations ~,! and !! appear
	 * on L.H.S. of str, where ! and !! can
	 * never coincide, but ~ can coincide with
	 * the other two. These operations can be
	 * permuted in the following ways:
	 * ----------------------
	 *  nothing    -> 0
	 *  !  only    -> 1
	 *  !! only    -> 2
	 *  ~  only    -> 3
	 *  ~ and !    -> 4
	 *  ~ and !!   -> 5
	 * - - - - - - - - - - - -
	 * in case factorial needs
	 * to be found then add 10:
	 * - - - - - - - - - - - -
	 *  nothing    -> 10
	 *  !  only    -> 11
	 *  !! only    -> 12
	 *  ~  only    -> 13
	 *  ~ and !    -> 14
	 *  ~ and !!   -> 15
	 * ----------------------
	 * The number is kept in ops.
	 */
	unsigned char ops = 0;

	/* now find ops */
	if(*str=='~'||*str=='!') {
		if(*str++=='~') {
			ops += 3;
			if(*str++=='!') {
				ops++;
				if(*str=='!') ops++;
				else str--;
			} else str--;
		} else { /* (*str++=='!') */
			ops++;
			if(*str++=='!') ops++;
			else str--;
		}
	} if(str[strlen(str)-1]=='!')
		ops += 10;

	/* extract the value */
	if(!strncmp(str,"0x",2))
		sscanf(str,"%lx",value);
	else if((*str>='0'&&*str<='9')||
	(*str=='-'&&str[1]>='0'&&str[1]<='9'))
		*value = atol(str);
	else {
		/* get variable value */
		*value = (getint(str)==NULL) ? 0:*getint(str);
		if(ops==1||ops==2||ops==4||ops==5||
		ops==11||ops==12||ops==14||ops==15) {
			/* nullify variable */
			long int *tmp;
			tmp = getint(str);
			if(tmp!=NULL)
				*tmp = 0;
		}
		else if(ops==2||ops==5||
		       ops==12||ops==15) {
			/* destroy variable */
		}
	}
	/* find factorial & complement */
	if(ops>=10) {
		*value = factorial(*value);
		ops -= 10;
	} if(ops>=3)
		*value = ~*value;
}
Пример #3
0
int main(int argc, char *argv[]){
	int size = atoi(argv[1]);
	double result = factorial(2 * size) / pow(factorial(size),2);
	printf("There are %.0f paths\n", result);
	return 0;
}
Пример #4
0
int main(int argn, char** argv) {

  int base;
  std::vector<char> digits;
  
  if (argn < 2) {
    std::cerr << "no parameters" << std::endl;
    return -1;
  }
  base = atoi(argv[1]);
  std::cout << "base " << base << std::endl;

  char digit;
  for (size_t i = 2; i < argn; i++) {
    digits.push_back(argv[i][0]);
  }

  std::cout << "digits: ";
  for (size_t i = 0; i < digits.size(); ++i) {
    std::cout << digits[i] << " ";
  }
  std::cout << std::endl;

  // number of unique arrangements of the digits
  const int num_unique = digits.size();
  const int n_factorial = factorial(num_unique);
  std::cout << num_unique << "! = " << n_factorial << std::endl;
 
  const int max_num = 7;

  // find the number of number that could go inbetween the digits
  // this number times the n_factorial is the total number of combinations
  // at the given length of digits

  // how many ways are there to add num + 1 numbers to 7 - num?
  // e.g. 7 - 3 = 4
  // these are the combinations where all the non-unique digits are 
  // in front, in the middle, or at the end
  // 4 + 0 + 0 + 0 
  // 0 + 4 + 0 + 0 
  // 0 + 0 + 4 + 0 
  // 0 + 0 + 0 + 4

  // 3 + 1 + 0  and all permutations
  // 2 + 2 + 0
  // 2 + 1 + 1
  // 1 + 1 + 1 + 1
  // and all permutations of those

  
  // for the first step there is no room for non-unique digits, so
  // the answer is n_factorial
  // at the next step there is room for a single non-unique digit that
  // can be positioned at any of num_unique + 1 positions, so the answer
  // is ( num_unique + 1 ) * n_factorial
  // at the the next step there are are sum( 1:(num_unique + 1) )
  // ..uuuu - 5 of these
  // .u.uuu - 4 of these
  // .uu.uu - 3 of these
  // .uuu.u - 2 of these
  // .uuuu. - 1 of these
  // then
  // ...uuuu - 5 of these
  // ..u.uuu - 4
  // 3
  // 2
  // 1
  // but there area two patterns ..u. and .u..  - 3 patterns
  // 
  // For four non-uniques
  // ...., ...u. x 4, ...uu. x 4  ( num_unique * 4) ..
  for (size_t i = num_unique; i < max_num; i++) {
    
  }

  return 0;
}
Пример #5
0
double gamma_function(int xx)
{
	return factorial(xx - 1);
}
TEST(factorialTest,positiveNos){
    EXPECT_EQ(24,factorial(4));
    EXPECT_EQ(120,factorial(5));
    EXPECT_EQ(720,factorial(6));
}
Пример #7
0
/* ************************************************************************** */
int factorial(const int n)
{
   return ((n == 1 || n == 0) ? 1 : factorial(n - 1) * n);
}
Пример #8
0
float euler(int n){
float s=0;
   for(int i=1; i<=n; i++)
      s+=1/factorial(x);
return s;
}
Пример #9
0
// test cases
int main(int argc, char** argv)
{
    
    unsigned char bytes1[] = {35, 67, 234, 147};
    unsigned char bytes2[] = {87, 187, 221, 254, 123, 23};

    VTBignum bignum1 = VTBignum::fromByteArray(bytes1, 4, 0);
    VTBignum bignum2 = VTBignum::fromByteArray(bytes2, 6, 0);

    // test 1
    VTBignum bignum_result = bignum1 + bignum2;

    unsigned char* bytes_result = (unsigned char*) malloc(bignum_result.size());
    bignum_result.toByteArray(bytes_result);

    // test 2
    VTBignum bignum_result2 = bignum2 + bignum1;

    unsigned char* bytes_result2 = (unsigned char*) malloc(bignum_result2.size());
    bignum_result2.toByteArray(bytes_result2);

    assert(bignum_result == bignum_result2);

    assert( VTBignum::fromInt(0) == VTBignum() );

    assert( VTBignum::fromInt(-20000) < VTBignum::fromInt(20000) );
    assert( VTBignum::fromInt(20000) < VTBignum::fromInt(-20000) == false );
    assert( VTBignum::fromInt(20000) < VTBignum::fromInt(20000) == false );
    assert( VTBignum::fromInt(21231) <= VTBignum::fromInt(21231) );
    assert( VTBignum::fromInt(3245) <= VTBignum::fromInt(3246) );
    assert( VTBignum::fromInt(1) <= VTBignum::fromInt(1234556) );
    assert( VTBignum::fromInt(45634564) == VTBignum::fromInt(45634564) );

    test_plus(-515, -495, -1010);
    test_plus(515, 495, 1010);
    test_plus(515, -495, 20);
    test_plus(-515, 495, -20);
    test_plus(495, -515, -20);
    test_plus(-495, 515, 20);
    test_plus(-495, 495, 0);
    test_plus(495, -495, 0);
    test_plus(0, 0, 0);
    test_plus(0, 1234, 1234);
    test_plus(123, 0, 123);

    test_minus(515, 495, 20);
    test_minus(495, 515, -20);
    test_minus(515, -495, 1010);
    test_minus(-495, 515, -1010);
    test_minus(-515, -495, -20);
    test_minus(-495, -515, 20);
    test_minus(515, 515, 0);
    test_minus(-515, -515, 0);
    test_minus(0, 0, 0);
    test_minus(0, 1234, -1234);
    test_minus(123, 0, 123);

    test_mult(500, 500, 250000);
    test_mult(500, -500, -250000);
    test_mult(-500, 500, -250000);
    test_mult(-500, -500, 250000);
    test_mult(500, 1, 500);
    test_mult(1, 500, 500);
    test_mult(0, 500, 0);
    test_mult(500, 0, 0);
    test_mult(0, 0, 0);

    long long a = 2147483647;
    a += 1;
    a *= a;
    a *= 2;
    a -= 1;

    VTBignum res1 = VTBignum::fromLongLong(2147483647) + VTBignum::fromInt(1);
    VTBignum res = res1 * res1 * VTBignum::fromInt(2) - VTBignum::fromInt(1);
    long long b = res.toLongLong();

    assert(a == b);
    assert( VTBignum::fromLongLong(-2147483647).toLongLong() == -2147483647 );
    assert( VTBignum::fromLongLong(0).toLongLong() == 0 );

    assert( VTBignum::fromString( "123" ) == VTBignum::fromLongLong(123) );
    assert( VTBignum::fromString( "+12345678" ) == VTBignum::fromLongLong(12345678) );

    VTBignum frstr = VTBignum::fromString( "-12345678" );
    VTBignum frll = VTBignum::fromLongLong(-12345678);
    assert( VTBignum::fromString( "-12345678" ) == VTBignum::fromLongLong(-12345678) );

    // 2^64 == 2^16 * 2^16 * 2^16 * 2^16
    assert( VTBignum::fromString( "18446744073709551616" ) 
        == VTBignum::fromInt(65536) * VTBignum::fromInt(65536) * VTBignum::fromInt(65536) * VTBignum::fromInt(65536) );

    assert( ++VTBignum::fromInt(10000) == VTBignum::fromInt(10001) );
    assert( VTBignum::fromInt(10000)++ == VTBignum::fromInt(10000) );
    assert( --VTBignum::fromInt(10000) == VTBignum::fromInt(9999) );
    assert( VTBignum::fromInt(10000)-- == VTBignum::fromInt(10000) );

    VTBignum pow10 = VTBignum::fromInt(2).pow(10);
    assert( pow10 == VTBignum::fromInt(1024) );

    int d = 100000;
    printf("%d\n", d);
    printf("base 256: %s\n", VTBignum::fromInt(d).toString(256).c_str());
    printf("base  10: %s\n", VTBignum::fromInt(d).toString(10).c_str());
    printf("base  16: %s\n", VTBignum::fromInt(d).toString(16).c_str());
    printf("base  25: %s\n", VTBignum::fromInt(d).toString(35).c_str());
    printf("All good\n");
    
    

    long long fact = 1000;
    VTBignum factbig = factorial(fact);
    printf("Factorial %lld size: %d\n", fact, factbig.size());

    getchar();
    return 0;
}
Пример #10
0
uint64_t factorial(int n){
    if (n<=1){
        return 1;
    }
    return n*factorial(n-1);
}
Пример #11
0
int EulerUtility::factorial(int n) 
{
	if (n == 0)
		return 1;
	return n * factorial(n - 1);
}
Пример #12
0
int main()
{
    char seguir='s';
    int opcion=0;
    int num1=0, num2=0, flag=0;

           do{
        printf("\n1- Ingresar 1er operando (A=%d)\n",num1);
        printf("2- Ingresar 2do operando (B=%d)\n",num2);
        printf("3- Calcular la suma (A+B)\n");
        printf("4- Calcular la resta (A-B)\n");
        printf("5- Calcular la division (A/B)\n");
        printf("6- Calcular la multiplicacion (A*B)\n");
        printf("7- Calcular el factorial (A!)\n");
        printf("8- Calcular todas las operacione\n");
        printf("9- Salir\n");

        scanf("%d",&opcion);

        switch(opcion)
        {
            case 1:
                system("cls");
                printf("Ingrese numero");
                scanf("%d",&num1);
                flag = 1;
                break;
            case 2:
                system("cls");
                printf("Ingrese numero");
                scanf("%d",&num2);
                flag = 1;
                break;
            case 3:
                if(flag == 0)
                {
                    system("cls");
                    printf("Por favor ingrese los numeros primero");
                }
                else{
                system("cls");
                suma(num1,num2);
                }
                break;
            case 4:
                if(flag == 0)
                {
                    system("cls");
                    printf("Por favor ingrese los numeros primero");
                }
                else{
                system("cls");
                resta(num1,num2);
                }
                break;
            case 5:
                if(flag == 0)
                {
                    system("cls");
                    printf("Por favor ingrese los numeros primero");
                }
                else{
                system("cls");
                if(num2 == 0)
                {
                    printf("Error, no se puede dividir por cero,reingrese numero");
                    scanf("%d",&num2);
                }
                division(num1,num2);
                }
                break;
            case 6:
                if(flag == 0)
                {
                    system("cls");
                    printf("Por favor ingrese los numeros primero");
                }
                else{
                system("cls");
                multiplicacion(num1,num2);
                }
                break;
            case 7:
           if(flag == 0)
                {
                    system("cls");
                    printf("Por favor ingrese los numeros primero");
                }
                else{
                     factorial(num1);
                }
                break;
            case 8:
                if(flag == 0)
                {
                    system("cls");
                    printf("Por favor ingrese los numeros primero");
                }
                else{
            suma(num1,num2);
            resta(num1,num2);
            division(num1,num2);
            multiplicacion(num1,num2);
                }
                break;
            case 9:
                seguir = 'n';
                break;
        }


        }while(seguir == 's');

        system("pause");
    return 0;
}
Пример #13
0
int ExtraMath::combination(int i, int n) {
  return factorial(n) /
            (factorial(i) * factorial(n - i));
}
Пример #14
0
/* recursive function to get the factorial of a number */
int factorial(unsigned x) {
	return x == 0 ? 1 : x * factorial(x-1);
}
Пример #15
0
// Coeficiente binomial
int coefBinomial(int m, int n) {
	int coef;
	coef = (factorial(m))/(factorial(n)*factorial(m - n));
	return coef;
}
Пример #16
0
int factorial(int x) {
    return (x==1) ? 1 : x*factorial(x-1);
}
TEST(factorialTest,zeros){
    EXPECT_EQ(1,factorial(0));
}
Пример #18
0
int main(int argc, char **argv) {
    int i,j,maxn,maxm;
    const char str[]="../test/2/input20.txt";//14 15
    char bool_show=1;
    freopen(str,"rt",stdin);
    scanf("%d%d\n",&maxn,&maxm);
    char **map;
    map=(char **)malloc(maxn*sizeof(char *));
    for (i=0;i<maxn;i++) map[i]=(char *)malloc(maxm*sizeof(char));
    ELEM_POINT *Gold, *Monster, *Player;
    int cgolds=0,cmonsters=0,cplayers=0;
    for (i=0;i<maxn;i++) {
        char *string=(char *)malloc((maxm+2)*sizeof(char));
        gets(string);
        for (j=0;j<maxm;j++) {
            map[i][j]=string[j];
            if (map[i][j]>='0' && map[i][j]<='9') cplayers++;
            if (map[i][j]=='*') cgolds++;
            if (map[i][j]=='@') cmonsters++;
        }
    }
    fclose(stdin);
    Player=(ELEM_POINT *)malloc(cplayers*sizeof(ELEM_POINT));
    Gold=(ELEM_POINT *)malloc(cgolds*sizeof(ELEM_POINT));
    Monster=(ELEM_POINT *)malloc(cmonsters*sizeof(ELEM_POINT));
    int *score=(int *)malloc(cplayers*sizeof(int));
    for (i=0;i<cplayers;i++) score[i]=0;
    int numbergame;
    for (numbergame=0;numbergame<factorial(cplayers);numbergame++) {
        printf("Number of game = %d\n",numbergame);
        //usleep(3*1000*1000);

        int move=0,indexgold,indexmonster,indexplayer;

        //set position
        int iteration=-1;
        int number_of_iteration=0;
        int *massive_iteration=(int *)malloc(cplayers*sizeof(int)) ;
        while (iteration!=numbergame) {
            iteration++;
            while (1) {
                int index_of_iteration, tmp_number_of_iteration=number_of_iteration;
                for (index_of_iteration=cplayers-1;index_of_iteration>=0;index_of_iteration--) {
                    massive_iteration[index_of_iteration]=tmp_number_of_iteration%cplayers;
                    tmp_number_of_iteration/=cplayers;
                }
                int flagin=1;
                for (i=0;i<cplayers-1;i++) {
                    for (j=i+1;j<cplayers;j++) {
                        if (massive_iteration[i]==massive_iteration[j]) {
                            flagin=0;
                            break;
                        }
                    }
                    if (flagin==0) break;
                }
                number_of_iteration++;
                if (flagin==1) break;
            }
        }

        freopen(str,"rt",stdin);
        scanf("%d%d\n",&maxn,&maxm);
        int cgolds=0,cmonsters=0,cplayers=0;
        for (i=0;i<maxn;i++) {
            for (j=0;j<maxm;j++) {
                map[i][j]='.';
            }
        }

        for (i=0;i<maxn;i++) {
            char *string=(char *)malloc((maxm+2)*sizeof(char));
            gets(string);
            for (j=0;j<maxm;j++) {
                map[i][j]=string[j];
                if (map[i][j]>='0' && map[i][j]<='9') {
                    cplayers++;
                    Player[ map[i][j]-'0' ].y=i;
                    Player[ map[i][j]-'0' ].x=j;
                    Player[ map[i][j]-'0' ].alive=map[i][j];
                }
                if (map[i][j]=='*') cgolds++;
                if (map[i][j]=='@') cmonsters++;
            }
        }
        fclose(stdin);

        for (i=0;i<cplayers;i++) {
            map[Player[i].y][Player[i].x]=Player[massive_iteration[i]].alive;
        }
        free(massive_iteration);

        while (1) {
            cgolds=0;
            cmonsters=0;
            indexgold=0;
            indexmonster=0;
            indexplayer=0;
            system("clear");
            for (i=0;i<maxn;i++) {
                for (j=0;j<maxm;j++) {

                    if (bool_show) {


                        if (map[i][j]>='0' && map[i][j]<='9') printf("\033[33m%c",map[i][j]);
                        if (map[i][j]=='#') printf("\033[35m%c",map[i][j]);
                        if (map[i][j]=='*') printf("\033[31m%c",map[i][j]);
                        if (map[i][j]=='@') printf("\033[36m%c",map[i][j]);
                        if (map[i][j]=='.') printf("\033[37m%c",map[i][j]);//37
                        printf("\033[37m");

                        //printf("%c",map[i][j]);
                    }
                    if (map[i][j]>='0' && map[i][j]<='9') {
                        Player[ map[i][j]-'0' ].y=i;
                        Player[ map[i][j]-'0' ].x=j;
                        Player[ map[i][j]-'0' ].alive=map[i][j];
                    }
                    if (map[i][j]=='*') {
                        Gold[indexgold].y=i;
                        Gold[indexgold].x=j;
                        Gold[indexgold].alive=1;
                        indexgold++;
                    }
                    if (map[i][j]=='@') {
                        Monster[indexmonster].y=i;
                        Monster[indexmonster].x=j;
                        Monster[indexmonster].alive=1;
                        indexmonster++;
                    }
                }
                if (bool_show) printf("\n");
            }
            //if (move==0) usleep(1000*1000);
            cgolds=indexgold;
            cmonsters=indexmonster;
            if (move>=10000 || cgolds==0) break;

            int command;
            clock_t time;

            printf("Players think ");
            //0
            if (cplayers>-1) {
                int isGold=0;
                for (i=0;i<maxn;i++) {
                    for (j=0;j<maxm;j++) {
                        if (map[i][j]=='*') {
                            isGold=1;
                            break;
                        }
                    }
                    if (isGold) break;
                }
                if (isGold) {
                    indexplayer=0;
                    time=clock();
                    get_player1(map,maxn,maxm,Player[indexplayer].y,Player[indexplayer].x,&command);
                    time = clock()-time;
                    printf("%3.0f ms ", 1000*(double)time/CLOCKS_PER_SEC);
                    if (1000*(double)time/CLOCKS_PER_SEC<1000) {
                        switch (command) {
                            case 0: break;
                            case 1: if (Player[indexplayer].x>0 && map[Player[indexplayer].y][Player[indexplayer].x-1]!='#') Player[indexplayer].x--; break;
                            case 2: if (Player[indexplayer].y>0 && map[Player[indexplayer].y-1][Player[indexplayer].x]!='#') Player[indexplayer].y--; break;
                            case 3: if (Player[indexplayer].x<maxm-1 && map[Player[indexplayer].y][Player[indexplayer].x+1]!='#') Player[indexplayer].x++; break;
                            case 4: if (Player[indexplayer].y<maxn-1 && map[Player[indexplayer].y+1][Player[indexplayer].x]!='#') Player[indexplayer].y++; break;
                            default : break;
                        }
                    }
                    updatemap(map,maxn,maxm,Monster,cmonsters,Gold,cgolds,Player,cplayers,indexplayer,score);
                }
            }
            //1
            if (cplayers>0) {
                int isGold=0;
                for (i=0;i<maxn;i++) {
                    for (j=0;j<maxm;j++) {
                        if (map[i][j]=='*') {
                            isGold=1;
                            break;
                        }
                    }
                    if (isGold) break;
                }
                if (isGold) {
                    indexplayer=1;
                    time=clock();
                    get_player2(map,maxn,maxm,Player[indexplayer].y,Player[indexplayer].x,&command);
                    time = clock()-time;
                    printf("%3.0f ms ", 1000*(double)time/CLOCKS_PER_SEC);
                    if (1000*(double)time/CLOCKS_PER_SEC<1000) {
                        switch (command) {
                            case 0: break;
                            case 1: if (Player[indexplayer].x>0 && map[Player[indexplayer].y][Player[indexplayer].x-1]!='#') Player[indexplayer].x--; break;
                            case 2: if (Player[indexplayer].y>0 && map[Player[indexplayer].y-1][Player[indexplayer].x]!='#') Player[indexplayer].y--; break;
                            case 3: if (Player[indexplayer].x<maxm-1 && map[Player[indexplayer].y][Player[indexplayer].x+1]!='#') Player[indexplayer].x++; break;
                            case 4: if (Player[indexplayer].y<maxn-1 && map[Player[indexplayer].y+1][Player[indexplayer].x]!='#') Player[indexplayer].y++; break;
                            default : break;
                        }
                    }
                    updatemap(map,maxn,maxm,Monster,cmonsters,Gold,cgolds,Player,cplayers,indexplayer,score);
                }
            }
            /*
            //2
            if (cplayers>0) {
                int isGold=0;
                for (i=0;i<maxn;i++) {
                    for (j=0;j<maxm;j++) {
                        if (map[i][j]=='*') {
                            isGold=1;
                            break;
                        }
                    }
                    if (isGold) break;
                }
                if (isGold) {
                    indexplayer=2;
                    time=clock();
                    get_player3(map,maxn,maxm,Player[indexplayer].y,Player[indexplayer].x,&command);
                    time = clock()-time;
                    printf("%3.0f ms ", 1000*(double)time/CLOCKS_PER_SEC);
                    if (1000*(double)time/CLOCKS_PER_SEC<1000) {
                        switch (command) {
                            case 0: break;
                            case 1: if (Player[indexplayer].x>0 && map[Player[indexplayer].y][Player[indexplayer].x-1]!='#') Player[indexplayer].x--; break;
                            case 2: if (Player[indexplayer].y>0 && map[Player[indexplayer].y-1][Player[indexplayer].x]!='#') Player[indexplayer].y--; break;
                            case 3: if (Player[indexplayer].x<maxm-1 && map[Player[indexplayer].y][Player[indexplayer].x+1]!='#') Player[indexplayer].x++; break;
                            case 4: if (Player[indexplayer].y<maxn-1 && map[Player[indexplayer].y+1][Player[indexplayer].x]!='#') Player[indexplayer].y++; break;
                            default : break;
                        }
                    }
                    updatemap(map,maxn,maxm,Monster,cmonsters,Gold,cgolds,Player,cplayers,indexplayer,score);
                }
            }
            //3
            if (cplayers>1) {
                int isGold=0;
                for (i=0;i<maxn;i++) {
                    for (j=0;j<maxm;j++) {
                        if (map[i][j]=='*') {
                            isGold=1;
                            break;
                        }
                    }
                    if (isGold) break;
                }
                if (isGold) {
                    indexplayer=3;
                    time=clock();
                    get_player4(map,maxn,maxm,Player[indexplayer].y,Player[indexplayer].x,&command);
                    time = clock()-time;
                    printf("%3.0f ms ", 1000*(double)time/CLOCKS_PER_SEC);
                    if (1000*(double)time/CLOCKS_PER_SEC<1000) {
                        switch (command) {
                            case 0: break;
                            case 1: if (Player[indexplayer].x>0 && map[Player[indexplayer].y][Player[indexplayer].x-1]!='#') Player[indexplayer].x--; break;
                            case 2: if (Player[indexplayer].y>0 && map[Player[indexplayer].y-1][Player[indexplayer].x]!='#') Player[indexplayer].y--; break;
                            case 3: if (Player[indexplayer].x<maxm-1 && map[Player[indexplayer].y][Player[indexplayer].x+1]!='#') Player[indexplayer].x++; break;
                            case 4: if (Player[indexplayer].y<maxn-1 && map[Player[indexplayer].y+1][Player[indexplayer].x]!='#') Player[indexplayer].y++; break;
                            default : break;
                        }
                    }
                    updatemap(map,maxn,maxm,Monster,cmonsters,Gold,cgolds,Player,cplayers,indexplayer,score);
                }
            }
            */

            printf("\nScores : ");
            //results
            for (indexplayer=0;indexplayer<cplayers;indexplayer++) {
                printf("%d | ",score[indexplayer]);
            }

            //monsters
            for (indexmonster=0;indexmonster<cmonsters;indexmonster++) {
                command = get_monster(map,maxn,maxm,Monster[indexmonster].y,Monster[indexmonster].x);
                switch (command) {
                    case 0: break;
                    case 1: Monster[indexmonster].x--; break;
                    case 2: Monster[indexmonster].y--; break;
                    case 3: Monster[indexmonster].x++; break;
                    case 4: Monster[indexmonster].y++; break;
                    default : break;
                }
                updatemap2(map,maxn,maxm,Monster,cmonsters,Gold, cgolds, Player, cplayers, score);
            }
            printf("move = %d\n",move);
            usleep(timesleep*1000);
            move++;
        }
        for (indexplayer=0;indexplayer<cplayers;indexplayer++) {
            printf("Player %d : scores %d\n",indexplayer,score[indexplayer]);
        }
        printf("move = %d\n",move);
    }
    free(Player);
    free(Gold);
    free(Monster);
    for (i=0;i<maxn;i++) free(map[i]);
    free(map);


    //RATING ELO
    typedef struct profile {
        char name[100];
        int rating;
    } profile;
    profile data[10], *P=(profile *)malloc(cplayers*sizeof(profile));
    int cdata=0;
    //current players
    freopen("../test/cur_game.txt","rt",stdin);
    for (i=0;i<cplayers;i++) {
        scanf("%s\n",P[i].name);
    }
    fclose(stdin);
    freopen("../test/rating.txt","rt",stdin);
    int *ind=(int *)malloc(cplayers*sizeof(int)); //indexs of players in data
    while (1) {
        char st[100];
        scanf("%s\n",st);
        st[(int)strlen(st)]='\0';
        if (!strcmp(st,"0")) break;
        int rate;
        scanf("%d\n",&rate);
        for (i=0;i<(int)strlen(st)+1;i++) data[cdata].name[i]=st[i];
        data[cdata].rating=rate;
        for (i=0;i<cplayers;i++) {
            if (!strcmp(data[cdata].name,P[i].name)) {
                ind[i]=cdata;
            }
        }
        cdata++;
    }
    fclose(stdin);
    printf("BEFORE : \n");
    double *addition=(double *)malloc(cplayers*sizeof(double));
    for (i=0;i<cplayers;i++) {
        addition[i]=0;
    }
    for (i=0;i<cplayers-1;i++) {
        for (j=i+1;j<cplayers;j++) {
            double x= (score[i]>score[j]) ? 1.0 : (score[i]==score[j]) ? 0.5 : 0.0;
            double E1=1.0/(1.0+pow(10.0,(double)(data[ind[j]].rating-data[ind[i]].rating)/400.0)),
                    E2=1.0/(1.0+pow(10.0,(double)(data[ind[i]].rating-data[ind[j]].rating)/400.0));
            addition[i]+=x-E1;
            addition[j]+=1.0-x-E2;
        }
    }

    for (i=0;i<cplayers;i++) {
        data[ind[i]].rating+=(int)(30.0*addition[i]);
    }
    free(P);
    free(addition);


    //HISTORY OF GAMES
    char history[1000][100];
    int indexhistory=0,chistory=0;
    freopen("../test/result_games.txt","rt",stdin);
    while (1) {
        gets(history[indexhistory]);
        if (!strcmp(history[indexhistory],"\0")) break;
        indexhistory++;
    }
    chistory=indexhistory;
    fclose(stdin);
    freopen("../test/result_games.txt","wt",stdout);
    for (indexhistory=0;indexhistory<chistory;indexhistory++) {
        printf("%s\n",history[indexhistory]);
    }
    int indexplayer;
    printf("%s\n",str);
    for (indexplayer=0;indexplayer<cplayers;indexplayer++) {
        printf("%d player = %d\n",indexplayer,score[indexplayer]);
    }
    fclose(stdout);
    free(score);

    printf("AFTER : \n");
    for (i=0;i<cdata;i++) {
        printf("%10s %4d\n",data[i].name,data[i].rating);
    }

    freopen("../test/rating.txt","wt",stdout);
    for (i=0;i<cdata;i++) {
        printf("%s\n%d\n",data[i].name,data[i].rating);
    }
    printf("0");
    fclose(stdout);

    return 0;
}
long Math_Utility::factorial(int n)
{
  return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
}
Пример #20
0
int ncr(int n,int r)
{ 
  return factorial(n)/((factorial(r))*(factorial(n-r)));    
}
Пример #21
0
void * combination(void * param)
{
	struct arguments  * arg = param;
	result = factorial(arg->n)/(factorial(arg->k)*factorial(arg->n - arg->k));
    	pthread_exit(NULL);
}
Пример #22
0
unsigned factorial(unsigned n) {
    if (n == 1) {
        return 1;
    }
    return n * factorial(n - 1);
}
Пример #23
0
int main()
{
    char seguir='s';
    int opcion=0;
    float x; // numero A
    float y; // numero B
    unsigned short int flagA=0; // Bnadera de ingreso variable x
    unsigned short int flagB=0; // Bandera de ingreso variable y
    while(seguir=='s')
    {
        printf("1- Ingresar 1er operando (A=x)\n");
        printf("2- Ingresar 2do operando (B=y)\n");
        printf("3- Calcular la suma (A+B)\n");
        printf("4- Calcular la resta (A-B)\n");
        printf("5- Calcular la division (A/B)\n");
        printf("6- Calcular la multiplicacion (A*B)\n");
        printf("7- Calcular el factorial (A!)\n");
        printf("8- Calcular todas las operaciones\n");
        printf("9- Salir\n");
        printf("Ingrese la opcion-->");
        scanf("%d",&opcion);
        system("cls");
        switch(opcion)
        {
            case 1:
                printf("Ingrese Un numero A: ");
                scanf("%f",&x); //Se ingresa numero A en x
                flagA=1; // Bandera A de ingreso de dato en 1
                break;
            case 2:
                printf("Ingrese Un numero B: ");
                scanf("%f",&y);//Se ingresa numero B en x
                flagB=1; // Bandera B de ingreso de dato en 1
                break;
            case 3:
                if(validarIngreso(flagA,flagB))
                {
                    printf("La suma de A+B es:%.2f\n",sumar(x,y));
                }
                break;
            case 4:
                if(validarIngreso(flagA,flagB))
                {
                    printf("La resta de A-B es:%.2f\n",restar(x,y));
                }
                break;
            case 5:
                if(validarIngreso(flagA,flagB))
                {
                    if( y != 0 )    //Se verifica que el dividendo sea diferente de cero
                    {
                        printf("La Division de A/B es:%.2f\n",dividir(x,y));
                    }
                    else
                    {
                        printf("ERROR, B tiene que ser distinto de 0\n");
                    }
                }
                break;
            case 6:
                if(validarIngreso(flagA,flagB))
                {
                    printf("La Division de A*B es:%.2f\n",multiplicar(x,y));
                }
                break;
            case 7:
                if(validarFactorial(x,flagA))//verifica si el dato ingresado es correcto
                {
                    printf("El Factorial de  %.0f! es:  %ld\n",x,factorial(x));
                }
                break;
            case 8:
                if(flagA) //Si se encuentra el dato A ingresado
                {
                    if(validarFactorial(x,flagA)) //Verifica si el dato ingresado es correcto
                    {
                       printf("El Factorial de  %.0f! es:  %lld\n",x,factorial(x));

                    }
                }

                if(validarIngreso(flagA,flagB))
                {
                    printf("La suma de A+B es:%.2f\n",sumar(x,y));
                    printf("La resta de A-B es:%.2f\n",restar(x,y));
                    if( y != 0 )
                    {
                        printf("La Division de A/B es:%.2f\n",dividir(x,y));
                    }
                    else
                    {
                        printf("ERROR, B tiene que ser distinto de 0\n");

                    }
                printf("La Division de A*B es:%.2f\n",multiplicar(x,y));
                }

                break;
            case 9:
                seguir = 'n';
                break;
            default:
                printf("La opcion ingresada no es correcta\n");
                break;
        }
        system("pause");
        system("cls");
    }
    return 0;
}
Пример #24
0
real choose(int n, int c) {
    return (real)factorial(n) / (real)(factorial(c) * (real)factorial(n-c));
}
Пример #25
0
int factorial(int n) {
	if ( n == 1) return 1;
	return n * factorial(n-1);
}
Пример #26
0
int factorial(int n) {
    if (n <= 0){
        return 1;
    }
    return n * factorial(n-1);
}
Пример #27
0
int factorial(int n){
	if(n!=1){
		return (n*factorial(n-1));
	}
	else return 1;
}
Пример #28
0
int main() {
  int f = factorial(5);

  std::cout << "factorial(5) = " << f << std::endl;
}
Пример #29
0
int main(){
	int valor;
	valor = factorial(5);
	printf("%d\n", valor);
}
Пример #30
0
void calculation(char *buffer,char *answer) {
	int i, j;
	long int f;
	double a, b, k;
	char temp1[BUFLEN],temp2[BUFLEN];
	int size;
	printf("Received: %s\n", buffer);
	bzero(temp1, BUFLEN);
	bzero(temp2, BUFLEN);
	for (i = 1; i < 255; i++) {
		//factorial
		if (buffer[i] == '!'){
			strncpy(temp1, buffer, i);
			f = atoi(temp1);
			printf("f = %d\n", f);
			break;
		}
		//sqrt
		if (buffer[i] == '#'){
			strncpy(temp1, buffer, i);
			a = atof(temp1);
			printf("k = %f\n", a);
			break;
		}
		// *, /, +,-
		if ((buffer[i] == '*') ||(buffer[i] == '/') ||(buffer[i] == '+') || (buffer[i] == '-'))  {
			strncpy(temp1, buffer, i);
			a = atof(temp1);
			printf("a = %f\n", a);
			break;
		}
	}
	//end expression
	for (j = 0; j < BUFLEN-1; j++) {
		if (buffer[j] == '=') {
			strncpy(temp2, buffer + i + 1, j - i - 1);
			b = atof(temp2);
			printf("b = %f\n", b);
			break;
		}
	}
	if (buffer[i] == '+'){
		Sleep(10000);
		sprintf(answer, "Answer = %f", a+b);
	}
	if (buffer[i] == '-'){
		sprintf(answer, "Answer = %f", a-b);
	}
	if (buffer[i] == '*'){
		sprintf(answer, "Answer = %f", a*b);
	}
	if (buffer[i] == '/'){
		if (b==0){
			sprintf(answer, "Error, input divider != 0 !\n");
		}
		else {
			sprintf(answer, "Answer = %f", a/b);
		}
	}
	if (buffer[i] == '#'){
		if (a<0){
			sprintf(answer, "Error, input Root >= 0 !\n");
		}
		else {
			sprintf(answer, "Sqrt = %f", sqrt(a));
		}
	}
	if (buffer[i] == '!'){
		if (f<=0){
			sprintf(answer, "Error, input Factorial > 0 !\n");
		}
		else {
			sprintf(answer, "Factorial = %d", factorial(f));
		}
	}
}