一个亿万富翁和一个陌生人的换钱计划

计划:陌生人每天给你十万元,而你第一天只需要给我一分钱,第二天我仍然给你十万 元,你给我两分钱,第三天我仍然给你十万元,你给我四分钱........你每天给我的钱是前一天的两倍,直到满一个月(30天).求他们互相给对方多少钱?

代码实现:

 1 #include<iostream>
2 #include<cmath>
3 using namespace std;
4 void main()
5 {
6 float a,b,s=0;
7 a=100000*30;
8 for(int i=1;i<=30;i++)
9 {
10 b=0.01*pow(2,i-1);
11 s+=b;
12 }
13 cout<<a<<endl;
14 cout<<s<<endl;
15 cout<<"富翁就是个傻逼"<<endl;
16 }

另一种算法:

 1 #include<iostream>
 2 #include<cmath>
 3 using namespace std;
 4 int main()
 5 {
 6     float s1=0,s2=0,x=0.01;
 7     for(int i=1;i<=30;i++)
 8     {
 9         s1=s1+100000;
10         s2=s2+x;
11         x=2*x;
12     }
13     cout<<s1<<endl;
14     cout<<s2<<endl;
15     return 0;
16 }

 

运行结果:

posted @ 2012-04-08 13:53  iamvirus  阅读(980)  评论(0编辑  收藏  举报