1
//
//  main.c
//  단기 연구 과제
//  Created by Shiro Nai on 07/05/2019.
//  Copyright © 2019 Shiro Nai. All rights 
//  reserved.


#include <stdio.h>
#include <math.h>

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

    printf("How many people?");
    scanf("%lf", &n);

    int k = floor(log10(n)/log10(2));

    printf("The last standing person's number is %lf.", (2*(n-(2^k)))+1);
    return 0;
}

The output is different from what I expected. For example, when n=5 the answer is 3, but I get 11 for the output.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56

1 Answers1

3

The ^ symbol is not a power operator. This does not do any kind of math, this is an exclusive-or operator.

In order to do power calculations, you need pow() from math.h

#include <stdio.h>
#include <math.h>

int main() {
    printf("The value of 11 ^ 3 = %.2f", pow(11.0, 3)); // outputs 1331.00
}

Sources:

Frontear
  • 1,150
  • 12
  • 25