n 实验作业
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阶乘),要求使用两种以上的方法。
#include<iostream>
using namespace std;
int main()
{
cout<<"This is a problem.\n";
cout<<"This is a problem.\n";
cout<<"This "
<<"is "
<<"a "
<<"problem.\n";
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a=10,b=20,c=30;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"c = "<<c<<endl;
cout<<"(a+c)/(2*b) = "<<(a+c)/(2*b)<<"\n";
return 0 ;
}
#include<iostream>
using namespace std;
int main()
{
int n1,n2;
cout<<"Please input two numbers:";
cin>>n1>>n2;
cout<<"the average of the two numbers is "<<(n1+n2)/2.0<<"\n";
return 0;
}
#include<iostream>
using namespace std;
int main()
{
cout<<"广东海洋大学信息学院"<<endl;
cout<<"自动化系"<<endl;
return 0 ;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a=10,b=1000;
cout<<setw(5)<<a<<endl;
cout<<setw(2)<<b<<endl;
return 0;
}
例3.6
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int x=30,y=300,z=1024;
cout<<"Decimal:"<<'\t'
<<"x = "<<x<<"\t\t"
<<"y = "<<y<<"\t\t"
<<"z = "<<z<<endl;
cout<<"Octal:"<<"\t\t"<<oct
<<"x = "<<x<<"\t\t"
<<"y = "<<y<<"\t\t"
<<"z = "<<z<<endl;
cout<<"Hexadecimal:"<<'\t\t'<<hex
<<"x = "<<x<<"\t\t"
<<"y = "<<y<<"\t\t"
<<"z = "<<z<<endl;
cout<<setiosflags(ios::uppercase);
cout<<"Hexadecimal:"<<'\t'
<<"x = "<<x<<"\t\t"
<<"y = "<<y<<"\t\t"
<<"z = "<<z<<endl;
cout<<resetiosflags(ios::uppercase);
cout<<"Hexadecimal:"<<'\t'
<<"x = "<<x<<"\t\t"
<<"y = "<<y<<"\t\t"
<<"z = "<<z<<endl;
cout<<"Decimal:"<<'\t'<<dec;
cout<<"x = "<<x<<"\t\t"
<<"y = "<<y<<"\t\t"
<<"z = "<<z<<endl;
return 0;
}
2.百钱百鸡问题:
#include<math.h>
#include<stdio.h>
#include<iostream>
int main()
{
int x,y,z,j=0; //**x,y,z,分别代表鸡翁,鸡母,鸡雏**//
printf("百钱百鸡100元可买的鸡翁,鸡母,鸡雏的数量.\n");
for(x=0;x<=20;x++) //*鸡翁数:100/5=20,最多可买20只鸡翁*//
for(y=0;y<=33;y++) //*鸡母数y在0~33变化*//
{
z=100-x-y; //*鸡雏数z的值受x,y的值的制约*//
if(z%3==0&&5*x+3*y+z/3==100) //*验证取z值的合理性及得到一组解的合理性*//
printf("%2d:鸡翁=%2d 鸡母=%2d 鸡雏=%2d\n",++j,x,y,z);
}
}
3.
#include <iostream>
using namespace std;
int main()
{
int n,i,j,k,result;
cout<<"输入一个三位数:";
cin>>n;
int temp = n;
i = temp/100;
temp-=i*100,j=temp/10;
k = temp-j*10;
result = k*100+j*10+i;
cout<<n<<"的逆转数是:"<<result<<endl;
return 0 ;
}
#include <math.h>
#include <iostream>
using namespace std;
int main()
{
int n,m,i,j,k,result;
cout<<"输入一个数:";
cin>>n;
int temp = n;
i = temp/100;
temp-=i*100,j=temp/10;
k = temp-j*10;
result = i+j+k;
m=log10(n)+1;
cout<<"n的位数是:"<<"\t";
cout<<m<<endl;
cout<<"各个数位上的数之和是:"<<result<<endl;
return 0 ;
}
4.第四题觉得怪怪的,不知道怎么说,所以干脆再看看,再做~
5.求n阶乘的和s的和:方法一:
#include <math.h>
#include <iostream>
using namespace std;
int main()
{
int n,s=1,sum=0;
for(n=1;n<=10;n++)
{
s=s*n;
sum=sum+s;
}
cout<<"n阶乘的和sum="<<sum<<endl;
return 0;
}
方法2:
#include<iostream>
using namespace std;
int main()
{
int sum=0,n=1,s=1;
do
{
s=s*n;
sum=sum+s;
n++;
}
while (n<=10);
cout<<"sum = "<<sum<<endl;
return 0;
}
其实觉得两种方法都差不多的~~~