用于计算x提高Y的X功能的程序

发布于 2025-02-13 07:28:33 字数 577 浏览 1 评论 0原文

int main()
{ unsigned int x, y;

    printf_s("Enter value for x: ");
    scanf_s("%u", &x);

    printf_s("Enter value for y: ");
    scanf_s("%u", &y);

    unsigned int count = 1;
    while (count < y);
    {
        x *= x;
        count++;
    }

    printf_s("x raised to the power of y is: %u", x);
}

嗨,我不确定我在哪里出错。 当我运行它时,我会按照提示输入两个值,然后什么也没有发生。 我输入y的价值之后就停止了。

输入x:3的值 输入y:2

这样的值。有人可以将我指向正确的方向吗? 我知道,如果y&lt; = 1,这种方式将无法工作。 但是当y&gt时,它不应该适合; 1?

我已经搜索了。还有另一个问题用于循环。 我可以看到它可以用于循环,但我认为虽然循环更合适,因为它可以提供更多的自由。 请,谢谢!

int main()
{ unsigned int x, y;

    printf_s("Enter value for x: ");
    scanf_s("%u", &x);

    printf_s("Enter value for y: ");
    scanf_s("%u", &y);

    unsigned int count = 1;
    while (count < y);
    {
        x *= x;
        count++;
    }

    printf_s("x raised to the power of y is: %u", x);
}

Hi, I'm not sure where I went wrong.
When I run it, I enter two values as prompted and then nothing else happens.
It just stops after I enter they value for y.

Enter value for x: 3
Enter value for y: 2

Like this. Could someone point me in the right direction?
I understand that this way will not work if y <= 1.
But shouldn't it work for when y > 1?

I've searched for it. There is another question using for loop.
I can see that it could be done with for loop but I think while loop is more appropriate since it gives more freedom.
Please and thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£噩梦荏苒 2025-02-20 07:28:33
  1. 使用
  2. ;在上使用,在循环中未执行您的代码,
  3. 将较大的整数类型作为“正常”无签名的int将在循环中执行迅速地。
  4. x *= x肯定是错误的。
unsigned long long mypow(unsigned x, unsigned y)
{
    unsigned long long result = 1;
    while(y--) result *= x;
    return result;
}


int main(void)
{
    unsigned x, y;

    scanf("%u,%u", &x, &y);

    printf("%u ^ %u = %llu\n", x, y, mypow(x, y));
}

https://godbolt.org/z/xzx8mo5aw

  1. Use functions
  2. you have ; past the while and your code in braces is not executed in the loop
  3. use a larger integer type as the result as a "normal" unsigned int will wraparound quickly.
  4. x *= x is definitely wrong.
unsigned long long mypow(unsigned x, unsigned y)
{
    unsigned long long result = 1;
    while(y--) result *= x;
    return result;
}


int main(void)
{
    unsigned x, y;

    scanf("%u,%u", &x, &y);

    printf("%u ^ %u = %llu\n", x, y, mypow(x, y));
}

https://godbolt.org/z/xzx8Mo5aW

旧梦荧光笔 2025-02-20 07:28:33
#include <iostream>

int main()
{

    unsigned int long x; // assign x
    printf_s("Enter value for x: "); // prompt
    scanf_s("%u", &x); // read input

    unsigned int y; // assign y
    printf_s("Enter value for y: "); // prompt
    scanf_s("%u", &y); // read input

    unsigned int count = 0; // initialize count
    unsigned int power = 1; // initialize power
    while (count < y) // loop while y is less than count
    {
        power *= x; // multiply power by x
        count++; // increment count

    } // loop ends

    printf_s("x raised to the power of y is: %u", power); // display result
}

我已经删除了;
我添加了很长的时间,以供未签名的int。
我已经设置了一个新的变量,以包含X升至Y的X的值。
我已经检查了当y = 0或1时是否有效,并且确实如此。
谢谢大家的帮助!

PS教科书答案具有pritnf(“%s”,“输入第一个整​​数:”);要提示第一个整数,x。我不确定为什么它们会添加%s,因为没有它,它的工作正常。有人知道为什么会添加%s吗?

#include <iostream>

int main()
{

    unsigned int long x; // assign x
    printf_s("Enter value for x: "); // prompt
    scanf_s("%u", &x); // read input

    unsigned int y; // assign y
    printf_s("Enter value for y: "); // prompt
    scanf_s("%u", &y); // read input

    unsigned int count = 0; // initialize count
    unsigned int power = 1; // initialize power
    while (count < y) // loop while y is less than count
    {
        power *= x; // multiply power by x
        count++; // increment count

    } // loop ends

    printf_s("x raised to the power of y is: %u", power); // display result
}

I've deleted ;.
I've added long to unsigned int.
I've set up a new variable, power, in order to contain the value of x raised to a power of y.
I've checked if this works when y = 0 or 1 and it does.
Thank you everyone for your help!

P.S. Textbook answer has pritnf( "%s", "Enter first integer: "); for prompting for the first integer, x. I'm not sure why they add %s as it works perfectly fine without it. Does anyone know why one would add %s?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文