【问题标题】:Calculating the result of a power and a base without math.h在没有 math.h 的情况下计算幂和基数的结果
【发布时间】:2016-04-20 06:47:34
【问题描述】:

在我的大学里,我被要求创建一个程序,要求用户提供两个输入。一个是基数,另一个是数的幂。我不能只使用 math.h 循环。

这是我目前的代码:

#include <stdio.h>

int main() {
    int base;
    printf(" Please enter the base: ");
    scanf("%d", &base);

    int power;
    printf(" Please enter the power: ");
    scanf("%d", &power);

    printf("\n%d ^ %d is the same as...\n\n", base, power);

    printf(" %d", base);

    int reps;
    int number;
    for(reps = base; reps <= power; reps += 1) {
        printf("* %d ", base);
    }

    for(number; number <= power;number += 1) {
        int result = base * base;
        for (result; number <= power; result = base * result) {
            result = result * base;
            printf("\n or  %d", result);
        }
    }


    return 0;
}

请帮助我。我很迷茫,我想哭:(没关系。

【问题讨论】:

  • int number; --> int number = 0;
  • 您能告诉我们会发生什么以及预计会发生什么吗?
  • 你是this OP..lol的同学
  • @SouravGhosh 更好for(number = 0; ...
  • 更好(如果在以后的 c 标准上):for (int number = 0

标签: c


【解决方案1】:

(您的主要问题是您使用的是未初始化的变量;在 C 中这样做的行为是 undefined。)

但是让我们重新设计答案。首先要做的是将实际的电源 function 从所有输入和输出中分离出来。关于该功能,我将我最喜欢的方式放入答案池中,前提是您会仔细研究并理解它。

您可以使用一种称为平方求幂的技术来解决这个问题

int getPower(int base, int power/*must be non-negative*/)
{
    int ret = 1;
    while (power){
        if (power & 1){ /*this means the current value of `power` is odd*/
            ret *= base;
        }
        power >>= 1; /*ToDo - figure this out with your debugger*/
        base *= base;
    }
    return ret;
}

该方法在https://en.wikipedia.org/wiki/Exponentiation_by_squaring中有充分说明

【讨论】:

  • 唯一理智的答案,其他人就像通过添加循环来建议乘法一样。但/*this means power is odd*/ 的评论并不真正相关,更多的是检查每个位的位置。
  • @WeatherVane:是的,这是误导。我已经修改了。
  • @AnttiHaapala:该算法适用于否定的base。 (它甚至适用于浮点 base)。
【解决方案2】:

计算功率的循环如下所示

int product = 1;
for(int multiplicationCounter = 1;multiplicationCounter <= power; multiplicationCounter ++) {
    product *= base;
}
printf("Result is %d", product);

您可以将其集成到您的代码中,也许可以更改输出。 这应该替换你的整个第二个 for 循环。

【讨论】:

  • 基数为 10 的 2 次方 = 10 的平方。然后循环将循环 2 次,直到 power 并将乘积乘以每次底数,如 10*10。或者你是什么意思?
  • 我对他问题的措辞感到困惑。我认为基数是指数字的基数。例如11 base 2 = 3 base 10。不过我错了。
  • 对不起。没带你到那里。
【解决方案3】:

假设底数和幂都是正整数,

然后

int Result =1;
for (int i=0; i<=power;i++)
{
if(power==0)
Result=1;

Result =Result*base;
}

【讨论】:

    【解决方案4】:

    这应该可以工作

    #include <stdio.h>
    
    int main()
    {
         int base;
         printf(" Please enter the base: ");
         scanf("%d", &base);
    
         int power;
         printf(" Please enter the power: ");
         scanf("%d", &power);
    
         printf("\n%d ^ %d is the same as...\n\n", base, power);
    
         printf(" %d", base);
    
         int reps;
         int number;
         int result=1:
    
    
            for(number=1; number <= power;number += 1)
            {
                result=result*base
            }
    
         printf("The result is %d", result);
         return 0;
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-24
      • 2019-09-02
      • 2017-09-24
      • 1970-01-01
      • 2022-10-13
      • 2015-12-02
      • 1970-01-01
      相关资源
      最近更新 更多