【问题标题】:Error on Normalized Equation for Josephus Problem [duplicate]约瑟夫斯问题的归一化方程错误[重复]
【发布时间】:2019-05-06 16:31:07
【问题描述】:
//
//  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;
}

输出与我的预期不同。 例如,当n=5 时,答案是 3,但我的输出是 11。

【问题讨论】:

  • 你知道2^k是什么吗?它使用的是异或运算符,但我猜你是提升到2 的幂k?
  • 我很惊讶没有规范的欺骗
  • C 有一个log2() 函数,顺便说一句。
  • 您可能还会对exp2() 感兴趣。
  • 等等...我生疏了,但exp2(log2(n)) 不只是n?

标签: c josephus


【解决方案1】:

^ 符号不是幂运算符。这不做任何数学运算,这是一个exclusive-or 运算符。

为了进行功率计算,你需要来自math.h的pow()

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

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

来源:

【讨论】:

  • 您可以指向 C 标准,而不是其中一个显示“cplusplus”的页面
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-20
相关资源
最近更新 更多