int test_ex_1_1_18(int argc, const char * argv[]) {
    int a = 2, b = 25, m = 3, n = 11;;
    printf("%d\n", mystery(a, b));
    printf("%d\n", mystery(m, n));
    
    printf("%d\n", mystery2(a, b));
    printf("%d\n", mystery2(m, n));
    
    return 0;
}
/*
 a的指数增加方式(偶数时增加一倍,奇数时增加一倍,再加1),类似于b的递减(偶数时减少一半,奇数时减1后,再减少一半),所以最终mystery2(a, b) = a ^ b
 */
int mystery2(int a, int b) {
    if (b == 0) {
        return 1;
    }
    
    if (b % 2 == 0) {
        return mystery2(a * a, b / 2);
    }
    
    return mystery2(a * a, b / 2) * a;
}
Exemple #3
0
int main( void )
{
  char string[ SIZE ];

  puts( "Enter a string: " );
  scanf("%79s", string);
  printf("%d\n", mystery2( string ) );
}
Exemple #4
0
int main( void )
{
   char string[ 80 ]; /* create char array */

   printf( "Enter a string: ");
   scanf( "%s", string ); 
   printf( "%d\n", mystery2( string ) );
   return 0; /* indicates successful termination */
} /* end main */
Exemple #5
0
int
main(int argc, char *argv[])
{
	int x = 4, y = 2;

	printf("x = %d, y = %d\n\n",x,y);
	mystery2(&x,&y);
	printf("x = %d, y = %d\n\n",x,y);

	return EXIT_SUCCESS;
}