C与大数据结构基础简明教程课后习题问题详解.doc
wordC+与数据结构根底简明教程课后习题答案 1-7-1/输出一个数的10倍#include <iostream>using namespace std;int main() cout<<"Please input an interger:n" int a; cin>>a; cout<<"result="<<a*10<<endl; return 0;1-7-2/输出两个数中的较小的#include <iostream>using namespace std;int main() double x,y,min; cout<<"please input two numbers"<<endl; cin>>x>>y; min=(x>y?y:x); cout<<"the smaller is"<<' '<<min<<endl; return 0;1-7-3/输入半径计算周长和面积#include <iostream>using namespace std;int main() cout<<"请输入半径n" double rad; cin>>rad; double c,s; c=2*3.14*rad; s=3.14*rad*rad; cout<<"周长是"<<c<<endl; cout<<"面积是"<<s<<endl; return 0;1-7-4/将10000秒化成小时分钟秒#include <iostream>using namespace std;int main() int hour,min,second; hour=10000/3600; min=(10000%3600)/60; second=10000-hour*3600-60*min; cout<<hour<<"hours"<<min<<"minutes"<<second<<"seconds"<<endl; return 0;1-7-5/将一个三位数倒叙输出#include <iostream>using namespace std;int main()int num,num_bai,num_shi,num_ge;cout<<"请输入一个三位数"cin>>num;num_bai=num/100;num_shi=(num-num_bai*100)/10;num_ge=num-num_bai*100-num_shi*10;cout<<num_ge<<endl;cout<<num_shi<<endl;cout<<num_bai<<endl;return 0;2-3/简单的输入输出#include <iostream>using namespace std;int main() double y,n; cout<<"please input a numbern" cin>>n; if (n>0) y=n+1; if (n<0) y=n*n; if (n=0) y=0; cout<<"the result is:"<<' '<<y; return 0;2-4/输出学生学号和对应的成绩等级#include <iostream>using namespace std;int main() double fenshu; int xuehao; char dengji; int i; for (i=1;i<=10;i+) onerror:cout<<"please input student scoren" cin>>fenshu; if (fenshu>=90&&fenshu<=100) dengji='A' if (fenshu>=80&&fenshu<90) dengji='B' if (fenshu>=600&&fenshu<80) dengji='C' if (fenshu>=0&&fenshu<60) dengji='D' if (fenshu<0) cout<<"error" goto onerror; if (fenshu>100) cout<<"error" goto onerror; cout<<"the id is"<<i<<endl; cout<<"the score is"<<dengji<<endl; return 0;2-5/判断输入字符类别#include <iostream>using namespace std;int main()char zifu;int ascii;cout<<"please input a characteristicn"cin>>zifu;ascii=zifu;if (ascii>=48&&ascii<=57) cout<<"数字n" goto end; if (ascii>=97&&ascii<=122) cout<<"小写字母n" goto end; if (ascii>=65&&ascii<=90) cout<<"大写字母n" goto end; cout<<"其他字符"cout<<endl;end:return 0;2-6/求圆周率的近似值#include <iostream>#include <iomanip>using namespace std;int main()int xishu=-1;double danshu=1.0;int i=1;double sifenzhipi=0;do xishu=xishu*(-1); danshu=1.0/(2*i-1); sifenzhipi=sifenzhipi+xishu*danshu; i+;while(danshu>=0.000001);cout<<setprecision(15)<<sifenzhipi*4<<endl;return 0;2-7/输出斐波那契数列前40个数,以8*5的格式输出#include <iostream>#include <iomanip>using namespace std;int main() int b=2; int feibo1=0; int feibo2=1; cout<<setw(9)<<'0'<<setw(9)<<'1' for(int i=1;i<=38;i+) int a=feibo2; feibo2=feibo1+feibo2; feibo1=a; cout<<setw(9)<<feibo2; b=b+1; if(b%8=0) cout<<endl; return 0;2-8/统计输入的元音字母个数#include <iostream>using namespace std;int main() char zimu; int a,e,i,o,u; a=e=u=i=o=0; begin:cin>>zimu; switch(zimu) case 'a' :a+;break; case 'e' :e+;break; case 'i' :i+;break; case 'o' :o+;break; case 'u' :u+;break; case '#' :goto end; goto begin;end:cout<<a<<endl<<e<<endl<<i<<endl<<o<<endl<<u<<endl<<a+e+i+o+u; return 0;2-10/计算5!+6!+7!#include <iostream>using namespace std;int main() int i,j; int sum=0; for (i=5;i<=7;i+) int jiecheng=1; for (j=1;j<=i;j+) jiecheng=jiecheng*j; sum=sum+jiecheng; cout<<sum; return 0;2-11/从2开始100个素数#include <iostream>using namespace std;int main() int num=1; int sushu=0; start:; num+; for (int i=2;i<=num;i+) if(num%i=0) break; if (i=num) cout<<num<<endl; sushu+=1; if(sushu!=100) goto start; return 0;2-12/百钱买百鸡问题#include <iostream>using namespace std;int main() int x,y,z; for(x=0;x<=100;x+) for(y=0;y<=100;y+) for(z=0;z<=100;z+) if (x+y+z=100&&5*x+3*y+z/3=100&&z%3=0) cout<<x<<'t'<<y<<'t'<<z<<endl; return 0;2-13#include <iostream>using namespace std;int main() int a,b,c,d,e; c=0; cin>>a; cout<<"输入"<<a<<endl<<"该数的三次方是"<<a<<"个连续奇数" b=1; e=a*a*a; while (c!=e) d=b; c=0; for (int i=1;i<=a;i+) c=c+d; d=d+2; b=b+2; /cout<<b-2; int f=b-4; for (int j=1;j<=a;j+) f=f+2; cout<<f<<' ' cout<<"的和n" return 0;2-14/输出四方*形状#include <iostream>using namespace std;int main() int i,j; for (i=1;i<=9;i+) j=1; for(j=1;j<=9;j+) if(i+j>=6&&i+j<=14&&j-i<=4&&i-j<=4) cout<<'*' else cout<<' ' cout<<endl; return 0;3-1-1/向数组a中输入10个整数,求其中的最大值,最小值和10个数的算术平均值#include <iostream>using namespace std;int main() int a10; for(int i=0;i<=9;i+) cin>>ai; int max=a0,min=a0,sum=a0; for(int j=0;j<=9;j+) sum=sum+aj; max=(max>aj?max:aj); min=(min<aj?min:aj); double ave=sum/10.00; cout<<max<<endl<<min<<endl<<ave<<endl; return 0; 3-1-2#include <iostream>using namespace std;int main() int score10; int sum=0,a=0,b=0; for(int i=0;i<=9;i+) cin>>scorei; sum=sum+scorei; double ave=sum/10.0; for(int j=0;j<=9;j+) if(scorej>ave) a+; if(scorej<60) b+; cout<<"成绩大于平均值的人数是"<<a<<endl<<"成绩小于60的人数"<<b<<endl; return 0;3-3-1#include <iostream>using namespace std;int main() int a10,sum=0,min,max; double ave; int *ptoa=a; for (int i=0;i<=9;i+) cin>>*(a+i); sum+=*(a+i); min=*(a); max=*(a); for (int j=1;j<=9;j+) min=(min<*(a+j)?min:*(a+j); max=(max>*(a+j)?max:*(a+j); cout<<max<<endl<<min<<endl; ave=sum/10.0; cout<<ave<<endl; return 0;3-3-2#include <iostream>using namespace std;int main() int a10,sum=0,j=0,c=0; /定义数组a,sum是各学生成绩之和,j是小于60的个数,c是大于平均值的个数 double ave=0; /定义平均值 int *ptoa=a; /使用指针 for (int i=0;i<=9;i+) /输入并算和 ,统计小于60的 cin>>*(ptoa+i); sum+=*(ptoa+i); if (*(ptoa+i)<60) j+; ave=sum/10.0; /计算平均值 /cout<<ave<<endl; /可以输出平均值 for (int k=0;k<=9;k+) /计算大于平均值的 if (*(ptoa+k)>ave) c+; cout<<c<<endl<<j; return 0;3-3-3#include <iostream>using namespace std;int main() char string100; cin>>string; int i=0,an=0,in=0; cout<<"您输入的字符串" while (stringi!='0') if(stringi='a') an+; if(stringi='i') in+; cout<<stringi; i+; cout<<endl<<an<<endl<<in<<endl; return 0;3-3-3a#include <iostream>using namespace std;int main() char a100; /定义存放字符串的数组 char *ptoa=a; /使用指针 int i=0,an=0,in=0; /an,in分别为a,i的个数 cin>>a; while (*(ptoa+i)!='0') /a【i】 cout<<*(ptoa+i); switch (*(ptoa+i) /判断 case 'a' : an+;break; case 'i' : in+;break; i+; cout<<endl<<an<<endl<<in; return 0;3-3-4#include <iostream>using namespace std;int main() int a33; for (int i=0;i<=2;i+) for(int j=0;j<=2;j+) cin>>*(*(a+i)+j); cout<<*(*(a+0)+0)+*(*(a+1)+1)+*(*(a+2)+2)<<endl; cout<<*(*(a+0)+2)+*(*(a+1)+1)+*(*(a+2)+0)<<endl; return 0;3-3-5v1#include <iostream>using namespace std;int main() int a11=2,4,5,8,12,14,16,18,20,30,0,b,i,m,j; cin>>b; a10=b; if (b<2) m=0; goto end; if (b>30) goto end2; for(i=0;i<=9;i+) if (ai+1>b) m=i+1; goto end; end:; for (j=10;j>=m+1;j-) aj=aj-1; am=b;end2:; for (int k=0;k<=10;k+) cout<<ak<<endl; return 0;3-3-5v2#include <iostream>using namespace std;int main() int i,j,temp; int a11=2,4,5,8,12,14,16,18,20,30; cin>>a10; for (i=0;i<=10;i+) for(j=0;j<=10-i-1;j+) if(aj>aj+1) temp=aj+1; aj+1=aj; aj=temp; for (int k=0;k<=10;k+) cout<<ak<<' ' cout<<'n' return 0;3-3-5v3#include <iostream>using namespace std;int main() int a11=2,4,5,8,12,14,16,18,20,30,num,i,j; cout<<"请输入一个整数" cin>>num; if (num>a9) a10=num; else for (i=0;i<=11;i+) if(ai>num) for(j=9;j>=i;j-) aj+1=aj; ai=num; break; for (i=0;i<=10;i+) cout<<ai<<' ' return 0;3-3-5final#include<iostream.h>void main() int a11=2,4,5,8,12,14,16,18,20,30,b,i=0,j=10; cin>>b; while(ai<b&&i<=9)i+; while(j>i) aj=aj-1; j-; ai=b; cout<<"输入插入后的结果:" for(i=0;i<11;i+)cout<<ai<<" " cout<<endl;3-3-6#include <iostream>using namespace std;int main() int a53,s5=0,0,0,0,0,temp; for (int i=0;i<=4;i+) cout<<"请输入第"<<i+1<<"行的数据"<<endl; for (int j=0;j<=2;j+) cin>>*(*(a+i)+j); *(s+i)+=*(*(a+i)+j); /* /注释掉的局部可以输出每行数据之和 for (int k=0;k<=4;k+) cout<<*(s+k)<<endl; */ for (int m=0;m<=4;m+) for (int n=0;n<=4-m-1;n+) if (*(s+n)>*(s+n+1) temp=*(s+n+1); *(s+n+1)=*(s+n); *(s+n)=temp; for (int l=0;l<=4;l+) cout<<endl<<*(s+l); cout<<endl; system("pause"); return 0;4-3#include <iostream>using namespace std;int fun1(int x,int y);int fun2(int x,int y);int fun3(int x,int y);int fun4(int x,int y); int main() int a,b; cout<<"输入两个数" cin>>a>>b; cout<<"两个数的和的