计算2的N次方&&计算e

2的N次方

注意:这里在处理的时候并没有用循环来处理,而是用移位的做法。    n<<4  就是 n*2^4    ,所以在本例中只需要写 1<<time  (time是要求的精度)。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int main(){
 4     int time;
 5     printf("要求出2的多少次方:");
 6     scanf("%d",&time) ;    
 7     int number=3<<time;
 8     printf("number = %d",number);
 9     return 0;
10 }
11 
12   

计算e    ,这个计算e的程序,在另一篇博客“ 错排公式及其近似公式 ”中也有提及,那里给出了递归的算法来解决。

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main(){
 5     double e=1;
 6     double temp=1;
 7     cout<<"输入您想要的e有多精确:";
 8     int num;
 9     cin>>num;
10     for(int i=1;i<=num;i++){
11         temp*=i;
12         e+=1.0/temp;
13     } 
14     cout<<"e近似等于:"<<e<<endl;
15     return 0; 
16 }
原文地址:https://www.cnblogs.com/liugl7/p/4820429.html