Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用While语句查找x^y_C++ - Fatal编程技术网

C++ 使用While语句查找x^y

C++ 使用While语句查找x^y,c++,C++,请帮我解决这个问题: 使用while语句编写一个C++程序,提示用户输入两个数字(x,y),然后找到x^ y.< /p> 样本运行: 请输入x和y的值 7 0 7^0=1 请输入x和y的值 5 6 5^6=15625 请不要用pow声明。 我用power声明(很简单)来做,但我需要它,而不是pow声明 我就是这么做的 整数计数器,x,y,ttl; 计数器=0 while (counter == 0){ cout << "Please enter the values of x

请帮我解决这个问题:

使用while语句编写一个C++程序,提示用户输入两个数字(x,y),然后找到x^ y.< /p> 样本运行:

请输入x和y的值

7

0

7^0=1

请输入x和y的值

5

6

5^6=15625

请不要用pow声明。 我用power声明(很简单)来做,但我需要它,而不是pow声明 我就是这么做的

整数计数器,x,y,ttl; 计数器=0

while (counter == 0){
    cout << "Please enter the values of x and y ";
    cin >> x >> y;
    ttl = pow(x,y);

    counter++;
}
cout << x << " ^ " << y << " = " << ttl ;
while(计数器==0){
cout>x>>y;
ttl=功率(x,y);
计数器++;
}

cout这里有三种解决方案
(尽管第一种不使用while循环)

或者,如果您确实想要一个while循环:

int power(int x, int y)
{
    while (y-1)
    return x*power(x, --y);
    return x;
}
使用“转到”如何:


由于user3041987指定应该使用while语句,也许我们实际上可以编写一些使用while循环的东西:):

整数幂(整数x,整数y)
{
int结果=1;
int i=0;
而(i这应该可以

int x,y,prod=1;
cout<<"Please enter the values of x and y ";
cin>>x>>y;

while (y>0)
{
 prod*=x;
 y--;
}
cout<<x<<"^"<<y<<"= "<<prod;
intx,y,prod=1;
coutx>>y;
而(y>0)
{
prod*=x;
y--;
}

coutHello和欢迎访问stackoverflow.com。请花些时间阅读,特别是名为和的部分。更重要的是,请阅读。您可能还想了解a是什么。最简单(但不是最快)求幂的方法是通过重复乘法。使用循环来重复乘法。如果在执行此操作时遇到任何具体问题,请询问有关这些问题的具体问题。注意:某些字符在一种上下文中可能具有某些含义,但在另一种上下文中可能具有不同的含义。而插入符号(^)在某些上下文中,在编程中,特别是在C++中,它是指幂运算,它指的是代码“XOR ”。至少在第一次使用时,描述除了最明显的操作符以外的任何东西的意图含义。我使用PoWα括括使用命名空间STD;int()int {计数器,x,y,tTL;计数器=0;(counter==0){cout>x>>y;ttl=pow(x,y);counter++;}但老师说不要使用pow:(@user3041987:老师有没有说“请让堆栈溢出人群帮你解决它?”
int power(int x, int y)
{
    int r = 1;
    while (y --> 0) r *= x; return r;
}
int power(int x, int y)
{
  int result = 1;
  int i = 0;
  while(i<y)
  {
    result *= x; 
    ++i;
  }
  return result;
}
int x,y,prod=1;
cout<<"Please enter the values of x and y ";
cin>>x>>y;

while (y>0)
{
 prod*=x;
 y--;
}
cout<<x<<"^"<<y<<"= "<<prod;