最新_实验三_参考答案详解
- 格式:doc
- 大小:59.50 KB
- 文档页数:9
if(n==1)
return 1;
return fun(n-1)+fun(n-2);
}
5、冒泡法排序。
冒泡法排序:
#include <iostream>
#define N 10
using namespace std;
void main()
{
int i,j,t,a[N]={9,8,7,6,5,4,3,2,1,0};
for(j=0;j<N-1;j++)
for(i=0;i<N-j-1;i++)
if(a[i]>a[i+1])
{t=a[i];a[i]=a[i+1];a[i+1]=t;}
for(j=0;j<N;j++)
cout<<a[j]<<" ";
cout<<endl;
}
#include <iostream>
using namespace std;
设y为被检测的年份。可采取以下步骤:
S1:2000 y
S2:若y不能被4整除,则输出y“不是闰年”。然后转到S6
S3:若y能被4整除,不能被100整除,则输出y“是闰年”。然后转到S6
S4:若y能被100整除,又能被400整除,则输出y“是闰年”。然后转到S6
S5:输出y“不是闰年”
S6:y+1 y
k=j;
swap(a[i],a[k]);
}
}
void swap(int &a,int &b)
{
int t;
t=a;
a=b;
b=t;
}
冒泡法:只要是每挨着的两个数都要排序的就叫做冒泡法。
选择法:每一大趟选择出一个最小或最大最小的。
下面是咱上课讲的一个不太规范但最好理解的一个排序法:
void select(int a[],int n)
int k=0;
for(i=i;i<=100;i++)//另一种判断素数的方法
{
if(i==2)
{
cout<<i<<" ";
k++;
}
else
for(j=i-1;j>=2;j--)
{
if(i%j==0)
break;
if(j==2)
{
cout<<i<<" ";
k++;
if(k%3==0)
cout<<endl;
cin>>a[j];
select(a,N);
for(j=0;j<N;j++)
cout<<a[j]<<" ";
cout<<endl;
}
void select(int a[],int n)//选择法
{
int i,j,k;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(a[k]>a[j])
}
}
}
cout<<"总共个数是:"<<k<<endl;
}
(上机题)请编写函数fun,其功能是:计算并输出3到n之间所有素数的平方根之和。
答案:double fun(int n)//红色部分为上机要求填写部分
{
int m,k,I;
double s=0.0;
for(m=3;m<=n;m++)
{
k=sqrt(m);
cout<<year<<"是闰年";
else
cout<<year<<"不是闰年";
return 0;
}
bool IsLeapYear(int year)
{
if((year%4==0)&&(year%100!=0)||(year%400==0))
return true;
else
return false;
{
int i,j,t;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{t=a[i];a[i]=a[j];a[j]=t;}
}
}
用文件流完成冒泡法排序程序如下:
#include <iostream>
#include <fstream>
using namespace std;
//cout.fill('*');//这种方法与下面<<setfill('*')效果等同
cout<<setfill('*')<<left<<setw(6)<<fun(i)<<' ';//setw()只对一次输入和输出有效;left,right
}
cout<<endl;
}
int fun(int n)
{
if(n==0)
试根据上述公式编程计算 的近似值(精确到10-6)。
#include <iostream>
#include <cmath>
using namespace std;
void main()
{
float s=1.0;
for(int i=2;(1.0/(i*i))>=1.0e-6;i++)// 是float 的精度 不是<=
最简单最笨的方法来判断某一年是不是闰年:
#include <iostream>
using namespace std;
void main( )
{
int year,leap;
cout<<"Please enter a year:"<<endl;
cin>>year;
if(year%4==0)
{
if(year%100==0)
if(number==gw*gw*gw+sw*sw*sw+bw*bw*bw)
cout<<number<<endl;
}
}
另一种方法:
#include <iostream>
using namespace std;
bool num(int x) ;
void main()
{
for(int i=100;i <999;i++)
PrintfNum();
}
void PrintfNum()
{
int gw,sw,bw;
int number;
for (number=100;number<1000;number++)
{
gw=number%10;
sw=number%100/10;
bw=number/100;
//bw=number%1000/100;s=s+1.Fra bibliotek/(i*i);
float pi=sqrt(6.0*s);
cout<<i<<endl;//显示累加到的最大的n值
cout<<"pi="<<pi<<endl;//精确度越高,值越接近。试精确到e-8或更高
}
4、求素数:
概念:质数,又称素数,是只能被1或者自己整除的自然数。
比1大但不是素数的数我们称之为合数,1和0即非素数也非合数
if(i==m)//此处不同
cout<<m<<" is a prime number\n";
else
cout<<m<<" is not a prime number\n";*/
输出100以内所有素数
#include<iostream>
using namespace std;
void main()
{
int i,j;
{
if(num(i))
cout<<i<<" ";
}
}
bool num(int x)
{
int i=x%10;
int j=(x/10)%10;
int k=(x/100)%10;
if(x==i*i*i+j*j*j+k*k*k)
return true;
else
return false;
}
3、若有如下公式:
+ + +。。。。。。。+
const int N=10;//或#define N 10;
void sort(int a[],int n);
void swap(int &a,int &b);
void main()
{
int a[N];
ifstream indata("shuzu.txt");/*不鼓励用这种方法,鼓励用上课那种方法:ifstream indata;indata.open("shuzu.txt");*/