# 用C++编写输入两个数,如,X,Y,求X的Y次方.

yjip267 2008-03-18 05:29:46

#include <iostream>
using namespace std;
long fun(int x,int y);
int main()
{ int number,power;
cout<<"输入一个数";
cin>>number;
cout<<"输入次方数:";
cin>>power;
return 0;
}
long fun(int x,int y)
{ if (y==1)
return x;
else
return (x*fun(x,y-1));
}
...全文
2220 12 打赏 收藏 举报

12 条回复

mjlxlx 2008-03-19
• 打赏
• 举报

tsocpp 2008-03-19
• 打赏
• 举报

x的-y次方应该返回x的y次方的倒数啊
jianjun7 2008-03-19
• 打赏
• 举报

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
double x,y;
while(cin>>x>>y)
{
cout<<pow(x,y)<<endl;
}
return 0;
}
ouyh12345 2008-03-19
• 打赏
• 举报

babyofbaby 2008-03-19
• 打赏
• 举报

hasen2008 2008-03-18
• 打赏
• 举报

#include <iostream>
#include <cmath>
using namespace std;

template <typename _T>
_T Square(int x, int y)
{
if ((0 == x) || (1 == x))
{
return x;
}
if (0 == y)
{
return 1;
}
if (1 == y)
{
return x;
}

if (0 < y)
{
return x * Square<_T>(x, y - 1);
}
else
{
return 1 / (x * Square<_T>(x, abs(y) - 1));
}
}

void main()
{
double x;
int y;
cout << "input x , y:" << endl;
cin >> x;
cin >> y;

double z1 = 0;
int z2 = 0;

y < 0 ? z1 =
Square<double>(x, y)
: z2 = Square<int>(x, y);

cout << "NAGATIVE:" << z1 << endl;
cout << "PULUS:" << z2 << endl;

}
WZL19870523 2008-03-18
• 打赏
• 举报

paidfighting 2008-03-18
• 打赏
• 举报

Lynn_Ran 2008-03-18
• 打赏
• 举报

``````
#include  <iostream>
using namespace std;

long fun(int iNumber,int iPower);

int main()
{
int iNumber, iPower;
cout << "输入一个数";
cin >> iNumber;
cout <<"输入次方数:";
cin >> iPower;
if ( iPower >= 0 )
cout << iNumber << " to the " << iPower << "th power is " << iAnswer << endl;
else
cout << iNumber << " to the " << iPower << "th power is 1/" << iAnswer << endl;
return 0;
}

long fun(int iNumber,int iPower)
{

if ( iNumber==0 || iNumber==1 || iPower==1 )
return iNumber;
else if ( iPower == 0 )
return 1;
else if ( iPower < 0 )
iPower = -iPower;
for ( int i=1; i<=iPower; i++)
}
``````

ouyh12345 2008-03-18
• 打赏
• 举报

ouyh12345 2008-03-18
• 打赏
• 举报

long fun(int x,int y)
{
if (x==0 || x==1)
return x;

if (y==1)
return x;

if (y == 0)
return 1;

if (y > 0)
return (x*fun(x,y-1));

return fun(x, y+1) / x;
}

• 打赏
• 举报

X的3次方，不就是X * X * X 嘛

6.3w+

C++ 语言相关问题讨论，技术干货分享，前沿动态等
c++ 技术论坛（原bbs）

2008-03-18 05:29

1. 请不要发布与C++技术无关的贴子
2. 请不要发布与技术无关的招聘、广告的帖子
3. 请尽可能的描述清楚你的问题，如果涉及到代码请尽可能的格式化一下