P61,练习2解答<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
1
,
#include
<
iostream
>
using
namespace
std;
![None.gif](/Images/OutliningIndicators/None.gif)
//
筛法求素数
void
FilterPrime(
int
n)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
bool* isPrimes = new bool[n+1];
for(int i=2;i<=n;++i)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
isPrimes[i] = true;
}
isPrimes[2] = true;
for(int j=2;j<=n;++j)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(isPrimes[j]==true)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(int m=2;j*m<=n;++m)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
isPrimes[j*m] = false;
}
}
}
for(int k=2;k<=n;++k)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(isPrimes[k]==true)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout<<k<<"是素数"<<endl;
}
}
delete [] isPrimes;
}
int
main()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int num;
cin>>num;
FilterPrime(num);
system("pause");
return 0;
}
2
,#include
<
iostream
>
using
namespace
std;
![None.gif](/Images/OutliningIndicators/None.gif)
void
process(
int
num)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
double item=1,sum=1;
int count=1;
int sign = 1;
while(abs(item)>1e-8)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
item=item*num*sign/count;
sum+=item;
sign*=-1;
count++;
}
cout<<sum<<endl;
}
int
main()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int x;
cin>>x;
process(x);
system("pause");
return 0;
}
3
,
#include
<
iostream
>
using
namespace
std;
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
int
main()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
__int64 a[13];
a[1] = 1;
for(int i=2;i<=12;++i)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
a[i] = a[i-1]*i;
cout<<"a["<<i<<"]="<<a[i]<<endl;
}
__int64 sum = 0;
for(int j=1;j<=12;++j)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sum+=a[j];
}
cout<<sum<<endl;
system("pause");
return 0;
}
4
,
#include
<
iostream
>
#include
<
cmath
>
using
namespace
std;
![None.gif](/Images/OutliningIndicators/None.gif)
bool
isNarcissusNum(
int
num)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{//是否是水仙数
int sum = 0,tmp,oldNum = num;
while(num!=0)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tmp = num%10;
sum += tmp*tmp*tmp;
num = num/10;
}
if(sum==oldNum)
return true;
else
return false;
}
bool
isPerfectNum(
int
num)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{//是否是完数
int sum=1;
for(int i=2;i<=num/2;++i)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(num%i==0)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sum+=i;
}
}
if(sum==num)
return true;
else
return false;
}
int
main()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
for(int i=1;i<=1000;++i)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(isPerfectNum(i))
cout<<i<<"是完数"<<endl;
}
system("pause");
return 0;
}
5
,
#include
<
iostream
>
#include
<
cmath
>
#include
<
deque
>
#include
<
stack
>
using
namespace
std;
![None.gif](/Images/OutliningIndicators/None.gif)
bool
isPrimer(
int
num)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
for(int i=2;i<=sqrt(static_cast<double>(num));++i)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(num%i==0)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
}
return true;
}
![None.gif](/Images/OutliningIndicators/None.gif)
bool
isPairNum(
int
num)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
deque<int> q;
stack<int> s;
int oldNum = num;
while(num!=0)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
q.push_back(num%10);
s.push(num%10);
num/=10;
}
while(!s.empty()&&!q.empty())
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(s.top()!=q.front())
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
s.pop();
q.pop_front();
}
}
return true;
}
int
main()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
for(int i=100;i<=999;++i)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(isPrimer(i)&&isPairNum(i))
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout<<"find a num: "<<i<<endl;
}
}
system("pause");
return 0;
}
6
,
#include
<
iostream
>
using
namespace
std;
![None.gif](/Images/OutliningIndicators/None.gif)
int
main()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int preNum=1,nextNum;
for(int i=9;i>=1;--i)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
nextNum = (preNum+1)*2;
preNum = nextNum;
}
cout<<nextNum<<endl;
system("pause");
return 0;
}
7
,
#include
<
iostream
>
using
namespace
std;
![None.gif](/Images/OutliningIndicators/None.gif)
int
main()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int i,j;
for(i=1;i<=10;++i)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
![None.gif](/Images/OutliningIndicators/None.gif)
int
main()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int i,j;
for(i=1;i<=10;++i)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
![None.gif](/Images/OutliningIndicators/None.gif)
int
main()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int i,j;
for(i=1;i<=10;++i)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(j=1;j<=i-1;++j)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout<<" ";
}
for(j=1;j<=21-2*i;++j)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
![None.gif](/Images/OutliningIndicators/None.gif)
int
cow(
int
n)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{//递归
if(n<=4)
return 1;
else
return cow(n-1)+cow(n-4);
}
int
cow2(
int
n)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{//非递归
int a1,a2,a3,a4;
a1=a2=a3=a4=1;
int result = 0;
if(n<=4)
return 1;
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(int i=5;i<=n;++i)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
result = a1+a4;
a1 = a2;
a2 = a3;
a3 = a4;
a4 = result;
}
return result;
}
}
int
main()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int n;
cin>>n;
cout<<"cows are: "<<cow2(n)<<endl;
system("pause");
return 0;
}
11
,
#include
<
iostream
>
using
namespace
std;
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
double
Height(
double
n,
int
level,
double
&
bound)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if(level==1)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bound = n/2;
return n;
}
else
return n+n/2+Height(n/2,--level,bound);
}
int
main()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
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;
![None.gif](/Images/OutliningIndicators/None.gif)
int
main()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int i,j,k;
for(i=1;i<=9;++i)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(j=1;j<=17;++j)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(k=1;k<=85;++k)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(10*i+5*j+k==100)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout<<i<<"张10元,"<<j<<"张5元,"<<k<<"张1元"<<endl;
}
}
}
}
system("pause");
return 0;
}
13
,
#include
<
iostream
>
using
namespace
std;
![None.gif](/Images/OutliningIndicators/None.gif)
int
main()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int data[] =
{0,1,2,3,4,5,6};
int i,j,count;
for(i=1;i<=6;++i)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout<<i<<'\t';
for(j=i-1,count=1;count<=7;++count)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
if(j>=7)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
j=j%7;
}
cout<<data[j]<<" ";
j++;
if(count==7)
cout<<'\n';
}
}
system("pause");
return 0;
}
![None.gif](/Images/OutliningIndicators/None.gif)
#include
<
iostream
>
using
namespace
std;
![None.gif](/Images/OutliningIndicators/None.gif)
int
main()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int data[] =
{0,1,2,3,4,5,6};
int i,j,count;
for(i=1;i<=6;++i)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(j=1;j<=7;++j)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout<<"("<<i<<","<<j<<")"<<" ";
if(j==7)
cout<<'\n'<<endl;
}
}
system("pause");
return 0;
}