C语言求等比数列2的0次方,2的1次方,2的2次方,...,2的63次方前64项的和。
答案:4 悬赏:20 手机版
解决时间 2021-04-18 23:11
- 提问者网友:咪咪
- 2021-04-18 11:36
C语言求等比数列2的0次方,2的1次方,2的2次方,...,2的63次方前64项的和。
最佳答案
- 五星知识达人网友:罪歌
- 2021-04-18 12:25
给你提供三种方法,你自己根据其优劣进行选择。
#include
#define N 64
unsigned _int64 fun_1( )
{
unsigned _int64 sum = 0, item = 1;
int i;
for(i = 0; i < N; i++)
{
sum += item;
item *= 2;
}
return sum;
}
unsigned _int64 fun_2( )
{
unsigned _int64 sum = 0;
int i;
for(i = 0; i < N; i++)
sum = sum * 2 + 1;
return sum;
}
unsigned _int64 pow(int x, int y)
{
unsigned _int64 tmp;
if(y == 0) return 1;
tmp = pow(x, y / 2);
if(y % 2 == 0) return tmp * tmp;
else return tmp * tmp * x;
}
unsigned _int64 fun_3( )
{
return pow(2, N) - 1;
}
void main()
{
printf("%I64u\n", fun_1());
printf("%I64u\n", fun_2());
printf("%I64u\n", 0xffffffffffffffff);
}
#include
#define N 64
unsigned _int64 fun_1( )
{
unsigned _int64 sum = 0, item = 1;
int i;
for(i = 0; i < N; i++)
{
sum += item;
item *= 2;
}
return sum;
}
unsigned _int64 fun_2( )
{
unsigned _int64 sum = 0;
int i;
for(i = 0; i < N; i++)
sum = sum * 2 + 1;
return sum;
}
unsigned _int64 pow(int x, int y)
{
unsigned _int64 tmp;
if(y == 0) return 1;
tmp = pow(x, y / 2);
if(y % 2 == 0) return tmp * tmp;
else return tmp * tmp * x;
}
unsigned _int64 fun_3( )
{
return pow(2, N) - 1;
}
void main()
{
printf("%I64u\n", fun_1());
printf("%I64u\n", fun_2());
printf("%I64u\n", 0xffffffffffffffff);
}
全部回答
- 1楼网友:孤独的牧羊人
- 2021-04-18 15:21
利用两个循环嵌套,最外层的循环用来相加这64项,内层循环用来得出每个分项的值,具体的代码应该自己想,想通了那么这里的知识也就明白了。
- 2楼网友:罪歌
- 2021-04-18 13:59
看我的方法,好牛的哦!
#include
void main()
{
unsigned _int64 result =0xFFFFFFFFFFFFFFFF;
printf("%I64u\n", result);
}
#include
void main()
{
unsigned _int64 result =0xFFFFFFFFFFFFFFFF;
printf("%I64u\n", result);
}
- 3楼网友:舊物识亽
- 2021-04-18 13:54
//常规累加
# include
# include
int main(void)
{
double s=0;
int i;
for(i=0;i<64;i++)
s+=pow(2,i);
printf("%.lf",s);
return 0;
}//利用数学公式
# include
# include
int main(void)
{
printf("%.lf",2*(1-pow(2,63))/(1-2));
return 0;
}
# include
# include
int main(void)
{
double s=0;
int i;
for(i=0;i<64;i++)
s+=pow(2,i);
printf("%.lf",s);
return 0;
}//利用数学公式
# include
# include
int main(void)
{
printf("%.lf",2*(1-pow(2,63))/(1-2));
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