第一范文网 - 专业文章范例文档资料分享平台

《c++程序设计》谭浩强课后习题答案及解析 

来源:用户分享 时间:2025/5/28 22:48:39 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

.

#include using namespace std; int main()

{void godbaha(int); int n;

cout<<\ cin>>n; godbaha(n); return 0; }

void godbaha(int n) {int prime(int); int a,b;

for(a=3;a<=n/2;a=a+2) {if(prime(a)) {b=n-a;

if (prime(b)) cout<

int prime(int m) {int i,k=sqrt(m); for(i=2;i<=k;i++) if(m%i==0) break; if (i>k) return 1; else return 0; }

4.8题//递归法

#include using namespace std; int main() {int x,n;

float p(int,int);

cout<<\ cin>>n>>x;

cout<<\ cout<<\ return 0; }

float p(int n,int x)

.

.

{if (n==0) return(1); else if (n==1) return(x); else

return(((2*n-1)*x-p((n-1),x)-(n-1)*p((n-2),x))/n); }

4.9题//汉诺塔问题 #include using namespace std; int main()

{void hanoi(int n,char one,char two,char three); int m;

cout<<\ cin>>m;

cout<<\ hanoi(m,'A','B','C'); return 0; }

void hanoi(int n,char one,char two,char three) //将n个盘从one座借助two座,移到three座 {void move(char x,char y); if(n==1) move(one,three); else

{hanoi(n-1,one,three,two); move(one,three);

hanoi(n-1,two,one,three); } }

void move(char x,char y)

{cout<

#include using namespace std; int main()

{void convert(int n); int number;

cout<<\ cin>>number;

cout<<\ if (number<0)

.

.

{cout<<\

number=-number; }

convert(number); cout<

void convert(int n) //感觉根本想不出的么 {int i; char c;

if ((i=n/10)!=0) convert(i); c=n+'0'; cout<<\ }

4.11题

#include using namespace std; int main() {int f(int); int n,s;

cout<<\ cin>>n; s=f(n);

cout<<\ return 0; }

int f(int n) {;

if (n==1) return 1; else

return (n*n+f(n-1)); }

4.12题

#include #include using namespace std;

.

.

#define S(a,b,c) (a+b+c)/2

#define AREA(a,b,c) sqrt(S(a,b,c)*(S(a,b,c)-a)*(S(a,b,c)-b)*(S(a,b,c)-c)) int main() {float a,b,c;

cout<<\ cin>>a>>b>>c;

if (a+b>c && a+c>b && b+c>a)

cout<<\ else

cout<<\ return 0; }

4.14题

#include using namespace std;

//#define LETTER 1 int main() {char c; cin>>c;

#if LETTER if(c>='a' && c<='z') c=c-32;

#else if(c>='A' && c<='Z') c=c+32; #endif

cout<

4.15题

#include using namespace std; #define CHANGE 1 int main() {char ch[40];

cout<<\ gets(ch);

#if (CHANGE)

{for (int i=0;i<40;i++) {if (ch[i]!='\\0')

if (ch[i]>='a'&& ch[i]<'z'||ch[i]>'A'&& ch[i]<'Z') ch[i]+=1;

.

.

else if (ch[i]=='z'||ch[i]=='Z') ch[i]-=25; } } #endif

cout<<\ return 0; }

4.16题file

#include using namespace std; int a; int main()

{extern int power(int); int b=3,c,d,m;

cout<<\ cin>>a>>m; c=a*b;

cout<

cout<

4.16题file extern int a; int power(int n) {int i,y=1;

for(i=1;i<=n;i++) y*=a; return y; }

5.1题

#include using namespace std; int main()

{cout<<'2'<<' ';

for (int i=3;i<=100;i++) {bool t=true;

for (int a=2;a

.

《c++程序设计》谭浩强课后习题答案及解析 .doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c4tr8n37guk79ew80o94h77xpo584e200qvv_5.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top