二分求幂
2015-07-19 21:54
246 查看
//二分求幂and二分快速求幂
#include<stdio.h>
int main()
{
int cmp(int a,int b);
int a,b;
int s;
while(~scanf("%d%d",&a,&b))
{
s=cmp(a,b);
printf("%d\n",s);
}
return 0;
}
//普通求幂--同pow(a,b)
/*
int cmp(int a,int b)
{
int i;
int s=1;
for(i=1;i<=b;i++)
{
s*=a;
}
return s;
}
*/
//二分求幂
/*
int cmp(int a,int b)
{
int s=1;
while(b)
{
if(b%2)
s*=a;
b/=2;
a*=a;
}
return s;
}
*/
//二分快速求幂
int cmp(int a,int b)
{
int s=1;
while(b)
{
if(b&1)//b与1相与判断b的奇偶
s*=a;
b>>=1;//b右移一相当于b除以2
a*=a;
}
return s;
}
#include<stdio.h>
int main()
{
int cmp(int a,int b);
int a,b;
int s;
while(~scanf("%d%d",&a,&b))
{
s=cmp(a,b);
printf("%d\n",s);
}
return 0;
}
//普通求幂--同pow(a,b)
/*
int cmp(int a,int b)
{
int i;
int s=1;
for(i=1;i<=b;i++)
{
s*=a;
}
return s;
}
*/
//二分求幂
/*
int cmp(int a,int b)
{
int s=1;
while(b)
{
if(b%2)
s*=a;
b/=2;
a*=a;
}
return s;
}
*/
//二分快速求幂
int cmp(int a,int b)
{
int s=1;
while(b)
{
if(b&1)//b与1相与判断b的奇偶
s*=a;
b>>=1;//b右移一相当于b除以2
a*=a;
}
return s;
}
相关文章推荐
- JavaScript的几种常见用法
- spring单例bean实现原理
- iOS 字符串读写
- leetcode235:Lowest Common Ancestor of a Binary Search Tree
- PHP的基本语法(一)
- python的高级特性3:神奇的__call__与返回函数
- spring入门教程——笔记
- JAVA程序 通过IP地址 获取MAC地址
- maven的环境搭建
- 存储过程简介(转载)
- 手机还能用多久
- 机房重构——增加用户
- Red and Black(DFS)
- 【Linux调试技巧----标准输出重定向到文件】dup2和dup的妙用
- 岩田聪去世 任天堂复兴之路会怎样
- 正斜杠(/)与反斜杠(\)区别
- opencv 二维直方图
- Oracle 利用储存过程插入循环插入大量数据方法
- liunx架构mysql操作
- [POJ 1190] 生日蛋糕