您的位置:首页 > 其它

第四周作业

2014-04-03 00:05 246 查看
实验作业
1.完成课本每一个编程题。要求先画出流程算法图或N-S图,然后编程实现,有可能的话使用两种以上方法;

2.编程求“百钱百鸡”问题。(鸡翁一值钱五,鸡母 一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?)

3.编程输入一个整数,计算它是几位数字,分别输出每一位数字,并输出各个数位上数字之和。

4.在一个平面上,有四个圆柱形塔,塔底圆心坐标分别为(2,2)、(-2,2)、(-2,-2)、(2,-2),塔半径为1,塔高为10米,塔外无建筑物。编程,输入任一个点平面坐标,求该点建筑物的高度。

5.编程计算s=1!+2!+3!+......n!(其中n为整数,n!表示计算n阶乘),要求使用两种以上的方法。

6.猴子吃苹果问题:猴子第一天摘了若干个苹果,当时吃了一半,还不过隐,又多吃了一个。第二天,又吃掉余下的一半,又多吃一个。以后每一天,都是吃掉前一天余下的一半零一个。到第10天,只有一个苹果了。问猴子第一天共摘了多少个苹果?

7.计算s
=a+aa+aaa+aa...a(n个)的值。其中a是一个数字,n表示a的位数。例如,当a=1,n=5时,则要计算的表达式为

s[5]=1+11+111+1111+11111

8.打印九九乘法表。

9.两个羽毛队进行单打比赛,各出3个人。甲队为张三、李四、王五3个队员,已队为陈六、赵七、宋八3个队员。现已经抽签决定比赛名单,有人向队员打听比赛名单,张三说他不和陈六打,王五说他不和陈六和宋八打。请编程找出3对比赛名单。

10.积累调试程序经验,收集错误信息原因(每个同学收集3-5条错误信息原因,并输入电脑形成文字)。

习题5

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
double e=1.0,tmp=1.0;
int i=1,sum=1;
for(i;;i++)
{
sum*=i;
e+=1.0/sum;
if(e-tmp<=1e-6)
break;
tmp=e;
}
cout<<"近似值为:"<<setprecision (8)<<e<<endl;
return 0;
}


圆周率

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
double begin=1,end=-1,i;
for(i=3.0;1/i>=1e-6;i+=2.0)
{
begin+=end/i;
end=-end;
}
cout<<"圆周率π≈"<<setprecision (8)<<begin*4<<endl;
return 0;
}


习题7

#include<iostream>
using namespace std;
int main()
{
double num;
cout<<"请输入一个数:";
cin>>num;
if(num<=10)
cout<<num<<"小于10"<<endl;
else if(num<=100)
cout<<num<<" is 10 to 100"<<endl;
else if(num<=1000)
cout<<num<<" is 100 to 1000"<<endl;
else
cout<<num<<" is 大于1000"<<endl;
return 0;
}


习题8

#include<iostream>
using namespace std;
int main()
{
cout<<"       *       \n"
<<"     * * *    \n"
<<"   * * * * *  \n"
<<" * * * * * * *\n"
<<"   * * * * *  \n"
<<"     * * *    \n"
<<"       *       \n";
return 0;
}


习题⑨

#include<iostream>
using namespace std;
int main()
{
int n,i=1,s=0;
while(s<=1000)
{
n=i;
i++;
n*=n;
s+=n;
}
i--;
cout<<"1^+2^+3^+...+n^<=1000的最大值n="<<i<<endl;
return 0;
}


习题10

#include<iostream>
using namespace std;
int main()
{
double sum=0.0,in=0.0,out=0.01;
int i=1;
for(i;i<=30;i++)
{
sum+=10;
in+=out;
out*=2;
}
cout<<"陌生人给了百万符富翁"<<sum<<"万元"<<endl;
cout<<"百万富翁给了陌生人"<<in/10000<<"万元"<<endl;
return 0;
}


2.百钱买鸡

