鱼C论坛

 找回密码
 立即注册
查看: 1130|回复: 6

[已解决]求助一个取幂程序

[复制链接]
发表于 2021-12-5 20:11:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

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


int main()
{
        int a;
        int b;
        int Pow(  int m, int n );
       
       
        while( 1 )
        {
               
                printf( "Please input 2 numbers:\n" );
               
                scanf( "%d, %d", &a, &b );
                getchar();
                printf( "The power is %d.\n", Pow( a, b ) );
                getchar();
        }
}


int IsEven( int Y )                                                                        //判断奇偶
{
        if( ( Y % 2 ) == 0 )
                return 1;
        else
                return 0;
}


int
Pow(  int X, int N )                                                                                        //递归求幂运算
{
         if( N == 0 )
         {
                 return 1;
         }       
         if( N == 1 )
         {
                 return X;
         }       
         if( IsEven( N ) )
         {
                 return Pow( (X * X), (N / 2) );
         }       
         else
         {
                 return Pow( (X * X), (N / 2) ) * X;
                 
         }       
}




//这个程序在VB6怎么也得不出正确的运算结果,不知道哪出问题了,望来个大佬解惑,感激感谢
最佳答案
2021-12-5 21:24:55
PTF 发表于 2021-12-5 20:43
就是一个递归求幂的程序,输入a,b,求a的b次幂,b是偶数输出幂Pow(  int X, int N )= Pow( (X * X), (N  ...
  1. #include <stdio.h>
  2. #include <math.h>

  3. int main(){
  4.         int POW(int b, int e);
  5.         int a, b;
  6.         printf("Please enter 2 integer: ");
  7.         scanf("%d%d", &a, &b);
  8.         printf("The power %d of %d is %d", b, a, POW(a, b));
  9.         return 0;
  10. }

  11. int POW(int b, int e){
  12.         if(!(e)) return 1;
  13.         else if(e == 1) return b;
  14.         else if(!(e%2)) return POW(b*b, e/2);
  15.         else return POW(b*b, e/2)*b;
  16. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-12-5 20:17:21 | 显示全部楼层
        什么是正确结果,把它说出来,你这个代码的目标是干什么,你不是说,没人知道,别人也不知道你的代码对不对。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-5 20:43:17 | 显示全部楼层
jackz007 发表于 2021-12-5 20:17
什么是正确结果,把它说出来,你这个代码的目标是干什么,你不是说,没人知道,别人也不知道你的代 ...

就是一个递归求幂的程序,输入a,b,求a的b次幂,b是偶数输出幂Pow(  int X, int N )= Pow( (X * X), (N / 2) ),若b是奇数则Pow(  int X, int N )= Pow( (X * X), (N / 2) ) * X,可以运行就是算不准
Please input 2 numbers:
2 5
The power is 0.
Please input 2 numbers:
12 0
The power is 0.
Please input 2 numbers:
4 1
The power is 0.
Please input 2 numbers:
2 3
The power is 0.
Please input 2 numbers:
4 6
The power is 0.
Please input 2 numbers:
5 6 65
The power is 2098733489.
Please input 2 numbers:
The power is 865578241.
20
Please input 2 numbers:
The power is 0.

Please input 2 numbers:
4 4
The power is 0.
Please input 2 numbers:
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-5 20:51:09 | 显示全部楼层
本帖最后由 jackz007 于 2021-12-5 20:55 编辑
PTF 发表于 2021-12-5 20:43
就是一个递归求幂的程序,输入a,b,求a的b次幂,b是偶数输出幂Pow(  int X, int N )= Pow( (X * X), (N  ...

  1. #include <stdio.h>

  2. unsigned long long powerx(unsigned long long a , unsigned long long b)
  3. {
  4.         return (b > 0) ? a * powerx(a , b - 1) : 1                ;
  5. }

  6. int main(void)
  7. {
  8.         unsigned long long a , b                                  ;
  9.         scanf("%I64u%I64u" , & a , & b)                           ;
  10.         printf("%I64u ^ %I64u = %I64u\n" , a , b , powerx(a , b)) ;
  11. }
复制代码

        编译、运行实况:
  1. D:\00.Excise\C>g++ -o x x.c

  2. D:\00.Excise\C>x
  3. 2 5
  4. 2 ^ 5 = 32

  5. D:\00.Excise\C>x
  6. 12 0
  7. 12 ^ 0 = 1

  8. D:\00.Excise\C>x
  9. 4 1
  10. 4 ^ 1 = 4

  11. D:\00.Excise\C>x
  12. 2 3
  13. 2 ^ 3 = 8

  14. D:\00.Excise\C>x
  15. 4 6
  16. 4 ^ 6 = 4096

  17. D:\00.Excise\C>x
  18. 5 6
  19. 5 ^ 6 = 15625

  20. D:\00.Excise\C>x
  21. 4 4
  22. 4 ^ 4 = 256

  23. D:\00.Excise\C>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-5 21:21:24 | 显示全部楼层
jackz007 发表于 2021-12-5 20:51
编译、运行实况:

你这个可以实现但是算法和时间复杂度不一样了,我是在做算法效率题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-5 21:24:55 | 显示全部楼层    本楼为最佳答案   
PTF 发表于 2021-12-5 20:43
就是一个递归求幂的程序,输入a,b,求a的b次幂,b是偶数输出幂Pow(  int X, int N )= Pow( (X * X), (N  ...
  1. #include <stdio.h>
  2. #include <math.h>

  3. int main(){
  4.         int POW(int b, int e);
  5.         int a, b;
  6.         printf("Please enter 2 integer: ");
  7.         scanf("%d%d", &a, &b);
  8.         printf("The power %d of %d is %d", b, a, POW(a, b));
  9.         return 0;
  10. }

  11. int POW(int b, int e){
  12.         if(!(e)) return 1;
  13.         else if(e == 1) return b;
  14.         else if(!(e%2)) return POW(b*b, e/2);
  15.         else return POW(b*b, e/2)*b;
  16. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-5 21:49:58 | 显示全部楼层

解决了,多谢, 问题所在scanf("%d%d", &a, &b);的空格哈尴尬,你的代码简洁多了,学习m(_ _)m
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 22:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表