习题5:
- /***********************
- ** 习题5 **
- ***********************/
- #include<iostream>
- #include<iomanip>
- using namespace std;
- int main()
- {
- int i,n;
- double e=1,a=1;
- for(i=1;i<=10;i++)
- {
- for(n=1;n<=i;n++)
- {
- a*=n;
- }
- e+=1/a;
- a=1;
- }
- cout<<"e="<<setprecision(8)<<e<<endl;
- return 0;
- }
习题6:
- /********************
- ** 习题6 **
- ********************/
- #include<iostream>
- #include<iomanip>
- using namespace std;
- int main()
- {
- double i,pi=0,a=1;
- for(i=1;i<=100;i=i+2)
- {
- pi+=4*1/i*a;
- a*=-1;
- }
- cout << "pi/4 ="<<pi<<endl;
- return 0;
- }
习题7.
- /************************
- ** 习题7 **
- ************************/
- #include<iostream>
- #include<iomanip>
- using namespace std;
- int main()
- {
- int a;
- while(1)
- {
- cout<<"请输入一个数"<<endl;
- cin>>a;
- if(a<10)
- cout<<a<<"<10"<<endl;
- else if(a>10&&a<100)
- cout<<a<<"is 10 to 100"<<endl;
- else if(a>100&&a<1000)
- cout<<a<<"is 100 to 1000"<<endl;
- else if(a>1000)
- cout<<a<<">1000"<<endl;
- else
- cout<<"error"<<endl;
- }
- return 0;
- }
习题8.
- /**********************
- ** 习题8 **
- **********************/
- #include <iostream>
- using namespace std;
- int main()
- {
- cout<<" *"<<endl;
- cout<<" * * *"<<endl;
- cout<<" * * * * *"<<endl;
- cout<<"* * * * * * *"<<endl;
- cout<<" * * * * *"<<endl;
- cout<<" * * *"<<endl;
- cout<<" *"<<endl;
- return 0;
- }
习题9:
- /**********************
- ** 习题9 **
- **********************/
- #include<iostream>
- using namespace std;
- int main()
- {
- int n=0,sum=0;
- while(sum<1000)
- {
- n++;
- sum +=n*n;
- }
- cout<<"所求的n为:"<<n<<endl;
- return 0;
- }
习题10:
- /*********************
- ** 习题10 **
- *********************/
- #include<iostream>
- using namespace std;
- int main()
- {
- long unsigned int a=0,sum1=0,sum2=0;
- double b=0.01;
- while(a<30)
- {
- sum1 +=100000;
- b=b*2;
- sum2 +=b;
- a++;
- }
- cout<<"陌生人一共给了百万富翁"<<sum1<<"元"<<endl;
- cout<<"百万富翁一共给了陌生人"<<sum2<<"元"<<endl;
- return 0;
- }
作业2:
- /******************
- ** 百钱百鸡 **
- ******************/
- #include<iostream>
- using namespace std;
- int main()
- {
- int x,y,z,sum,num;
- for(x=0;x<=20;x++)
- for(y=0;y<34;y++)
- for(z=0;z<=99;z+=3)
- {
- sum =x+y+z;
- num =5*x+3*y+z/3;
- if(sum==100&&num==100)
- {
- cout<<"公鸡数为:"<<x<<endl;
- cout<<"母鸡数为:"<<y<<endl;
- cout<<"小鸡数为:"<<z<<endl;
- cout<<"另一种情况如下:"<<endl;
- }
- }
- return 0;
- }
作业3:
- /**********************************
- ** 计算位数,并将各位数相加 **
- **********************************/
- #include<iostream>
- using namespace std;
- int main()
- {
- long int n,i=0,sum=0;
- cout<<"请输入一个数:"<<endl;
- cin>>n;
- while(n!=0)
- {
- sum +=n%10;
- n=n/10;
- i++;
- }
- cout<<"所输入的数是一个"<<i<<"位数"<<endl;
- cout<<"各位数之和是:"<<sum<<endl;
- return 0;
- }
作业4:
- /*********************************
- ** 求某一点处建筑物的高度 **
- *********************************/
- #include<iostream>
- using namespace std;
- int main()
- {
- double x,y;
- cout<<"请输入x的值"<<endl;
- cin>>x;
- cout<<"请输入y的值"<<endl;
- cin>>y;
- if((x+2)*(x+2)+(y+2)*(y+2)<=1
- ||(x+2)*(x+2)+(y-2)*(y-2)<=1
- ||(x-2)*(x-2)+(y+2)*(y+2)<=1
- ||(x-2)*(x-2)+(y-2)*(y-2)<=1)
- cout<<"该点处的建筑物高度为10米"<<endl;
- else
- cout<<"该点处的建筑物的高度为0米"<<endl;
- return 0;
- }
作业5:
- /***********************
- ** n的阶乘的和 **
- ***********************/
- #include<iostream>
- using namespace std;
- int main()
- {
- int a;
- cout<<"请输入数字1或2查看方法:"<<endl;
- cin>>a;
- if(a=1)
- {
- cout<<"下面是方法1"<<endl;
- int x,y=1,sum=0;
- cout<<"*****************"<<endl;
- cout<<"请输入n的值"<<endl;
- cin>>x;
- while(y<x)
- {
- sum +=y*y;
- y++;
- }
- cout<<"所求的值为:"<<sum<<endl;
- }
- else
- {
- cout<<"下面是方法2"<<endl;
- int i,n,num=0;
- cout<<"*****************"<<endl;
- cout<<"请输入n的值"<<endl;
- cin>>n;
- for(i=1;i<=n;i++)
- {
- num +=i*i;
- }
- cout<<"所求的值为:"<<num<<endl;
- }
- return 0;
- }
作业6:
- /******************************
- ** 猴子吃苹果的问题 **
- ******************************/
- #include<iostream>
- using namespace std;
- int main()
- {
- cout<<"下面来看一个猴子吃苹果的问题。"<<endl;
- cout<<"猴子第一天摘了若干个苹果。"<<endl;
- cout<<"当时吃了一半,还不过隐,又多吃了一个。"<<endl;
- cout<<"第二天,又吃掉余下的一半,又多吃一个。"<<endl;
- cout<<"以后每一天,都是吃掉前一天余下的一半零一个。"<<endl;
- cout<<"到第10天,只有一个苹果了。"<<endl;
- cout<<"问猴子第一天共摘了多少个苹果?"<<endl;
- int n=1,i;
- for(i=1;i<10;i++)
- n =(n+1)*2;
- cout<<"********************"<<endl;
- cout<<"********************"<<endl;
- cout<<"********************"<<endl;
- cout<<"猴子第一天一共摘了"<<n<<"个苹果"<<endl;
- return 0;
- }
作业7:
- /****************************
- *** 作业7 ***
- ****************************/
- #include<iostream>
- using namespace std;
- int main()
- {
- int a,b=0,n,s=0,i=1;
- cout<<"s[n]=a+aa+aaa+aa...a(n个)的值。"<<endl;
- cout<<"请输入一个小于10的正整数a"<<endl;
- cin>>a;
- cout<<"请输入一个正整数n"<<endl;
- cin>>n;
- while(i<=n)
- {
- b=b*10+a;
- s +=b;
- i++;
- }
- cout<<"s[n]="<<s<<endl;
- return 0;
- }
作业8:
- /**********************************
- ** 打印九九乘法表 **
- **********************************/
- #include<iostream>
- using namespace std;
- int main()
- {
- int i,n,s;
- for(i=1;i<=9;i++)
- {
- for(n=1;n<=i;n++)
- {
- s=i*n;
- cout<<n<<"*"<<i<<"="<<s<<'\t';
- }
- cout<<endl;
- }
- return 0;
- }
作业9:
- /********************************
- ** 比赛名单 **
- ********************************/
- #include<iostream>
- using namespace std;
- int main()
- {
- int m=0;
- char x,y,z;
- cout<<"将张三,李四,王五分别记为a,b,c"<<endl;
- cout<<"将陈六,赵七,宋八分别记为A,B,C"<<endl;
- for(x='A';x<='C';x++)
- for(y='A';y<='C';y++)
- for(z='A';z<='C';z++)
- {
- if(x!=y&&x!=z&&y!=z)
- {
- if(x!='A'&&z!='A'&&z!='C')
- {
- cout<<"比赛名单如下:"<<endl;
- cout<<"a--"<<x<<" "<<"b--"<<y<<" "<<"c--"<<z<<" ";
- }
- }
- }
- return 0;
- }