#include<iostream>
using namespace std;
int main()
{
int a,b,c;
int i=0;
for(a=0;a<=20;a++)
for(b=0;b<=33;b++)
for(c=0;c<=100;c+=3)
{
if(5*a+3*b+c/3==100 && a+b+c==100)
{
i++;
cout<<"方案"<<i<<endl;
cout<<"公鸡"<<a<<"只"<<endl<<"母鸡"<<b<<"只"<<endl<<"鸡雏"<<c<<"只"<<endl;
}
}
return 0;
}




3.整数计算

/*********************
**    输入整数计算  **
*********************/
#include<iostream>
using namespace std;
int main()
{
long int a,b;
int c=0,d,e=0;
cout<<"输入一个整数:"<<endl;
cin>>a;
for(b=10;b<=1000000000;b=b*10)
{
c++;
if(a<b)break;
}
cout<<"位数:"<<c<<endl;
cout<<"   各位上的数分别为:";
while(b>1)
{
b=b/10;
d=a/b;
a=a%b;
e+=d;
cout<<d<<' ';
}
cout<<"   各位上的数值之和: "<<e<<endl;
return 0;
}



4.求建筑物的高度

#include<iostream>
using namespace std;
int main()
{
float x,y;
int height;
cout<<"请输入x坐标:"<<endl;
cin>>x;
cout<<"请输入y坐标:"<<endl;
cin>>y;
if((x+2)*(x+2)+(y+2)*(y+2)<=1) height=10;
else if((x-2)*(x-2)+(y+2)*(y+2)<=1)
height=10;
else if((x-2)*(x-2)+(y-2)*(y-2)<=1)height=10;
else if((x+2)*(x+2)+(y-2)*(y-2)<=1)height=10;
else height=0;
cout<<"建筑物高度为:"<<height<<"m"<<endl;
return 0;
}



5.编程计算s=1!+2!+3!+......n!(其中n为整数,n!表示计算n阶乘),要求使用两种以上的方法。

while 语句

#include<iostream>
using namespace std;
int main()
{
int n,i=1,j=1,s=0;
cout<<"请输入整数n:"<<endl;
cin>>n;
while(i<=n)
{
j*=i;
s+=j;
i++;

}

cout<<"s=1!+2!+...+n!="<<s<<endl;
return 0;
}



for语句

#include<iostream>
using namespace std;
int main()
{
int  n,i,j=1,s=0;
cout<<"输入一个正整数n"<<endl;
cin>>n;
for(i=1;i<=n;i++)
{
j*=i;
s+=j;
}

cout<<"s=1!+2!+...+n!="<<s<<endl;
return 0;
}



6.猴子吃苹果问题

#include<iostream>
using namespace std;
int main()
{
int n=1,i=9;
for(i;i>=1;i--)
n=(n+1)*2;
cout<<"猴子一共摘了"<<n<<"个苹果\n";
return 0;
}




7.计算s
=a+aa+aaa+aa...a(n个)的值

#include<iostream>
using namespace std;
int main()
{
int i,a,n,s,b;
cout<<"计算s
=a+aa+aaa+aa...a(n个)的值"<<endl;
cout<<"输入数字a(0~9)=";
cin>>a;
cout<<"输入位数n=";
cin>>n;
b=a;
s=a;
for(i=1;i<n;i++)
{
b*=10;
a=b+a;
s+=a;
}
cout<<"s["<<n<<"]="<<s<<endl;
return 0;
}



8.打印九九乘法表。

#include<iostream>
using namespace std;
int main()
{
int x,y;
for(x=0;x<=9;x++)
for(y=1;y<=x;y++)
{
cout<<y<<"*"<<x<<"="<<y*x<<" ";
if(y==x)
cout<<endl;
}
return 0;
}



9.两个羽毛队进行单打比赛

#include<iostream>
using namespace std;
int main()
{
char three,four,five;
for(three='A';three<='C';three++)
for(four='A';four<='C';four++)
for(five='A';five<='C';five++)
{
if(three!=four&&three!=five&&four!=five)
if(three!='A'&&five!='A'&&five!='C')
{
cout<<"张三VS"<<three<<endl;
cout<<"李四VS"<<four<<endl;
cout<<"王五VS"<<five<<endl;
}
}
cout<<"A是陈六,B是赵七,C是宋八"<<endl;
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: