P61,练习2解答
1,
#include <iostream>
using namespace std;
//筛法求素数
void FilterPrime(int n)
{
bool* isPrimes = new bool[n+1];
for(int i=2;i<=n;++i)
{
isPrimes[i] = true;
}
isPrimes[2] = true;
for(int j=2;j<=n;++j)
{
if(isPrimes[j]==true)
{
for(int m=2;j*m<=n;++m)
{
isPrimes[j*m] = false;
}
}
}
for(int k=2;k<=n;++k)
{
if(isPrimes[k]==true)
{
cout<<k<<"是素数"<<endl;
}
}
delete [] isPrimes;
}
int main()
{
int num;
cin>>num;
FilterPrime(num);
system("pause");
return 0;
}
2,#include <iostream>
using namespace std;
void process(int num)
{
double item=1,sum=1;
int count=1;
int sign = 1;
while(abs(item)>1e-8)
{
item=item*num*sign/count;
sum+=item;
sign*=-1;
count++;
}
cout<<sum<<endl;
}
int main()
{
int x;
cin>>x;
process(x);
system("pause");
return 0;
}
3,
#include <iostream>
using namespace std;
int main()
{
__int64 a[13];
a[1] = 1;
for(int i=2;i<=12;++i)
{
a[i] = a[i-1]*i;
cout<<"a["<<i<<"]="<<a[i]<<endl;
}
__int64 sum = 0;
for(int j=1;j<=12;++j)
{
sum+=a[j];
}
cout<<sum<<endl;
system("pause");
return 0;
}
4,
#include <iostream>
#include <cmath>
using namespace std;
bool isNarcissusNum(int num)
{//是否是水仙数
int sum = 0,tmp,oldNum = num;
while(num!=0)
{
tmp = num%10;
sum += tmp*tmp*tmp;
num = num/10;
}
if(sum==oldNum)
return true;
else
return false;
}
bool isPerfectNum(int num)
{//是否是完数
int sum=1;
for(int i=2;i<=num/2;++i)
{
if(num%i==0)
{
sum+=i;
}
}
if(sum==num)
return true;
else
return false;
}
int main()
{
for(int i=1;i<=1000;++i)
{
if(isPerfectNum(i))
cout<<i<<"是完数"<<endl;
}
system("pause");
return 0;
}
5,
#include <iostream>
#include <cmath>
#include <deque>
#include <stack>
using namespace std;
bool isPrimer(int num)
{
for(int i=2;i<=sqrt(static_cast<double>(num));++i)
{
if(num%i==0)
{
return false;
}
}
return true;
}
bool isPairNum(int num)
{
deque<int> q;
stack<int> s;
int oldNum = num;
while(num!=0)
{
q.push_back(num%10);
s.push(num%10);
num/=10;
}
while(!s.empty()&&!q.empty())
{
if(s.top()!=q.front())
{
return false;
}
else
{
s.pop();
q.pop_front();
}
}
return true;
}
int main()
{
for(int i=100;i<=999;++i)
{
if(isPrimer(i)&&isPairNum(i))
{
cout<<"find a num: "<<i<<endl;
}
}
system("pause");
return 0;
}
6,
#include <iostream>
using namespace std;
int main()
{
int preNum=1,nextNum;
for(int i=9;i>=1;--i)
{
nextNum = (preNum+1)*2;
preNum = nextNum;
}
cout<<nextNum<<endl;
system("pause");
return 0;
}
7,
#include <iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=10;++i)
{
for(j=1;j<=10-i;++j)
cout<<" ";
for(j=1;j<=2*i-1;++j)
cout<<"%";
cout<<endl;
}
for(i=1;i<=9;++i)
{
for(j=1;j<=i;++j)
cout<<" ";
for(j=1;j<=19-2*i;++j)
cout<<"%";
cout<<endl;
}
system("pause");
return 0;
}
8,
#include <iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=10;++i)
{
for(j=1;j<=10-i;++j)
cout<<" ";
for(j=1;j<=i;++j)
cout<<"#";
for(j=1;j<=6;++j)
cout<<" ";
for(j=1;j<=i;++j)
cout<<"$";
cout<<endl;
}
system("pause");
return 0;
}
9,
#include <iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=10;++i)
{
for(j=1;j<=i-1;++j)
{
cout<<" ";
}
for(j=1;j<=21-2*i;++j)
{
if(j%2==0)//偶数位
cout<<"T";
else
cout<<"S";
}
cout<<endl;
}
system("pause");
return 0;
}
10,母牛问题
时间(年) 未成熟母牛(头) 成熟母牛(头) 母牛总数(头)
1 1 0 1
2 1 0 1
3 1 0 1
4 0 1 1
5 1 1 2
6 2 1 3
7 3 1 4
8 3 2 5
9 4 3 7
10 6 4 10
从图中可以看出A1=1,A2=2,A3=3,A4=4,A5=A4+A1等,则依此规律,An=An-1+An-4.这个规律很重要的,因为我们可以从中得到类似问题的规律(例如:从出生起第m个年头开始每年生一头母牛,则得到的规律是Fn=Fn-1+Fn-m (n>m)).
#include <iostream>
using namespace std;
int cow(int n)
{//递归
if(n<=4)
return 1;
else
return cow(n-1)+cow(n-4);
}
int cow2(int n)
{//非递归
int a1,a2,a3,a4;
a1=a2=a3=a4=1;
int result = 0;
if(n<=4)
return 1;
else
{
for(int i=5;i<=n;++i)
{
result = a1+a4;
a1 = a2;
a2 = a3;
a3 = a4;
a4 = result;
}
return result;
}
}
int main()
{
int n;
cin>>n;
cout<<"cows are: "<<cow2(n)<<endl;
system("pause");
return 0;
}
11,
#include <iostream>
using namespace std;
double Height(double n,int level,double& bound)
{
if(level==1)
{
bound = n/2;
return n;
}
else
return n+n/2+Height(n/2,--level,bound);
}
int main()
{
double height = 100.0,bound;
int times=10;
cout<<"第次落地时,经过距离: "<<Height(height,times,bound)<<endl;
cout<<"此时落地后反弹: "<<bound<<endl;
system("pause");
return 0;
}
12,
#include <iostream>
using namespace std;
int main()
{
int i,j,k;
for(i=1;i<=9;++i)
{
for(j=1;j<=17;++j)
{
for(k=1;k<=85;++k)
{
if(10*i+5*j+k==100)
{
cout<<i<<"张10元,"<<j<<"张5元,"<<k<<"张1元"<<endl;
}
}
}
}
system("pause");
return 0;
}
13 ,
#include <iostream>
using namespace std;
int main()
{
int data[] = {0,1,2,3,4,5,6};
int i,j,count;
for(i=1;i<=6;++i)
{
cout<<i<<'\t';
for(j=i-1,count=1;count<=7;++count)
{
if(j>=7)
{
j=j%7;
}
cout<<data[j]<<" ";
j++;
if(count==7)
cout<<'\n';
}
}
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int data[] = {0,1,2,3,4,5,6};
int i,j,count;
for(i=1;i<=6;++i)
{
for(j=1;j<=7;++j)
{
cout<<"("<<i<<","<<j<<")"<<" ";
if(j==7)
cout<<'\n'<<endl;
}
}
system("pause");
return 0;
}
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/05/10/1191665.html,如需转载请自行联系原作者