您的位置:首页 > 其它

第四周作业

2014-04-05 12:37 162 查看

 实验作业

1.完成课本每一个编程题。要求先画出流程算法图或N-S图,然后编程实现

求自然对数e的近似值

#include  <stdio.h>
void main( )
{int i;
double t,e;
i=0;
t=1;
e=1;
while(t>0.00001)
{i=i+1;
t=t/i;
e=e+t;}
printf ("e=%.6lf\n",e);
}


求圆周率的近似值
#include <stdio.h>

int main()
{
double pi = 0;
int i, k = 1;
for ( i = 0; i < 1000000; ++i ) {
pi += 1.0 / (2*i+1)*k;
k = -k;
}
printf( "π=%f\n", pi*4 );
}


数据分类

#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"please enter a number"<<endl;
cin>>n;
if(n<=10)
cout<<n<<" is less than 10"<<endl;
else if(n<=100)
cout<<n<<" is 10 to 100"<<endl;
else if(n<=1000)
cout<<n<<" is 100 to 1000"<<endl;
else
cout<<n<<" is more than 1000"<<endl;

return 0;
}


输出图形

#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"输入菱形边长"<<endl;
cin>>n;
cout<<""<<endl;
for(int i=1;i<=n;++i)
{
for(int j=1;j<=n-i;++j)
cout<<" ";
for(j=1;j<=i;++j)
cout<<"* ";
cout<<endl;
}
for( i=n;i>=1;--i)
{
for(int j=1;j<=n-i;++j)
cout<<" ";
for(j=1;j<=i;++j)
cout<<"* ";
cout<<endl;
}
return 0;
}


求最大的n值

#include<iostream>
using namespace std;
int main()
{

int n=0,a=0;
while(a<=1000)
{
n++;
a+=n*n;
}
cout<<"满足条件的最大值";
cout<<"n="<<n<<endl;
return 0;
}


百万富翁派钱啦

#include<iostream>
using namespace std;
int main()
{
int i=0;
long unsigned int a=0;
double b=0,s=0.01;
while(i!=30)
{
i++;
a+=100000;
s=s*2;
b+=s;
}
cout<<"一个月中"<<endl;
cout<<"陌生人给了百万富翁"<<a<<"元钱"<<endl;
cout<<"百万富翁给了陌生人"<<fixed<<b<<"元钱"<<endl;
return 0;
}


打印九九乘法表

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
int i=1;
int t=1;
while(i<=9)
{

while(t<=i)
{
cout<<t<<"*"<<i<<"="<<i*t<<" ";
t++;
}
t=1;
cout<<endl;
i++;
}
return 0;
}


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

#include <iostream>
using namespace std;
int main()
{
int x,y,z;
for(x=0;x<=20;++x)
for(y=0;y<=33;++y)
{
z=100-x-y;
if(5*x+3*y+z/3==100&&z%3==0)
cout<<"鸡翁"<<x<<"只,鸡母"<<y<<"只,鸡雏"<<z<<"只。"<<endl;
}
return 0;
}


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

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n,count=0,i,m=0;
cout<<"请输入一个整数:"<<endl;
cin>>n;
while(n!=0)
{
i=n%10,m+=i,cout<<setw(8)<<i;
n=n/10,count++;
}
cout<<"        位数:"<<count<<"    各数位和:"<<m<<endl;
return 0;

}


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

#include <iostream>

using namespace std;

int main()
{
float x,y;//坐标
cout << "请输入该点横坐标:"<<endl;
cin >> x;
cout << "请输入该点纵坐标:"<< 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.编程计算s=1!+2!+3!+......n!(其中n为整数,n!表示计算n阶乘),要求使用两种以上的方法。

#include<iostream>
using namespace std;
int main()
{
int sum=0,j=1,n;
cout<<"请输入n的值: "<<endl;
cin>>n;
for(int i=1;i<=n;i++)
{
j=j*i;
sum=sum+j;
}
cout<<"n="<<n<<endl<<"1!+2!+3!+...+n!="<<sum<<endl;
return 0;
}


#include <iostream>
using namespace std;
int main()
{
int n,s=0,i,fac;
cout << "请输入一个整数n:"<<endl;
cin >> n;
for (;n>0;n--)
{
fac=1;
for(i=n;i>0;i--)//求N!
fac*=i;
s+=fac;//求和
}
cout <<"s=1!+2!+3!……+n!="<<s<<endl;
return 0;
}


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

#include<iostream>
using namespace std;
int main()
{
int day=1,n=1;
for(;day<=9;day++)
{
n=2*(n+1);

}
cout<<"苹果数为:"<<n<<endl;

4000
return 0 ;

}


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

#include<iostream>
using namespace std;
void main()
{
cout<<"输入数值a和个数n:";
double a,s=0;
int n;
cin>>a>>n;
for(int i=1;i<=n;i++)
{
double t=1.0;
for(int j=1;j<=i;j++)
t=t*a;
s +=t;
}
}


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

#include <iostream>
using namespace std;
int main()
{
char Z_three,L_four,W_five;
for(Z_three='A';Z_three<='C';Z_three++)
for(L_four='A';L_four<='C';L_four++)
for(W_five='A';W_five<='C';W_five++)
{
if(Z_three!=L_four&&Z_three!=W_five&&L_four!=W_five)
if(Z_three!='A'&&W_five!='A'&&W_five!='C')
{
cout << "张三--"<<Z_three<<endl;
cout << "李四--"<<L_four<<endl;
cout << "王五--"<<W_five<<endl;
}
}
return 0;
}


10.错误信息收集
TZK.cpp

F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(8) : error C2059: syntax error : '>>'

执行 cl.exe 时出错.
TZK.obj - 1 error(s), 0 warning(s)输出的时候忘记定义输出什么
TZK.cpp

F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(17) : fatal error C1004: unexpected end of file found

执行 cl.exe 时出错.
TZK.obj - 1 error(s), 0 warning(s)忘记了},因为与上一个函数的混淆了
TZK.cpp

F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(16) : error C2450: switch expression of type 'double' is illegal

        Integral expression required

F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(16) : error C2143: syntax error : missing ':' before ';'

F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(16) : error C2051: case expression not constant

F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(16) : warning C4060: switch statement contains no 'case' or 'default' labels

F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(19) : fatal error C1004: unexpected end of file found

执行 cl.exe 时出错.switch函数不完整
TZK.cpp

F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(12) : error C2018: unknown character '0xa3'

F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(12) : error C2018: unknown character '0xa9'

F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(13) : error C2146: syntax error : missing ')' before identifier 't'

F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(16) : fatal error C1004: unexpected end of file found

执行 cl.exe 时出错.
TZK.obj - 1 error(s), 0 warning(s)输入的时候输入法没有在英文输入状态


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