C语言程序设计课后习题答案谭浩强

合集下载

(完整版)谭浩强c程序设计课后习题答案

(完整版)谭浩强c程序设计课后习题答案

谭浩强c++程序设计课后答案娄警卫第一章1.5题#include <iostream> using namespace std; int main(){cout<<"This"<<"is"; cout<<"a"<<"C++"; cout<<"program."; return 0;1.6题#include <iostream> using namespace std; int main(){int a,b,c;a=10;b=23;c=a+b;cout<<"a+b="; cout<<c;cout<<endl;return 0;}1.7七题#include <iostream> using namespace std; int main(){int a,b,c;int f(int x,int y,int z); cin>>a>>b>>c;c=f(a,b,c);cout<<c<<endl; return 0;}int f(int x,int y,int z) {int m;if (x<y) m=x;else m=y;if (z<m) m=z;return(m);}1.8题#include <iostream>using namespace std;int main(){int a,b,c;cin>>a>>b;c=a+b;cout<<"a+b="<<a+b<<endl; return 0;}1.9题#include <iostream>using namespace std;int main(){int a,b,c;int add(int x,int y); cin>>a>>b;c=add(a,b);cout<<"a+b="<<c<<endl; return 0;}int add(int x,int y){int z;z=x+y;return(z);}第二章2.3题#include <iostream>using namespace std;int main(){char c1='a',c2='b',c3='c',c4='\101',c5='\116'; cout<<c1<<c2<<c3<<'\n';cout<<"\t\b"<<c4<<'\t'<<c5<<'\n';return 0;}2.4题#include <iostream>using namespace std;int main(){char c1='C',c2='+',c3='+';cout<<"I say: \""<<c1<<c2<<c3<<'\"';cout<<"\t\t"<<"He says: \"C++ is very interesting!\""<< '\n';return 0;}2.7题#include <iostream>using namespace std;int main(){int i,j,m,n;i=8;j=10;m=++i+j++;n=(++i)+(++j)+m;cout<<i<<'\t'<<j<<'\t'<<m<<'\t'<<n<<endl; return 0;}2.8题#include <iostream>using namespace std;int main(){char c1='C', c2='h', c3='i', c4='n', c5='a';c1+=4;c2+=4;c3+=4;c4+=4;c5+=4;cout<<"password is:"<<c1<<c2<<c3<<c4<<c5<<endl;return 0;}第三章3.2题#include <iostream>#include <iomanip>using namespace std;int main ( ){float h,r,l,s,sq,vq,vz;const float pi=3.1415926;cout<<"please enter r,h:";cin>>r>>h;l=2*pi*r;s=r*r*pi;sq=4*pi*r*r;vq=3.0/4.0*pi*r*r*r;vz=pi*r*r*h;cout<<setiosflags(ios::fixed)<<setiosflags(ios:: right)<<setprecision(2);cout<<"l= "<<setw(10)<<l<<endl;cout<<"s= "<<setw(10)<<s<<endl;cout<<"sq="<<setw(10)<<sq<<endl;cout<<"vq="<<setw(10)<<vq<<endl;cout<<"vz="<<setw(10)<<vz<<endl;return 0;}3.3题#include <iostream>using namespace std;int main (){float c,f;cout<<"请输入一个华氏温度:";cin>>f;c=(5.0/9.0)*(f-32); //注意5和9要用实型表示,否则5/9值为0cout<<"摄氏温度为:"<<c<<endl;return 0;};3.4题#include <iostream>using namespace std;int main ( ){char c1,c2;cout<<"请输入两个字符c1,c2:";c1=getchar(); //将输入的第一个字符赋给c1c2=getchar(); //将输入的第二个字符赋给c2cout<<"用putchar函数输出结果为:"; putchar(c1);putchar(c2);cout<<endl;cout<<"用cout语句输出结果为:";cout<<c1<<c2<<endl;return 0;}3.4题另一解#include <iostream>using namespace std;int main ( ){char c1,c2;cout<<"请输入两个字符c1,c2:";c1=getchar(); //将输入的第一个字符赋给c1c2=getchar(); //将输入的第二个字符赋给c2cout<<"用putchar函数输出结果为:"; putchar(c1);putchar(44);putchar(c2);cout<<endl;cout<<"用cout语句输出结果为:";cout<<c1<<","<<c2<<endl;return 0;}3.5题#include <iostream>using namespace std;int main ( ){char c1,c2;int i1,i2; //定义为整型cout<<"请输入两个整数i1,i2:";cin>>i1>>i2;c1=i1;c2=i2;cout<<"按字符输出结果为:"<<c1<<" , "<<c2<<endl;return 0;}3.8题#include <iostream>using namespace std;int main ( ){ int a=3,b=4,c=5,x,y;cout<<(a+b>c && b==c)<<endl;cout<<(a||b+c && b-c)<<endl;cout<<(!(a>b) && !c||1)<<endl;cout<<(!(x=a) && (y=b) && 0)<<endl;cout<<(!(a+b)+c-1 && b+c/2)<<endl; return 0;}3.9题include <iostream>using namespace std;int main ( ){int a,b,c;cout<<"please enter three integer numbers:";cin>>a>>b>>c;if(a<b)if(b<c)cout<<"max="<<c;elsecout<<"max="<<b;else if (a<c)cout<<"max="<<c;elsecout<<"max="<<a;cout<<endl;return 0;}3.9题另一解#include <iostream>using namespace std;int main ( ){int a,b,c,temp,max ;cout<<"please enter three integer numbers:";cin>>a>>b>>c;temp=(a>b)?a:b; /* 将a和b中的大者存入temp中*/max=(temp>c)?temp:c; /* 将a和b中的大者与c比较,最大者存入max*/cout<<"max="<<max<<endl;return 0;}3.10题#include <iostream>using namespace std;int main ( ){int x,y;cout<<"enter x:";cin>>x;if (x<1){y=x;cout<<"x="<<x<<", y=x="<<y;}else if (x<10) // 1≤x<10{y=2*x-1;cout<<"x="<<x<<", y=2*x-1="<<y;}else// x≥10{y=3*x-11;cout<<"x="<<x<<",y=3*x-11="<<y;}cout<<endl;return 0;}3.11题#include <iostream>using namespace std; int main (){float score;char grade;cout<<"please enter score of student:"; cin>>score;while (score>100||score<0){cout<<"data error,enter data again.";cin>>score;}switch(int(score/10)){case 10:case 9: grade='A';break;case 8: grade='B';break;case 7: grade='C';break;case 6: grade='D';break;default:grade='E';}cout<<"score is "<<score<<", grade is "<<grade<<endl;return 0;}3.12题#include <iostream>using namespace std;int main (){long int num;intindiv,ten,hundred,thousand,ten_thousand,pla ce;/*分别代表个位,十位,百位,千位,万位和位数*/cout<<"enter an integer(0~99999):"; cin>>num;if (num>9999)place=5;else if (num>999)place=4;else if (num>99)place=3;else if (num>9)place=2;else place=1;cout<<"place="<<place<<endl;//计算各位数字ten_thousand=num/10000;thousand=(int)(num-ten_thousand*10000)/1 000;hundred=(int)(num-ten_thousand*10000-tho usand*1000)/100;ten=(int)(num-ten_thousand*10000-thousan d*1000-hundred*100)/10;indiv=(int)(num-ten_thousand*10000-thousa nd*1000-hundred*100-ten*10);cout<<"original order:";switch(place){case5:cout<<ten_thousand<<","<<thousand<<","< <hundred<<","<<ten<<","<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<ten<<hundred<<thousand<<ten _thousand<<endl;break;case4:cout<<thousand<<","<<hundred<<","<<ten <<","<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<ten<<hundred<<thousand<<en dl;break;case3:cout<<hundred<<","<<ten<<","<<indiv<<en dl;cout<<"reverse order:";cout<<indiv<<ten<<hundred<<endl;break;case 2:cout<<ten<<","<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<ten<<endl;break;case 1:cout<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<endl;break;}return 0;}3.13题#include <iostream>using namespace std;int main (){ long i; //i为利润floatbonus,bon1,bon2,bon4,bon6,bon10;bon1=100000*0.1; //利润为10万元时的奖金bon2=bon1+100000*0.075; //利润为20万元时的奖金bon4=bon2+100000*0.05; //利润为40万元时的奖金bon6=bon4+100000*0.03; //利润为60万元时的奖金bon10=bon6+400000*0.015; //利润为100万元时的奖金cout<<"enter i:";cin>>i;if (i<=100000)bonus=i*0.1;//利润在10万元以内按10%提成奖金else if (i<=200000)bonus=bon1+(i-100000)*0.075; //利润在10万元至20万时的奖金else if (i<=400000)bonus=bon2+(i-200000)*0.05; //利润在20万元至40万时的奖金else if (i<=600000)bonus=bon4+(i-400000)*0.03; //利润在40万元至60万时的奖金else if (i<=1000000)bonus=bon6+(i-600000)*0.015; //利润在60万元至100万时的奖金elsebonus=bon10+(i-1000000)*0.01; //利润在100万元以上时的奖金cout<<"bonus="<<bonus<<endl;return 0;}3.13题另一解#include <iostream>using namespace std;int main (){long i;float bonus,bon1,bon2,bon4,bon6,bon10; int c;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;cout<<"enter i:";cin>>i;c=i/100000;if (c>10) c=10;switch(c){case 0: bonus=i*0.1; break;case 1: bonus=bon1+(i-100000)*0.075; break;case 2:case3:bonus=bon2+(i-200000)*0.05;break;case 4:case5:bonus=bon4+(i-400000)*0.03;break;case 6:case 7:case 8:case 9: bonus=bon6+(i-600000)*0.015; break;case 10: bonus=bon10+(i-1000000)*0.01;}cout<<"bonus="<<bonus<<endl;return 0;}3.14题#include <iostream>using namespace std;int main (){int t,a,b,c,d;cout<<"enter four numbers:";cin>>a>>b>>c>>d;cout<<"a="<<a<<", b="<<b<<", c="<<c<<",d="<<d<<endl;if (a>b){t=a;a=b;b=t;}if (a>c){t=a; a=c; c=t;}if (a>d){t=a; a=d; d=t;}if (b>c){t=b; b=c; c=t;}if (b>d){t=b; b=d; d=t;}if (c>d){t=c; c=d; d=t;}cout<<"the sorted sequence:"<<endl;cout<<a<<", "<<b<<", "<<c<<", "<<d<<endl; return 0;}3.15题#include <iostream>using namespace std;int main (){int p,r,n,m,temp;cout<<"please enter two positive integer numbers n,m:";cin>>n>>m;if (n<m){temp=n;n=m;m=temp; //把大数放在n中, 小数放在m中}p=n*m; //先将n和m的乘积保存在p中, 以便求最小公倍数时用while (m!=0) //求n和m的最大公约数{r=n%m;n=m;m=r;}cout<<"HCF="<<n<<endl;cout<<"LCD="<<p/n<<endl; // p是原来两个整数的乘积return 0;}3.16题#include <iostream>using namespace std;int main (){char c;int letters=0,space=0,digit=0,other=0;cout<<"enter one line::"<<endl;while((c=getchar())!='\n'){if (c>='a' && c<='z'||c>='A' && c<='Z')letters++;else if (c==' ')space++;else if (c>='0' && c<='9')digit++;elseother++;}cout<<"letter:"<<letters<<", space:"<<space<<", digit:"<<digit<<", other:"<<other<<endl;return 0;}3.17题#include <iostream>using namespace std;int main (){int a,n,i=1,sn=0,tn=0;cout<<"a,n=:";cin>>a>>n;while (i<=n){tn=tn+a; //赋值后的tn为i个a 组成数的值sn=sn+tn; //赋值后的sn为多项式前i项之和a=a*10;++i;}cout<<"a+aa+aaa+...="<<sn<<endl;return 0;}3.18题#include <iostream>using namespace std;int main (){float s=0,t=1;int n;for (n=1;n<=20;n++){t=t*n; // 求n!s=s+t; // 将各项累加}cout<<"1!+2!+...+20!="<<s<<endl;return 0;}3.19题#include <iostream>using namespace std;int main (){int i,j,k,n;cout<<"narcissus numbers are:"<<endl;for (n=100;n<1000;n++){i=n/100;j=n/10-i*10;k=n%10;if (n == i*i*i + j*j*j + k*k*k)cout<<n<<" ";}cout<<endl;return 0;}3.20题#include <iostream>using namespace std;int main(){const int m=1000; // 定义寻找范围int k1,k2,k3,k4,k5,k6,k7,k8,k9,k10;int i,a,n,s;for (a=2;a<=m;a++) // a是2~1000之间的整数,检查它是否为完数{n=0; // n用来累计a的因子的个数s=a; // s用来存放尚未求出的因子之和,开始时等于afor (i=1;i<a;i++) // 检查i是否为a 的因子if (a%i==0) // 如果i是a的因子{n++; // n加1,表示新找到一个因子s=s-i; // s减去已找到的因子,s的新值是尚未求出的因子之和switch(n) // 将找到的因子赋给k1,...,k10{case 1:k1=i; break; // 找出的笫1个因子赋给k1case 2:k2=i; break; // 找出的笫2个因子赋给k2case 3:k3=i; break; // 找出的笫3个因子赋给k3case 4:k4=i; break; // 找出的笫4个因子赋给k4case 5:k5=i; break; // 找出的笫5个因子赋给k5case 6:k6=i; break; // 找出的笫6个因子赋给k6case 7:k7=i; break; // 找出的笫7个因子赋给k7case 8:k8=i; break; // 找出的笫8个因子赋给k8case 9:k9=i; break; // 找出的笫9个因子赋给k9case 10:k10=i; break; // 找出的笫10个因子赋给k10}}if (s==0) // s=0表示全部因子都已找到了{cout<<a<<" is a 完数"<<endl;cout<<"its factors are:";if (n>1) cout<<k1<<","<<k2; // n>1表示a至少有2个因子if (n>2) cout<<","<<k3; // n>2表示至少有3个因子,故应再输出一个因子if (n>3) cout<<","<<k4; // n>3表示至少有4个因子,故应再输出一个因子if (n>4) cout<<","<<k5; // 以下类似if (n>5) cout<<","<<k6;if (n>6) cout<<","<<k7;if (n>7) cout<<","<<k8;if (n>8) cout<<","<<k9;if (n>9) cout<<","<<k10;cout<<endl<<endl;}}return 0;}3.20题另一解#include <iostream>using namespace std;int main(){int m,s,i;for (m=2;m<1000;m++){s=0;for (i=1;i<m;i++)if ((m%i)==0) s=s+i;if(s==m){cout<<m<<" is a完数"<<endl;cout<<"its factors are:";for (i=1;i<m;i++)if (m%i==0) cout<<i<<" ";cout<<endl;}}return 0;}3.20题另一解#include <iostream>using namespace std;int main(){int k[11];int i,a,n,s;for (a=2;a<=1000;a++){n=0;s=a;for (i=1;i<a;i++)if ((a%i)==0){n++;s=s-i;k[n]=i; // 将找到的因子赋给k[1]┅k[10]}if (s==0){cout<<a<<" is a 完数"<<endl;cout<<"its factors are:";for (i=1;i<n;i++)cout<<k[i]<<" ";cout<<k[n]<<endl;}}return 0;}3.21题#include <iostream>using namespace std;int main(){int i,t,n=20;double a=2,b=1,s=0;for (i=1;i<=n;i++){s=s+a/b;t=a;a=a+b; // 将前一项分子与分母之和作为下一项的分子b=t; // 将前一项的分子作为下一项的分母}cout<<"sum="<<s<<endl;return 0;} 3.22题#include <iostream>using namespace std;int main(){int day,x1,x2;day=9;x2=1;while(day>0){x1=(x2+1)*2; // 第1天的桃子数是第2天桃子数加1后的2倍x2=x1;day--;}cout<<"total="<<x1<<endl;return 0;}3.23题#include <iostream>#include <cmath>using namespace std;int main(){float a,x0,x1;cout<<"enter a positive number:"; cin>>a; // 输入a的值x0=a/2;x1=(x0+a/x0)/2;do{x0=x1;x1=(x0+a/x0)/2;}while(fabs(x0-x1)>=1e-5);cout<<"The square root of "<<a<<" is "<<x1<<endl;return 0;}3.24题#include <iostream>using namespace std;int main(){int i,k;for (i=0;i<=3;i++) // 输出上面4行*号{for (k=0;k<=2*i;k++)cout<<"*"; // 输出*号cout<<endl; //输出完一行*号后换行}for (i=0;i<=2;i++) // 输出下面3行*号{for (k=0;k<=4-2*i;k++)cout<<"*"; // 输出*号cout<<endl; // 输出完一行*号后换行}return 0;}3.25题#include <iostream>using namespace std;int main(){char i,j,k; /* i是a的对手;j是b的对手;k是c的对手*/for (i='X';i<='Z';i++)for (j='X';j<='Z';j++)if (i!=j)for (k='X';k<='Z';k++)if (i!=k && j!=k)if (i!='X' && k!='X' && k!='Z')cout<<"A--"<<i<<"B--"<<j<<" C--"<<k<<endl;return 0;}第四章4.1题#include <iostream>using namespace std;int main(){int hcf(int,int);int lcd(int,int,int);int u,v,h,l;cin>>u>>v;h=hcf(u,v);cout<<"H.C.F="<<h<<endl;l=lcd(u,v,h);cout<<"L.C.D="<<l<<endl;return 0;}int hcf(int u,int v){int t,r;if (v>u){t=u;u=v;v=t;}while ((r=u%v)!=0){u=v;v=r;}return(v);}int lcd(int u,int v,int h){return(u*v/h);}4.2题#include <iostream>#include <math.h>using namespace std;float x1,x2,disc,p,q;int main(){void greater_than_zero(float,float); void equal_to_zero(float,float);void smaller_than_zero(float,float); float a,b,c;cout<<"input a,b,c:";cin>>a>>b>>c;disc=b*b-4*a*c;cout<<"root:"<<endl;if (disc>0){greater_than_zero(a,b);cout<<"x1="<<x1<<",x2="<<x2<<endl; }else if (disc==0){equal_to_zero(a,b);cout<<"x1="<<x1<<",x2="<<x2<<endl;}else{smaller_than_zero(a,b);cout<<"x1="<<p<<"+"<<q<<"i"<<endl;cout<<"x2="<<p<<"-"<<q<<"i"<<endl;}return 0;}void greater_than_zero(float a,float b) /* 定义一个函数,用来求disc>0时方程的根*/{x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);}void equal_to_zero(float a,float b) /* 定义一个函数,用来求disc=0时方程的根*/{x1=x2=(-b)/(2*a);}void smaller_than_zero(float a,float b) /* 定义一个函数,用来求disc<0时方程的根*/{p=-b/(2*a);q=sqrt(-disc)/(2*a);}4.3题#include <iostream>using namespace std;int main(){int prime(int); /* 函数原型声明*/int n;cout<<"input an integer:";cin>>n;if (prime(n))cout<<n<<" is a prime."<<endl;elsecout<<n<<" is not a prime."<<endl;return 0;}int prime(int n){int flag=1,i;for (i=2;i<n/2 && flag==1;i++)if (n%i==0)flag=0;return(flag);}4.4题#include <iostream>using namespace std;int main(){int fac(int);int a,b,c,sum=0;cout<<"enter a,b,c:";cin>>a>>b>>c;sum=sum+fac(a)+fac(b)+fac(c);cout<<a<<"!+"<<b<<"!+"<<c<<"!="<<sum<<e ndl;return 0;}int fac(int n){int f=1;for (int i=1;i<=n;i++)f=f*i;return f;}4.5题#include <iostream>#include <cmath>using namespace std;int main(){double e(double);double x,sinh;cout<<"enter x:";cin>>x;sinh=(e(x)+e(-x))/2;cout<<"sinh("<<x<<")="<<sinh<<endl;return 0;}double e(double x){return exp(x);}4.6题#include <iostream>#include <cmath>using namespace std;int main(){doublesolut(double ,double ,double ,double ); double a,b,c,d;cout<<"input a,b,c,d:";cin>>a>>b>>c>>d;cout<<"x="<<solut(a,b,c,d)<<endl;return 0;}double solut(double a,double b,double c,double d){double x=1,x0,f,f1;do{x0=x;f=((a*x0+b)*x0+c)*x0+d;f1=(3*a*x0+2*b)*x0+c;x=x0-f/f1;}while(fabs(x-x0)>=1e-5);return(x);}4.7题#include <iostream>#include <cmath>using namespace std;int main(){void godbaha(int);int n;cout<<"input n:";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<<n<<"="<<a<<"+"<<b<<endl;}}}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 <iostream>using namespace std;int main(){int x,n;float p(int,int);cout<<"input n & x:";cin>>n>>x;cout<<"n="<<n<<",x="<<x<<endl;;cout<<"P"<<n<<"(x)="<<p(n,x)<<endl; return 0;}float p(int n,int x){if (n==0)return(1);else if (n==1)return(x);elsereturn(((2*n-1)*x*p((n-1),x)-(n-1)*p((n-2),x))/n);}4.9题#include <iostream>using namespace std;int main(){void hanoi(int n,char one,char two,char three);int m;cout<<"input the number of diskes:"; cin>>m;cout<<"The steps of moving "<<m<<" disks:"<<endl;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<<x<<"-->"<<y<<endl;}4.10题#include <iostream>using namespace std;int main(){void convert(int n);int number;cout<<"input an integer:";cin>>number;cout<<"output:"<<endl;if (number<0){cout<<"-";number=-number;}convert(number);cout<<endl;return 0;}void convert(int n){int i;char c;if ((i=n/10)!=0)convert(i);c=n%10+'0';cout<<" "<<c;}4.11题#include <iostream>using namespace std;int main(){int f(int);int n,s;cout<<"input the number n:";cin>>n;s=f(n);cout<<"The result is "<<s<<endl;return 0;}int f(int n){;if (n==1)return 1;elsereturn (n*n+f(n-1));}4.12题#include <iostream>#include <cmath>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<<"input a,b,c:";cin>>a>>b>>c;if (a+b>c && a+c>b && b+c>a)cout<<"area="<<AREA(a,b,c)<<endl; elsecout<<"It is not a triangle!"<<endl; return 0;}4.14题#include <iostream>using namespace std;//#define LETTER 1int main(){char c;cin>>c;#if LETTERif(c>='a' && c<='z')c=c-32;#elseif(c>='A' && c<='Z')c=c+32;#endifcout<<c<<endl;return 0;}4.15题#include <iostream>using namespace std;#define CHANGE 1int main(){char ch[40];cout<<"input text:"<<endl;;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;}}#endifcout<<"output:"<<endl<<ch<<endl;return 0;}4.16题file#include <iostream>using namespace std;int a;int main(){extern int power(int);int b=3,c,d,m;cout<<"enter an integer a and its power m:"<<endl;cin>>a>>m;c=a*b;cout<<a<<"*"<<b<<"="<<c<<endl;d=power(m);cout<<a<<"**"<<m<<"="<<d<<endl; return 0;}4.16题fileextern int a;int power(int n){int i,y=1;for(i=1;i<=n;i++)y*=a;return y;}第五章5.1题#include <iostream>#include <iomanip>using namespace std;#include <math.h>int main(){int i,j,n,a[101];for (i=1;i<=100;i++)a[i]=i;a[1]=0;for (i=2;i<sqrt(100);i++)for (j=i+1;j<=100;j++){if(a[i]!=0 && a[j]!=0)if (a[j]%a[i]==0)a[j]=0; }cout<<endl;for (i=1,n=0;i<=100;i++){if (a[i]!=0){cout<<setw(5)<<a[i]<<" ";n++;}if(n==10){cout<<endl;n=0;}}cout<<endl;return 0;}5.2题#include <iostream>using namespace std;//#include <math.h>int main(){int i,j,min,temp,a[11];cout<<"enter data:"<<endl;for (i=1;i<=10;i++){cout<<"a["<<i<<"]=";cin>>a[i]; //输入10个数}cout<<endl<<"The original numbers:"<<endl;;for (i=1;i<=10;i++)cout<<a[i]<<" "; // 输出这10个数cout<<endl;;for (i=1;i<=9;i++) //以下8行是对10个数排序{min=i;for (j=i+1;j<=10;j++)if (a[min]>a[j]) min=j;temp=a[i]; //以下3行将a[i+1]~a[10]中最小者与a[i] 对换a[i]=a[min];a[min]=temp;}cout<<endl<<"The sorted numbers:"<<endl;for (i=1;i<=10;i++) // 输出已排好序的10个数cout<<a[i]<<" ";cout<<endl;return 0;}5.3题#include <iostream>using namespace std;int main(){int a[3][3],sum=0;int i,j;cout<<"enter data:"<<endl;;for (i=0;i<3;i++)for (j=0;j<3;j++)cin>>a[i][j];for (i=0;i<3;i++)sum=sum+a[i][i];cout<<"sum="<<sum<<endl;return 0;}5.4题#include <iostream>using namespace std;int main(){int a[11]={1,4,6,9,13,16,19,28,40,100};int num,i,j;cout<<"array a:"<<endl;for (i=0;i<10;i++)cout<<a[i]<<" ";cout<<endl;;cout<<"insert data:";cin>>num;if (num>a[9])a[10]=num;else。

《C语言程序设计》课后习题答案解析[第四版]谭浩强

《C语言程序设计》课后习题答案解析[第四版]谭浩强

第1章程序设计和C语言11.1什么是计算机程序11.2什么是计算机语言11.3C语言的发展及其特点31.4最简单的C语言程序51.4.1最简单的C语言程序举例61.4.2C语言程序的结构101.5运行C程序的步骤与方法121.6程序设计的任务141-5 #include <stdio.h>int main ( ){ printf ("**************************\n\n"); printf(" Very Good!\n\n");printf ("**************************\n"); return 0;}1-6#include <stdio.h>int main(){int a,b,c,max;printf("please input a,b,c:\n");scanf("%d,%d,%d",&a,&b,&c);max=a;if (max<b)max=b;if (max<c)max=c;printf("The largest number is %d\n",max);return 0;}第2章算法——程序的灵魂162.1什么是算法162.2简单的算法举例172.3算法的特性212.4怎样表示一个算法222.4.1用自然语言表示算法222.4.2用流程图表示算法222.4.3三种基本结构和改进的流程图262.4.4用N S流程图表示算法282.4.5用伪代码表示算法312.4.6用计算机语言表示算法322.5结构化程序设计方法34习题36第章最简单的C程序设计——顺序程序设计37 3.1顺序程序设计举例373.2数据的表现形式及其运算393.2.1常量和变量393.2.2数据类型423.2.3整型数据443.2.4字符型数据473.2.5浮点型数据493.2.6怎样确定常量的类型513.2.7运算符和表达式523.3C语句573.3.1C语句的作用和分类573.3.2最基本的语句——赋值语句593.4数据的输入输出653.4.1输入输出举例653.4.2有关数据输入输出的概念673.4.3用printf函数输出数据683.4.4用scanf函数输入数据753.4.5字符数据的输入输出78习题823-1 #include <stdio.h>#include <math.h>int main(){float p,r,n;r=0.1;n=10;p=pow(1+r,n);printf("p=%f\n",p);return 0;}3-2-1#include <stdio.h>#include <math.h>int main(){float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;p=1000;r5=0.0585;r3=0.054;r2=0.0468;r1=0.0414;r0=0.0072;p1=p*((1+r5)*5); // 一次存5年期p2=p*(1+2*r2)*(1+3*r3); // 先存2年期,到期后将本息再存3年期p3=p*(1+3*r3)*(1+2*r2); // 先存3年期,到期后将本息再存2年期p4=p*pow(1+r1,5); // 存1年期,到期后将本息存再存1年期,连续存5次 p5=p*pow(1+r0/4,4*5); // 存活期存款。

谭浩强C语言程序设计习题答案

谭浩强C语言程序设计习题答案

谭浩强C语言程序设计习题参考答案第一章1.6main(){int a,b,c,max;printf("input three numbers:\n");scanf("%d,%d,%d",&a,&b,&c);max=a;if(max<b)max=b;if(max<c)max=c;printf("max=%d",max);}第二章2.3(1)(10)10=(12)8=(a)16(2)(32)10=(40)8=(20)16(3)(75)10=(113)8=(4b)16(4)(-617)10=(176627)8=(fd97)16(5)(-111)10=(177621)8=(ff91)16(6)(2483)10=(4663)8=(963)16(7)(-28654)10=(110022)8=(9012)16(8)(21003)10=(51013)8=(520b)162.6aabb (8)cc (8)abc(7)AN2.7main(){char c1='C',c2='h',c3='i',c4='n',c5='a';c1+=4, c2+=4, c3+=4, c4+=4, c5+=4;printf("%c%c%c%c%c\n",c1,c2,c3,c4,c5);}2.8main(){int c1,c2;c1=97;c2=98;printf("%c %c",c1,c2);}2.9(1)=2.5(2)=3.52.109,11,9,102.12(1)24 (2)10 (3)60 (4)0 (5)0 (6)0第三章3.4main(){int a,b,c;long int u,n;float x,y,z;char c1,c2;a=3;b=4;c=5;x=1.2;y=2.4;z=-3.6;u=51274;n=128765;c1='a';c2='b';printf("\n");printf("a=%2d b=%2d c=%2d\n",a,b,c);printf("x=%8.6f,y=%8.6f,z=%9.6f\n",x,y,z);printf("x+y=%5.2f y+z=%5.2f z+x=%5.2f\n",x+y,y+z,z+x);printf("u=%6ld n=%9ld\n",u,n);printf("c1='%c'or %d(ASCII)\n",c1,c1);printf("c2='%c'or %d(ASCII)\n",c2,c2);}3.5575 767.856400,-789.12396267.856400,-789.12396267.86 -789.12,67.856400,-789.123962,67.856400,-789.1239626.785640e+001,-7.89e+002A,65,101,411234567,4553207,d68765535,177777,ffff,-1COMPUTER, COM3.6a=3 b=7/x=8.5 y=71.82/c1=A c2=a/3.710 20Aa1.5 -3.75 +1.4,67.8/(空3)10(空3)20Aa1.5(空1)-3.75(空1)(随意输入一个数),67.8回车3.8main(){float pi,h,r,l,s,sq,sv,sz;pi=3.1415926;printf("input r,h\n");scanf("%f,%f",&r,&h);l=2*pi*r;s=r*r*pi;sq=4*pi*r*r;sv=4.0/3.0*pi*r*r*r;sz=pi*r*r*h;printf("l=%6.2f\n",l);printf("s=%6.2f\n",s);printf("sq=%6.2f\n",sq);printf("vq=%6.2f\n",sv);printf("vz=%6.2f\n",sz);}3.9main(){float c,f;scanf("%f",&f);c=(5.0/9.0)*(f-32);printf("c=%5.2f\n",c);}3.10#include"stdio.h"main(){char c1,c2;scanf("%c,%c",&c1,&c2);putchar(c1);putchar(c2);printf("\n");printf("%c%c\n",c1,c2);}第四章4.3(1)0 (2)1 (3)1 (4)0 (5)1 4.4main(){int a,b,c;scanf("%d,%d,%d",&a,&b,&c); if(a<b)if(b<c)printf("max=%d\n",c);elseprintf("max=%d\n",b);else if(a<c)printf("max=%d\n",c);elseprintf("max=%d\n",a);}main(){int a,b,c,temp,max;scanf("%d,%d,%d",&a,&b,&c);temp=(a>b)?a:b;max=(c>temp)?c:temp;printf("max=%d",max);}4.5main(){int x,y;scanf("%d",&x);if(x<1)y=x;else if(x<10)y=2*x-1;else y=3*x-11;printf("y=%d",y);}4.6main(){int score,temp,logic;char grade;logic=1;while(logic){scanf("%d",&score);if(score>=0&&score<=100)logic=0; }if(score==100)temp=9;elsetemp=(score-score%10)/10;switch(temp){case 9:grade='A';break;case 8:grade='B';break;case 7:grade='C';break;case 6:grade='D';break;case 5:case 4:case 3:case 2:case 1:case 0:grade='E';}printf("score=%d,grade=%c",score,grade);}4.7main(){long int num;int indiv,ten,hundred,thousand,ten_thousand,place;scanf("%ld",&num);if(num>9999) place=5;else if(num>999) place=4;else if(num>99) place=3;else if(num>9) place=2;else place=1;printf("place=%d\n",place);ten_thousand=num/10000;thousand=(num-ten_thousand*10000)/1000;hundred=(num-ten_thousand*10000-thousand*1000)/100;ten=(num-ten_thousand*10000-thousand*1000-hundred*100)/10;indiv=num-ten_thousand*10000-thousand*1000-hundred*100-ten*10;switch(place){case 5:printf("%d,%d,%d,%d,%d\n",ten_thousand,thousand,hundred,ten,indiv);printf("%d,%d,%d,%d,%d\n",indiv,ten,hundred,thousand,ten_thousand);break;case 4:printf("%d,%d,%d,%d\n",thousand,hundred,ten,indiv);printf("%d,%d,%d,%d\n",indiv,ten,hundred,thousand);break;case 3:printf("%d,%d,%d\n",hundred,ten,indiv);printf("%d,%d,%d\n",indiv,ten,hundred);break;case 2:printf("%d,%d\n",ten,indiv);printf("%d,%d\n",indiv,ten);break;case 1:printf("%d\n",indiv);printf("%d\n",indiv);}}4.8main(){long i;float bonus,bon1,bon2,bon4,bon6,bon10;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;scanf("%ld",&i);if(i<=1e5)bonus=i*0.1;else if(i<=2e5)bonus=bon1+(i-100000)*0.075; else if(i<=4e5)bonus=bon2+(i-200000)*0.05; else if(i<=6e5)bonus=bon4+(i-400000)*0.03; else if(i<=1e6)bonus=bon6+(i-600000)*0.015; else bonus=bon10+(i-1000000)*0.01;printf("bonus=%10.2f",bonus);}main(){long i;float bonus,bon1,bon2,bon4,bon6,bon10;int branch;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;scanf("%ld",&i);branch=i/100000;if(branch>10)branch=10;switch(branch){case 0:bonus=i*0.1;break;case 1:bonus=bon1+(i-100000)*0.075;break; case 2:case 3:bonus=bon2+(i-200000)*0.05;break; case 4:case 5:bonus=bon4+(i-400000)*0.03;break; case 6:case 7case 8:case 9:bonus=bon6+(i-600000)*0.015;break; case 10:bonus=bon10+(i-1000000)*0.01;}printf("bonus=%10.2f",bonus);}4.9main(){int t,a,b,c,d;scanf("%d,%d,%d,%d",&a,&b,&c,&d);if(a>b){t=a;a=b;b=t;}if(a>c){t=a;a=c;c=t;}if(a>d){t=a;a=d;d=t;}if(b>c){t=b;b=c;c=t;}if(b>d){t=b;b=d;d=t;}if(c>d){t=c;c=d;d=t;}printf("%d %d %d %d\n",a,b,c,d);}4.10main(){int h=10;float x,y,x0=2,y0=2,d1,d2,d3,d4;scanf("%f,%f",&x,&y);d1=(x-x0)*(x-x0)+(y-y0)*(y-y0);d2=(x-x0)*(x-x0)+(y+y0)*(y+y0);d3=(x+x0)*(x+x0)+(y-y0)*(y-y0);d4=(x+x0)*(x+x0)+(y+y0)*(y+y0);if(d1>1&&d2>1&&d3>1&&d4>1)h=0;printf("h=%d",h);}第五章循环控制5.1main(){int a,b,num1,num2,temp;scanf("%d,%d",&num1,&num2);if(num1<num2){temp=num1;num1=num2;num2=temp;}a=num1;b=num2;while(b!=0){temp=a%b;a=b;b=temp;}printf("%d\n",a);printf("%d\n",num1*num2/a);}5.2#include"stdio.h"main(){char c;int letters=0,space=0,digit=0,other=0;while((c=getchar())!='\n'){if(c>='a'&&c<='z'||c>='A'&&c<='Z') letters++;else if(c==' ')space++;else if(c>='0'&&c<='9')digit++;else other++;}printf("letters=%d\nspace=%d\ndigit=%d\nother=%d\n",letters,space,digit,other); }main(){int a,n,count=1,sn=0,tn=0;scanf("%d,%d",&a,&n);while(count<=n){tn+=a;sn+=tn;a*=10;++count;}printf("a+aa+aaa+...=%d\n",sn);}5.4main(){float n,s=0,t=1;for(n=1;n<=20;n++){t*=n;s+=t;}printf("s=%e\n",s);}5.5main(){int N1=100,N2=50,N3=10;float k;float s1=0,s2=0,s3=0;for(k=1;k<=N1;k++)s1+=k;for(k=1;k<=N2;k++)s2+=k*k;for(k=1;k<=N3;k++)s3+=1/k;printf("s=%8.2f\n",s1+s2+s3);}5.6main(){int i,j,k,n;for(n=100;n<1000;n++){i=n/100;j=n/10-i*10;k=n%10;if(i*100+j*10+k==i*i*i+j*j*j+k*k*k) printf("n=%d\n",n);}}5.7#define M 1000{int k0,k1,k2,k3,k4,k5,k6,k7,k8,k9; int i,j,n,s;for(j=2;j<=M;j++){n=0;s=j;for(i=1;i<j;i++){if((j%i)==0){n++;s=s-i;switch(n){case 1:k0=i;break;case 2:k1=i;break;case 3:k2=i;break;case 4:k3=i;break;case 5:k4=i;break;case 6:k5=i;break;case 7:k6=i;break;case 8:k7=i;break;case 9:k8=i;break;case 10:k9=i;break;}}}if(s==0){printf("j=%d\n",j);if(n>1)printf("%d,%d",k0,k1);if(n>2)printf(",%d",k2);if(n>3)printf(",%d",k3);if(n>4)printf(",%d",k4);if(n>5)printf(",%d",k5);if(n>6)printf(",%d",k6);if(n>7)printf(",%d",k7);if(n>8)printf(",%d",k8);if(n>9)printf(",%d\n",k9);}}}main(){static int k[10];int i,j,n,s;for(j=2;j<=1000;j++){n=-1;s=j;for(i=1;i<j;i++){if((j%i)==0){n++;s=s-i;k[n]=i;}}if(s==0){printf("j=%d\n",j);for(i=0;i<n;i++)printf("%d,",k[i]);printf("%d\n",k[n]);}}}5.8main(){int n,t,number=20;float a=2;b=1;s=0;for(n=1;n<=number;n++) {s=s+a/b;t=a,a=a+b,b=t;}printf("s=%9.6f\n",s);}5.9main(){float sn=100.0,hn=sn/2; int n;for(n=2;n<=10;n++) {sn=sn+2*hn;hn=hn/2;}printf("sn=%f\n",sn);printf("hn=%f\n",hn);}5.10main(){int day,x1,x2;day=9;x2=1;while(day>0){x1=(x2+1)*2;x2=x1;day--;}printf("x1=%d\n",x1);}5.11#include"math.h"main(){float a,xn0,xn1;scanf("%f",&a);xn0=a/2;xn1=(xn0+a/xn0)/2;do{xn0=xn1;xn1=(xn0+a/xn0)/2;}while(fabs(xn0-xn1)>=1e-5);printf("a=%5.2f\n,xn1=%8.2f\n",a,xn1); }5.12#include"math.h"main(){float x,x0,f,f1;x=1.5;do{x0=x;f=((2*x0-4)*x0+3)*x0-6;f1=(6*x0-8)*x0+3;x=x0-f/f1;}while(fabs(x-x0)>=1e-5);printf("x=%6.2f\n",x);}5.13#include"math.h"main(){float x0,x1,x2,fx0,fx1,fx2;do{scanf("%f,%f",&x1,&x2);fx1=x1*((2*x1-4)*x1+3)-6;fx2=x2*((2*x2-4)*x2+3)-6;}while(fx1*fx2>0);do{x0=(x1+x2)/2;fx0=x0*((2*x0-4)*x0+3)-6;if((fx0*fx1)<0){x2=x0;fx2=fx0;}else{x1=x0;fx1=fx0;}}while(fabs(fx0)>=1e-5);printf("x0=%6.2f\n",x0);}5.14main(){int i,j,k;for(i=0;i<=3;i++){for(j=0;j<=2-i;j++)printf(" ");for(k=0;k<=2*i;k++)printf("*");printf("\n");}for(i=0;i<=2;i++){for(j=0;j<=i;j++)printf(" ");for(k=0;k<=4-2*i;k++)printf("*");printf("\n");}}5.15main(){char i,j,k;for(i='x';i<='z';i++)for(j='x';j<='z';j++){if(i!=j)for(k='x';k<='z';k++){if(i!=k&&j!=k){if(i!='x'&&k!='x'&&k!='z')printf("\na--%c\tb--%c\tc--%c\n",i,j,k);}}}}第六章数组6.1#include <math.h>#define N 101main(){ int i,j,line,a[N];for (i=2;i<N;i++) a[i]=i; for (i=2;i<sqrt(N);i++)for (j=i+1;j<N;j++){if(a[i]!=0 && a[j]!=0)if (a[j]%a[i]==0)a[j]=0; }printf("\n");for (i=2,line=0;i<N;i++) { if(a[i]!=0){ printf("%5d",a[i]);line++; }if(line==10){ printf("\n");line=0; }}}6.2#define N 10main(){int i,j,min,temp,a[N];for(i=0;i<N;i++)scanf("%d",&a[i]);for(i=0;i<N-1;i++){min=i;for(j=i+1;j<N;j++)if(a[min]>a[j])min=j;temp=a[i];a[i]=a[min];a[min]=temp;}for(i=0;i<N;i++)printf("%5d",a[i]);}6.3main(){float a[3][3],sum;int i,j;for(i=0;i<3;i++)for(j=0;j<3;j++){scanf("%f",&sum);a[i][j]=sum;}for(i=0;i<3;i++)sum=sum+a[i][i];printf("sum=%f",sum);}6.4main(){int a[11]={1,4,6,9,13,16,19,28,40,100}; int temp1,temp2,number, end,i,j;scanf("%d",&number);end=a[9];if(number>end) a[10]=number;else{for(i=0;i<10;i++){if(a[i]>number){temp1=a[i];a[i]=number;for(j=i+1;j<11;j++){temp2=a[j];a[j]=temp1;temp1=temp2;}break;}}}for(i=0;i<11;i++)printf("%6d",a[i]);}6.5#define N 5main(){int a[N]={8,6,5,4,1},i,temp;for(i=0;i<N/2;i++){temp=a[i];a[i]=a[N-i-1];a[N-i-1]=temp;}for(i=0;i<N;i++)printf("%4d",a[i]);}6.6#define N 11main(){int i,j,a[N][N];for(i=1;i<N;i++){a[i][i]=1;a[i][1]=1;}for(i=3;i<N;i++)for(j=2;j<i;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];for(i=1;i<N;i++){for(j=1;j<=i;j++)printf("%6d",a[i][j]);printf("\n");}}6.7main(){int a[16][16],i,j,k,p,m,n;p=1;while(p==1){scanf("%d",&n);if((n!=0)&&(n<=15)&&(n%2!=0))p=0; }for(i=1;i<=n;i++)for(j=1;j<=n;j++)a[i][j]=0;j=n/2+1;a[1][j]=1;for(k=2;k<=n*n;k++){i=i-1;j=j+1;if((i<1)&&(j>n)){i=i+2;j=j-1;}else{if(i<1)i=n;if(j>n)j=1;}if(a[i][j]==0)a[i][j]=k;else{i=i+2;j=j-1;a[i][j]=k;}}for(i=1;i<=n;i++){for(j=1;j<=n;j++)printf("%3d",a[i][j]);printf("\n");}}6.8#define N 10#define M 10main(){int i,j,k,m,n,flag1,flag2,a[N][M],max,maxi,maxj; scanf("%d,%d",&n,&m);for(i=0;i<n;i++)for(j=0;j<m;j++)scanf("%d",&a[i][j]);flag2=0;for(i=0;i<n;i++){max=a[i][0];for(j=0;j<m;j++)if(max<a[i][j]){max=a[i][j];maxj=j;}for(k=0,flag1=1;k<n&&flag1;k++)if(max>a[k][maxj])flag1=0;if(flag1){ printf("\na[%d][%d]=%d\n",i,maxj,max);flag2=1;}}if(!flag2) printf("NOT");}6.9#include<stdio.h>#define N 15main(){int i,j,number,top,bott,min,loca,a[N],flag;char c;for(i=0;i<=N;i++)scanf("%d",&a[i]);flag=1;while(flag){scanf("%d",&number);loca=0;top=0;bott=N-1;if((number<a[0])||(number>a[N-1]))loca=-1;while((loca==0)&&(top<=bott)){min=(bott+top)/2;if(number==a[min]){loca=min;printf("number=%d,loca=%d\n",number,loca+1);}else if(number<a[min])bott=min-1;elsetop=min+1;}if(loca==0||loca==-1)printf("%d not in table\n",number);printf("continue Y/N or y/n\n");c=getchar();if(c=='N'||c=='n')flag=0;}}6.10main(){int i,j,uppn,lown,dign,span,othn;char text[3][80];uppn=lown=dign=span=othn=0;for(i=0;i<3;i++){gets(text[i]);for(j=0;j<80&&text[i][j]!='\0';j++){if(text[i][j]>='A'&&text[i][j]<='Z')uppn++;else if(text[i][j]>='a'&&text[i][j]<='z')lown++;else if(text[i][j]>='0'&&text[i][j]<='9')dign++;else if(text[i][j]==' ')span++;elseothn++;}}for(i=0;i<3;i++)printf("%s\n",text[i]);printf("uppn=%d\n",uppn);printf("lown=%d\n",lown);printf("dign=%d\n",dign);printf("span=%d\n",span);printf("othn=%d\n",othn);}6.11main(){static char a[5]={'*','*','*','*','*'};int i,j,k;char space=' ';for(i=0;i<=5;i++){printf("\n");for(j=1;j<=3*i;j++)printf("%1c",space);for(k=0;k<=5;k++)printf("%3c",a[k]);}}6.12#include<stdio.h>main(){int i,n;char ch[80],tran[80];gets(ch);i=0;while(ch[i]!='\0'){if((ch[i]>='A')&&(ch[i]<='Z'))tran[i]=26+64-ch[i]+1+64;else if((ch[i]>='a')&&(ch[i]<='z'))tran[i]=26+96-ch[i]+1+96;elsetran[i]=ch[i];i++;}n=i;for(i=0;i<n;i++)putchar(tran[i]);}6.13main(){char s1[80],s2[40];int i=0,j=0;scanf("%s",s1);scanf("%s",s2);while(s1[i]!='\0')i++;while(s2[j]!='\0')s1[i++]=s2[j++];s1[i]='\0';printf("s=%s\n",s1);}6.14#include<stdio.h>main(){int i,resu;char s1[100],s2[100];gets(s1);gets(s2);i=0;while((s1[i]==s2[i])&&(s1[i]!='\0'))i++;if(s1[i]=='\0'&&s2[i]=='\0')resu=0;elseresu=s1[i]-s2[i];printf("s1=%s,s2=%s,resu=%d\n",s1,s2,resu); }6.15#include"stdio.h"main(){char from[80],to[80];;int i;scanf("%s",from);for(i=0;i<=strlen(from);i++)to[i]=from[i];printf("%s\n",to);}第七章7.1hcf(u,v)int u,v;{int a,b,t,r;if(u>v){t=u;u=v;v=t;}a=u;b=v;while((r=b%a)!=0){b=a;a=r;}return(a);}lcd(u,v,h)int u,v,h;{return(u*v/h);}main(){int u,v,h,l;scanf("%d,%d",&u,&v);h=hcf(u,v);printf("H.C.F=%d\n",h);l=lcd(u,v,h);printf("L.C.D=%d\n",l);}7.2#include"math.h"float x1,x2,disc,p,q;greater_than_zero(a,b)float a,b;{x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);}equal_to_zero(a,b)flaot a,b;{x1=x2=-b/(2*a);}smaller_than_zero(a,b)float a,b;{p=-b/(2*a);q=sqrt(-disc)/(2*a);}main(){float a,b,c;scanf("%f,%f,%f",&a,&b,&c);disc=b*b-4*a*c;if(fabs(disc)<=1e-5){equal_to_zero(a,b);printf("x1=%5.2f\tx2=%5.2f\n",x1,x2); }else if(disc>0){greater_than_zero(a,b);printf("x1=%5.2f\tx2=%5.2f\n",x1,x2); }else{smaller_than_zero(a,b);printf("x1=%5.2f+%5.2fi\tx2=%5.2f-%5.2fi\n",p,q,p,q); }}7.3main(){int number;scanf("%d",&number);if(prime(number))printf("yes");elseprintf("no");}int prime(number)int number;{int flag=1,n;for(n=2;n<number/2&&flag==1;n++)if(number%n==0)flag=0;return(flag);}7.4#define N 3int array[N][N];convert(array)int array[3][3];{int i,j,t;for(i=0;i<N;i++)for(j=i+1;j<N;j++){t=array[i][j];array[i][j]=array[j][i];array[j][i]=t;}}main(){int i,j;for(i=0;i<N;i++)for(j=0;j<N;j++)scanf("%d",&array[i][j]);convert(array);for(i=0;i<N;i++){printf("\n");for(j=0;j<N;j++)printf("%5d",array[i][j]);}}7.5main(){char str[100];scanf("%s",str);inverse(str);printf("%s\n",str);}inverse(str)char str[];{char t;int i,j;for(i=0,j=strlen(str);i<strlen(str)/2;i++,j--) {t=str[i];str[i]=str[j-1];str[j-1]=t;}}7.6char concate(str1,str2,str)char str1[],str2[],str[];{int i,j;for(i=0;str1[i]!='\0';i++)str[i]=str1[i];for(j=0;str2[j]!='\0';j++)str[i+j]=str2[j];str[i+j]='\0';}main(){char s1[100],s2[100],s[100];scanf("%s",s1);scanf("%s",s2);concate(s1,s2,s);printf("\ns=%s",s);}7.7main(){char str[80],c[80];void cpy();gets(str);cpy(str,c);printf("\n%s\n",c);}void cpy(s,c)char s[],c[];{int i,j;for(i=0,j=0;s[i]!='\0';i++)if(s[i]=='a'||s[i]=='A'||s[i]=='e'||s[i]=='E'||s[i]=='i'||s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||s[i]=='U'){c[j]=s[i];j++;}c[j]='\0';}7.8main(){char str[80];scanf("%s",str);insert(str);}insert(str)char str[];{int i;for(i=strlen(str);i>0;i--){str[i*2]=str[i];str[i*2-1]=' ';}printf("%s\n",str);}7.9int alph,digit,space,others;main(){char text[80];gets(text);alph=0,digit=0,space=0,others=0;count(text);printf("\nalph=%d,digit=%d,space=%d,others=%d\n",alph,digit,space,others); }count(str)char str[];{int i;for(i=0;str[i]!='\0';i++)if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))alph++;else if(str[i]>='0'&&str[i]<='9')digit++;else if(strcmp(str[i],' ')==0)space++;elseothers++;}7.10int alph(c)char c;{if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) return(1);elsereturn(0);}int longest(string)char string[];{int len=0,i,length=0,flag=1,place,point; for(i=0;i<=strlen(string);i++)if(alph(string[i]))if(flag){point=i;flag=0;}elselen++;else{flag=1;if(len>length){length=len;place=point;len=0;}}return(place);}main(){int i;char line[100];gets(line);for(i=longest(line);alph(line[i]);i++) printf("%c",line[i]);printf("\n");}7.11#define N 10char str[N];main(){int i,flag;for(flag=1;flag==1;){scanf("%s",str);if(strlen(str)>N)printf("input error");elseflag=0;}sort(str);for(i=0;i<N;i++)printf("%c",str[i]);}sort(str)char str[N];{int i,j;char t;for(j=1;j<N;j++)for(i=0;(i<N-j)&&(str[i]!='\0');i++)if(str[i]>str[i+1]){t=str[i];str[i]=str[i+1];str[i+1]=t;}}7.12#include<math.h>float solut(a,b,c,d)float a,b,c,d;{float x=1,x0,f,f1;do{x0=x;f=((a*x0+b)*x0+c)*x0+d;f1=(3*a*x0+2*b)*x0+c;x=x0-f/f1;}while(fabs(x-x0)>=1e-5);return(x);}main(){float a,b,c,d;scanf("%f,%f,%f,%f",&a,&b,&c,&d); printf("x=%10.7f\n",solut(a,b,c,d)); }7.13main(){int x,n;scanf("%d,%d",&n,&x);printf("P%d(%d)=%10.2f\n",n,x,p(n,x));}float p(tn,tx)int tn,tx;{if(tn==0)return(1);else if(tn==1)return(tx);elsereturn(((2*tn-1)*tx*p((tn-1),tx)-(tn-1)*p((tn-2),tx))/tn); }7.14#define N 10#define M 5float score[N][M];float a_stu[N],a_cor[M];main(){int i,j,r,c;float h;float s_diff();float highest();r=0;c=1;input_stu();avr_stu();avr_cor();printf("\n number class 1 2 3 4 5 avr");for(i=0;i<N;i++){printf("\nNO%2d",i+1);for(j=0;j<M;j++)printf("%8.2f",score[i][j]);printf("%8.2f",a_stu[i]);}printf("\nclassavr");for(j=0;j<M;j++)printf("%8.2f",a_cor[j]);h=highest(&r,&c);printf("\n\n%8.2f %d %d\n",h,r,c);printf("\n %8.2f\n",s_diff());}input_stu(){int i,j;for(i=0;i<N;i++){for(j=0;j<M;j++){scanf("%f",&x);score[i][j]=x;}}}avr_stu(){int i,j;float s;for(i=0;i<N;i++){for(j=0,s=0;j<M;j++)s+=score[i][j];a_stu[i]=s/5.0;}}avr_cor(){int i,j;float s;for(j=0;j<M;j++){for(i=0,s=0;i<N;i++)s+=score[i][j];a_cor[j]=s/(float)N;}}float highest(r,c)int *r,*c;{float high;int i,j;high=score[0][0];for(i=0;i<N;i++)for(j=0;j<M;j++)if(score[i][j]>high){high=score[i][j];*r=i+1;*c=j+1;}return(high);}float s_diff(){int i,j;float sumx=0.0,sumxn=0.0; for(i=0;i<N;i++){sumx+=a_stu[i]*a_stu[i];sumxn+=a_stu[i];}return(sumx/N-(sumxn/N)*(sumxn/N)); }7.15#include<stdio.h>#define N 10void input_e(num,name)int num[];char name[N][8];{int i;for(i=0;i<N;i++){scanf("%d",&num[i]);gets(name[i]);}}void sort(num,name)int num[];char name[N][8];{int i,j,min,temp1;char temp2[8];for(i=0;i<N-1;i++){min=i;for(j=i;j<N;j++)if(num[min]>num[j])min=j;temp1=num[i];num[i]=num[min];num[min]=temp1;strcpy(temp2,name[i]);strcpy(name[i],name[min]);strcpy(name[min],temp2);}for(i=0;i<N;i++)printf("\n%5d%10s",num[i],name[i]); }void search(n,num,name)int n,num[];char name[N][8];{int top,bott,min,loca;loca=0;top=0;bott=N-1;if((n<num[0])||(n>num[N-1]))while((loca==0)&&(top<=bott)){min=(bott+top)/2;if(n==num[min]){loca=min;printf("number=%d,name=%s\n",n,name[loca]);}else if(n<num[min])bott=min-1;elsetop=min+1;}if(loca==0||loca==-1)printf("number=%d is not in table\n",n);}main(){int num[N],number,flag,c,n;char name[N][8];input_e(num,name);sort(num,name);for(flag=1;flag;){scanf("%d",&number);search(number,num,name);printf("continue?Y/N!");c=getchar();if(c=='N'||c=='n')flag=0;}}7.16#include<stdio.h>#define MAX 1000main(){int c,i,flag,flag1;char t[MAX];i=0;flag=0;flag1=1;while((c=getchar())!='\0'&&i<MAX&&flag1){if(c>='0'&&c<='9'||c>='A'&&c<='F'||c>='a'&&c<='f') {flag=1;t[i++]=c;}else if(flag)printf("\nnumber=%d\n",htoi(t));printf("continue?");c=getchar();if(c=='n'||c=='N')flag1=0;else{flag=0;i=0;}}}}htoi(s)char s[];{int i,n;n=0;for(i=0;s[i]!='\0';i++){if(s[i]>='0'&&s[i]<='9')n=n*16+s[i]-'0';if(s[i]>='a'&&s[i]<='f')n=n*16+s[i]-'a'+10;if(s[i]>='A'&&s[i]<='F')n=n*16+s[i]-'A'+10;}return(n);}7.17#include<stdio.h>void convert(n)int n;{int i;if((i=n/10)!=0)convert(i);putchar(n%10+'0');}main(){int number;scanf("%d",&number);if(number<0){putchar('-');number=-number;}convert(number);}7.18main(){int year,month,day;int days;scanf("\n%d,%d,%d",&year,&month,&day);days=sum_day(month,day);if(leap(year)&&(month>=3))days+=1;printf("days=%d\n",days);}static int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31} int sum_day(month,day)int month,day;{int i;for(i=1;i<month;i++)day+=day_tab[i];return(day);}int leap(year)int year;{int leap;leap=year%4==0&&year%100!=0||year%400==0;return(leap);}第八章编译预处理8.1#define SW AP(a,b) t=b;b=a;a=tmain(){int a,b,t;scanf("%d,%d",&a,&b);SWAP(a,b);printf("a=%d\tb=%d\n",a,b);}8.2#define SURPLUS(a,b) ((a)%(b))main(){int a,b;scanf("%d,%d",&a,&b);printf("%d",SURPLUS(a,b));}8.3#include"math.h"#define S(a,b,c) ((a+b+c)/2)。

谭浩强C语言程序设计基础课后题答案.doc

谭浩强C语言程序设计基础课后题答案.doc

课后题答案第一章程序设计基本概念习题分析与解答1.1 【参考答案】.EXE1.2 【参考答案】[1].C [2].OBJ [3].EXE1.3 【参考答案】[1]顺序结构[2]选择结构[3]循环结构第二章C程序设计的初步知识习题分析与解答一、选择题2.1 【参考答案】B)2.2 【参考答案】D)2.3 【参考答案】B)2.4 【参考答案】A)2.5 参考答案】C)2.6 【参考答案】A)2.7 【参考答案】B)2.8 【参考答案】B)2.9 【参考答案】D)2.10 【参考答案】C)2.11 【参考答案】B)2.12 【参考答案】B)2.13 【参考答案】A)二、填空题2.14 【参考答案】[1]11 [2]122.15 【参考答案】[1]4.2 [2]4.22.16 【参考答案】[1]{ [2]} [3]定义[4]执行2.17 【参考答案】[1]关键字[2]用户标识符2.18 【参考答案】[1]int [2]float [3]double2.19 【参考答案】float a1=10, a2=10;或float a1=1, a2=1;(系统将自动把1转换为10)2.20 【参考答案】存储单元2.21 【参考答案】 3.52.22 【参考答案】[1]a*b/c [2]a/c*b [3]b/c*a2.23 【参考答案】把10赋给变量s2.24 【参考答案】[1]位[2]0或12.25 【参考答案】[1]8 [2]127 [3]-128 [4]111111112.26 【参考答案】[1]32767 [2]-32768 [3]11111111111111112.27 【参考答案】[1]十[2]八[3]十六三、上机改错题2.28 【分析与解答】第1行的错误:(1) include是一个程序行,因此在此行的最后不应当有分号(;)。

(2) include程序行中后面的stdio.h是一个文件名,按规定,文件名应当放在一对双引号(″″)内,或放在一对尖括号(< >)内。

《C语言程序设计》课后习题答案(第四版)谭浩强

《C语言程序设计》课后习题答案(第四版)谭浩强

第1章程序设计和C语言11.1什么是计算机程序11.2什么是计算机语言11.3C语言的发展及其特点31.4最简单的C语言程序51.4.1最简单的C语言程序举例61.4.2C语言程序的结构101.5运行C程序的步骤与方法121.6程序设计的任务141-5 #include <stdio.h>int main ( ){ printf ("**************************\n\n"); printf(" Very Good!\n\n");printf ("**************************\n"); return 0;}1-6#include <stdio.h>int main(){int a,b,c,max;printf("please input a,b,c:\n");scanf("%d,%d,%d",&a,&b,&c);max=a;if (max<b)max=b;if (max<c)max=c;printf("The largest number is %d\n",max);return 0;}第2章算法——程序的灵魂162.1什么是算法162.2简单的算法举例172.3算法的特性212.4怎样表示一个算法222.4.1用自然语言表示算法222.4.2用流程图表示算法222.4.3三种基本结构和改进的流程图262.4.4用N S流程图表示算法282.4.5用伪代码表示算法312.4.6用计算机语言表示算法322.5结构化程序设计方法34 习题36第章最简单的C程序设计——顺序程序设计37 3.1顺序程序设计举例373.2数据的表现形式及其运算393.2.1常量和变量393.2.2数据类型423.2.3整型数据443.2.4字符型数据473.2.5浮点型数据493.2.6怎样确定常量的类型513.2.7运算符和表达式523.3C语句573.3.1C语句的作用和分类573.3.2最基本的语句——赋值语句593.4数据的输入输出653.4.1输入输出举例653.4.2有关数据输入输出的概念673.4.3用printf函数输出数据683.4.4用scanf函数输入数据753.4.5字符数据的输入输出78习题823-1 #include <stdio.h>#include <math.h>int main(){float p,r,n;r=0.1;n=10;p=pow(1+r,n);printf("p=%f\n",p);return 0;}3-2-1#include <stdio.h>#include <math.h>int main(){float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;p=1000;r5=0.0585;r3=0.054;r2=0.0468;r1=0.0414;r0=0.0072;p1=p*((1+r5)*5); // 一次存5年期p2=p*(1+2*r2)*(1+3*r3); // 先存2年期,到期后将本息再存3年期p3=p*(1+3*r3)*(1+2*r2); // 先存3年期,到期后将本息再存2年期p4=p*pow(1+r1,5); // 存1年期,到期后将本息存再存1年期,连续存5次 p5=p*pow(1+r0/4,4*5); // 存活期存款。

c语言程序设计第三版谭浩强课后习题及答案

c语言程序设计第三版谭浩强课后习题及答案

c语言程序设计第三版谭浩强课后习题及答案C语言程序设计是一门基础而重要的编程课程,谭浩强教授的《C语言程序设计》第三版是许多高校计算机专业学生的必修教材。

课后习题是检验学生对课程内容掌握程度的重要手段,以下是部分习题及答案的示例。

# 第一章:C语言概述习题1:简述C语言的特点。

答案: C语言是一种结构化编程语言,具有以下特点:1. 简洁、紧凑、贴近硬件。

2. 丰富的运算符和数据类型。

3. 具有结构化控制语句。

4. 支持模块化程序设计。

5. 可移植性好。

习题2:为什么说C语言具有“高级语言”的特点?答案: C语言被称为高级语言,因为它:1. 抽象了机器语言的复杂性。

2. 允许使用变量名代替内存地址。

3. 提供了丰富的控制结构和数据类型。

4. 支持高级的编程概念,如函数、数组和指针。

# 第二章:数据类型、运算符和表达式习题3:写出C语言中的基本数据类型。

答案: C语言中的基本数据类型包括:- 整型:`int`- 浮点型:`float` 和 `double`- 字符型:`char`习题4:解释赋值运算符的右结合性。

答案:赋值运算符的右结合性意味着当多个赋值运算符出现在同一个表达式中时,它们将从右向左进行计算。

例如,在表达式 `a = b = c;` 中,首先计算 `b = c`,然后将结果赋值给 `a`。

# 第三章:控制语句习题5:描述if语句的基本用法。

答案: if语句用于基于条件执行代码块。

基本语法如下:```cif (条件) {// 条件为真时执行的代码} else {// 条件为假时执行的代码}```其中条件是一个表达式,如果其值为真(非零),则执行if块中的代码;如果为假(零),则执行else块中的代码。

习题6:举例说明for循环的使用。

答案: for循环用于重复执行一段代码,直到给定条件不再满足。

基本语法如下:```cfor (初始化表达式;条件表达式;更新表达式) {// 循环体}```例如,打印0到9的数字:```cfor (int i = 0; i < 10; i++) {printf("%d ", i);}```以上是部分习题及答案的示例。

(完整版)谭浩强c程序设计课后习题答案

(完整版)谭浩强c程序设计课后习题答案

谭浩强c++程序设计课后答案娄警卫第一章1.5题#include <iostream> using namespace std; int main(){cout<<"This"<<"is"; cout<<"a"<<"C++"; cout<<"program."; return 0;1.6题#include <iostream> using namespace std; int main(){int a,b,c;a=10;b=23;c=a+b;cout<<"a+b="; cout<<c;cout<<endl;return 0;}1.7七题#include <iostream> using namespace std; int main(){int a,b,c;int f(int x,int y,int z); cin>>a>>b>>c;c=f(a,b,c);cout<<c<<endl; return 0;}int f(int x,int y,int z) {int m;if (x<y) m=x;else m=y;if (z<m) m=z;return(m);}1.8题#include <iostream>using namespace std;int main(){int a,b,c;cin>>a>>b;c=a+b;cout<<"a+b="<<a+b<<endl; return 0;}1.9题#include <iostream>using namespace std;int main(){int a,b,c;int add(int x,int y); cin>>a>>b;c=add(a,b);cout<<"a+b="<<c<<endl; return 0;}int add(int x,int y){int z;z=x+y;return(z);}第二章2.3题#include <iostream>using namespace std;int main(){char c1='a',c2='b',c3='c',c4='\101',c5='\116'; cout<<c1<<c2<<c3<<'\n';cout<<"\t\b"<<c4<<'\t'<<c5<<'\n';return 0;}2.4题#include <iostream>using namespace std;int main(){char c1='C',c2='+',c3='+';cout<<"I say: \""<<c1<<c2<<c3<<'\"';cout<<"\t\t"<<"He says: \"C++ is very interesting!\""<< '\n';return 0;}2.7题#include <iostream>using namespace std;int main(){int i,j,m,n;i=8;j=10;m=++i+j++;n=(++i)+(++j)+m;cout<<i<<'\t'<<j<<'\t'<<m<<'\t'<<n<<endl; return 0;}2.8题#include <iostream>using namespace std;int main(){char c1='C', c2='h', c3='i', c4='n', c5='a';c1+=4;c2+=4;c3+=4;c4+=4;c5+=4;cout<<"password is:"<<c1<<c2<<c3<<c4<<c5<<endl;return 0;}第三章3.2题#include <iostream>#include <iomanip>using namespace std;int main ( ){float h,r,l,s,sq,vq,vz;const float pi=3.1415926;cout<<"please enter r,h:";cin>>r>>h;l=2*pi*r;s=r*r*pi;sq=4*pi*r*r;vq=3.0/4.0*pi*r*r*r;vz=pi*r*r*h;cout<<setiosflags(ios::fixed)<<setiosflags(ios:: right)<<setprecision(2);cout<<"l= "<<setw(10)<<l<<endl;cout<<"s= "<<setw(10)<<s<<endl;cout<<"sq="<<setw(10)<<sq<<endl;cout<<"vq="<<setw(10)<<vq<<endl;cout<<"vz="<<setw(10)<<vz<<endl;return 0;}3.3题#include <iostream>using namespace std;int main (){float c,f;cout<<"请输入一个华氏温度:";cin>>f;c=(5.0/9.0)*(f-32); //注意5和9要用实型表示,否则5/9值为0cout<<"摄氏温度为:"<<c<<endl;return 0;};3.4题#include <iostream>using namespace std;int main ( ){char c1,c2;cout<<"请输入两个字符c1,c2:";c1=getchar(); //将输入的第一个字符赋给c1c2=getchar(); //将输入的第二个字符赋给c2cout<<"用putchar函数输出结果为:"; putchar(c1);putchar(c2);cout<<endl;cout<<"用cout语句输出结果为:";cout<<c1<<c2<<endl;return 0;}3.4题另一解#include <iostream>using namespace std;int main ( ){char c1,c2;cout<<"请输入两个字符c1,c2:";c1=getchar(); //将输入的第一个字符赋给c1c2=getchar(); //将输入的第二个字符赋给c2cout<<"用putchar函数输出结果为:"; putchar(c1);putchar(44);putchar(c2);cout<<endl;cout<<"用cout语句输出结果为:";cout<<c1<<","<<c2<<endl;return 0;}3.5题#include <iostream>using namespace std;int main ( ){char c1,c2;int i1,i2; //定义为整型cout<<"请输入两个整数i1,i2:";cin>>i1>>i2;c1=i1;c2=i2;cout<<"按字符输出结果为:"<<c1<<" , "<<c2<<endl;return 0;}3.8题#include <iostream>using namespace std;int main ( ){ int a=3,b=4,c=5,x,y;cout<<(a+b>c && b==c)<<endl;cout<<(a||b+c && b-c)<<endl;cout<<(!(a>b) && !c||1)<<endl;cout<<(!(x=a) && (y=b) && 0)<<endl;cout<<(!(a+b)+c-1 && b+c/2)<<endl; return 0;}3.9题include <iostream>using namespace std;int main ( ){int a,b,c;cout<<"please enter three integer numbers:";cin>>a>>b>>c;if(a<b)if(b<c)cout<<"max="<<c;elsecout<<"max="<<b;else if (a<c)cout<<"max="<<c;elsecout<<"max="<<a;cout<<endl;return 0;}3.9题另一解#include <iostream>using namespace std;int main ( ){int a,b,c,temp,max ;cout<<"please enter three integer numbers:";cin>>a>>b>>c;temp=(a>b)?a:b; /* 将a和b中的大者存入temp中*/max=(temp>c)?temp:c; /* 将a和b中的大者与c比较,最大者存入max*/cout<<"max="<<max<<endl;return 0;}3.10题#include <iostream>using namespace std;int main ( ){int x,y;cout<<"enter x:";cin>>x;if (x<1){y=x;cout<<"x="<<x<<", y=x="<<y;}else if (x<10) // 1≤x<10{y=2*x-1;cout<<"x="<<x<<", y=2*x-1="<<y;}else// x≥10{y=3*x-11;cout<<"x="<<x<<",y=3*x-11="<<y;}cout<<endl;return 0;}3.11题#include <iostream>using namespace std; int main (){float score;char grade;cout<<"please enter score of student:"; cin>>score;while (score>100||score<0){cout<<"data error,enter data again.";cin>>score;}switch(int(score/10)){case 10:case 9: grade='A';break;case 8: grade='B';break;case 7: grade='C';break;case 6: grade='D';break;default:grade='E';}cout<<"score is "<<score<<", grade is "<<grade<<endl;return 0;}3.12题#include <iostream>using namespace std;int main (){long int num;intindiv,ten,hundred,thousand,ten_thousand,pla ce;/*分别代表个位,十位,百位,千位,万位和位数*/cout<<"enter an integer(0~99999):"; cin>>num;if (num>9999)place=5;else if (num>999)place=4;else if (num>99)place=3;else if (num>9)place=2;else place=1;cout<<"place="<<place<<endl;//计算各位数字ten_thousand=num/10000;thousand=(int)(num-ten_thousand*10000)/1 000;hundred=(int)(num-ten_thousand*10000-tho usand*1000)/100;ten=(int)(num-ten_thousand*10000-thousan d*1000-hundred*100)/10;indiv=(int)(num-ten_thousand*10000-thousa nd*1000-hundred*100-ten*10);cout<<"original order:";switch(place){case5:cout<<ten_thousand<<","<<thousand<<","< <hundred<<","<<ten<<","<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<ten<<hundred<<thousand<<ten _thousand<<endl;break;case4:cout<<thousand<<","<<hundred<<","<<ten <<","<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<ten<<hundred<<thousand<<en dl;break;case3:cout<<hundred<<","<<ten<<","<<indiv<<en dl;cout<<"reverse order:";cout<<indiv<<ten<<hundred<<endl;break;case 2:cout<<ten<<","<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<ten<<endl;break;case 1:cout<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<endl;break;}return 0;}3.13题#include <iostream>using namespace std;int main (){ long i; //i为利润floatbonus,bon1,bon2,bon4,bon6,bon10;bon1=100000*0.1; //利润为10万元时的奖金bon2=bon1+100000*0.075; //利润为20万元时的奖金bon4=bon2+100000*0.05; //利润为40万元时的奖金bon6=bon4+100000*0.03; //利润为60万元时的奖金bon10=bon6+400000*0.015; //利润为100万元时的奖金cout<<"enter i:";cin>>i;if (i<=100000)bonus=i*0.1;//利润在10万元以内按10%提成奖金else if (i<=200000)bonus=bon1+(i-100000)*0.075; //利润在10万元至20万时的奖金else if (i<=400000)bonus=bon2+(i-200000)*0.05; //利润在20万元至40万时的奖金else if (i<=600000)bonus=bon4+(i-400000)*0.03; //利润在40万元至60万时的奖金else if (i<=1000000)bonus=bon6+(i-600000)*0.015; //利润在60万元至100万时的奖金elsebonus=bon10+(i-1000000)*0.01; //利润在100万元以上时的奖金cout<<"bonus="<<bonus<<endl;return 0;}3.13题另一解#include <iostream>using namespace std;int main (){long i;float bonus,bon1,bon2,bon4,bon6,bon10; int c;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;cout<<"enter i:";cin>>i;c=i/100000;if (c>10) c=10;switch(c){case 0: bonus=i*0.1; break;case 1: bonus=bon1+(i-100000)*0.075; break;case 2:case3:bonus=bon2+(i-200000)*0.05;break;case 4:case5:bonus=bon4+(i-400000)*0.03;break;case 6:case 7:case 8:case 9: bonus=bon6+(i-600000)*0.015; break;case 10: bonus=bon10+(i-1000000)*0.01;}cout<<"bonus="<<bonus<<endl;return 0;}3.14题#include <iostream>using namespace std;int main (){int t,a,b,c,d;cout<<"enter four numbers:";cin>>a>>b>>c>>d;cout<<"a="<<a<<", b="<<b<<", c="<<c<<",d="<<d<<endl;if (a>b){t=a;a=b;b=t;}if (a>c){t=a; a=c; c=t;}if (a>d){t=a; a=d; d=t;}if (b>c){t=b; b=c; c=t;}if (b>d){t=b; b=d; d=t;}if (c>d){t=c; c=d; d=t;}cout<<"the sorted sequence:"<<endl;cout<<a<<", "<<b<<", "<<c<<", "<<d<<endl; return 0;}3.15题#include <iostream>using namespace std;int main (){int p,r,n,m,temp;cout<<"please enter two positive integer numbers n,m:";cin>>n>>m;if (n<m){temp=n;n=m;m=temp; //把大数放在n中, 小数放在m中}p=n*m; //先将n和m的乘积保存在p中, 以便求最小公倍数时用while (m!=0) //求n和m的最大公约数{r=n%m;n=m;m=r;}cout<<"HCF="<<n<<endl;cout<<"LCD="<<p/n<<endl; // p是原来两个整数的乘积return 0;}3.16题#include <iostream>using namespace std;int main (){char c;int letters=0,space=0,digit=0,other=0;cout<<"enter one line::"<<endl;while((c=getchar())!='\n'){if (c>='a' && c<='z'||c>='A' && c<='Z')letters++;else if (c==' ')space++;else if (c>='0' && c<='9')digit++;elseother++;}cout<<"letter:"<<letters<<", space:"<<space<<", digit:"<<digit<<", other:"<<other<<endl;return 0;}3.17题#include <iostream>using namespace std;int main (){int a,n,i=1,sn=0,tn=0;cout<<"a,n=:";cin>>a>>n;while (i<=n){tn=tn+a; //赋值后的tn为i个a 组成数的值sn=sn+tn; //赋值后的sn为多项式前i项之和a=a*10;++i;}cout<<"a+aa+aaa+...="<<sn<<endl;return 0;}3.18题#include <iostream>using namespace std;int main (){float s=0,t=1;int n;for (n=1;n<=20;n++){t=t*n; // 求n!s=s+t; // 将各项累加}cout<<"1!+2!+...+20!="<<s<<endl;return 0;}3.19题#include <iostream>using namespace std;int main (){int i,j,k,n;cout<<"narcissus numbers are:"<<endl;for (n=100;n<1000;n++){i=n/100;j=n/10-i*10;k=n%10;if (n == i*i*i + j*j*j + k*k*k)cout<<n<<" ";}cout<<endl;return 0;}3.20题#include <iostream>using namespace std;int main(){const int m=1000; // 定义寻找范围int k1,k2,k3,k4,k5,k6,k7,k8,k9,k10;int i,a,n,s;for (a=2;a<=m;a++) // a是2~1000之间的整数,检查它是否为完数{n=0; // n用来累计a的因子的个数s=a; // s用来存放尚未求出的因子之和,开始时等于afor (i=1;i<a;i++) // 检查i是否为a 的因子if (a%i==0) // 如果i是a的因子{n++; // n加1,表示新找到一个因子s=s-i; // s减去已找到的因子,s的新值是尚未求出的因子之和switch(n) // 将找到的因子赋给k1,...,k10{case 1:k1=i; break; // 找出的笫1个因子赋给k1case 2:k2=i; break; // 找出的笫2个因子赋给k2case 3:k3=i; break; // 找出的笫3个因子赋给k3case 4:k4=i; break; // 找出的笫4个因子赋给k4case 5:k5=i; break; // 找出的笫5个因子赋给k5case 6:k6=i; break; // 找出的笫6个因子赋给k6case 7:k7=i; break; // 找出的笫7个因子赋给k7case 8:k8=i; break; // 找出的笫8个因子赋给k8case 9:k9=i; break; // 找出的笫9个因子赋给k9case 10:k10=i; break; // 找出的笫10个因子赋给k10}}if (s==0) // s=0表示全部因子都已找到了{cout<<a<<" is a 完数"<<endl;cout<<"its factors are:";if (n>1) cout<<k1<<","<<k2; // n>1表示a至少有2个因子if (n>2) cout<<","<<k3; // n>2表示至少有3个因子,故应再输出一个因子if (n>3) cout<<","<<k4; // n>3表示至少有4个因子,故应再输出一个因子if (n>4) cout<<","<<k5; // 以下类似if (n>5) cout<<","<<k6;if (n>6) cout<<","<<k7;if (n>7) cout<<","<<k8;if (n>8) cout<<","<<k9;if (n>9) cout<<","<<k10;cout<<endl<<endl;}}return 0;}3.20题另一解#include <iostream>using namespace std;int main(){int m,s,i;for (m=2;m<1000;m++){s=0;for (i=1;i<m;i++)if ((m%i)==0) s=s+i;if(s==m){cout<<m<<" is a完数"<<endl;cout<<"its factors are:";for (i=1;i<m;i++)if (m%i==0) cout<<i<<" ";cout<<endl;}}return 0;}3.20题另一解#include <iostream>using namespace std;int main(){int k[11];int i,a,n,s;for (a=2;a<=1000;a++){n=0;s=a;for (i=1;i<a;i++)if ((a%i)==0){n++;s=s-i;k[n]=i; // 将找到的因子赋给k[1]┅k[10]}if (s==0){cout<<a<<" is a 完数"<<endl;cout<<"its factors are:";for (i=1;i<n;i++)cout<<k[i]<<" ";cout<<k[n]<<endl;}}return 0;}3.21题#include <iostream>using namespace std;int main(){int i,t,n=20;double a=2,b=1,s=0;for (i=1;i<=n;i++){s=s+a/b;t=a;a=a+b; // 将前一项分子与分母之和作为下一项的分子b=t; // 将前一项的分子作为下一项的分母}cout<<"sum="<<s<<endl;return 0;} 3.22题#include <iostream>using namespace std;int main(){int day,x1,x2;day=9;x2=1;while(day>0){x1=(x2+1)*2; // 第1天的桃子数是第2天桃子数加1后的2倍x2=x1;day--;}cout<<"total="<<x1<<endl;return 0;}3.23题#include <iostream>#include <cmath>using namespace std;int main(){float a,x0,x1;cout<<"enter a positive number:"; cin>>a; // 输入a的值x0=a/2;x1=(x0+a/x0)/2;do{x0=x1;x1=(x0+a/x0)/2;}while(fabs(x0-x1)>=1e-5);cout<<"The square root of "<<a<<" is "<<x1<<endl;return 0;}3.24题#include <iostream>using namespace std;int main(){int i,k;for (i=0;i<=3;i++) // 输出上面4行*号{for (k=0;k<=2*i;k++)cout<<"*"; // 输出*号cout<<endl; //输出完一行*号后换行}for (i=0;i<=2;i++) // 输出下面3行*号{for (k=0;k<=4-2*i;k++)cout<<"*"; // 输出*号cout<<endl; // 输出完一行*号后换行}return 0;}3.25题#include <iostream>using namespace std;int main(){char i,j,k; /* i是a的对手;j是b的对手;k是c的对手*/for (i='X';i<='Z';i++)for (j='X';j<='Z';j++)if (i!=j)for (k='X';k<='Z';k++)if (i!=k && j!=k)if (i!='X' && k!='X' && k!='Z')cout<<"A--"<<i<<"B--"<<j<<" C--"<<k<<endl;return 0;}第四章4.1题#include <iostream>using namespace std;int main(){int hcf(int,int);int lcd(int,int,int);int u,v,h,l;cin>>u>>v;h=hcf(u,v);cout<<"H.C.F="<<h<<endl;l=lcd(u,v,h);cout<<"L.C.D="<<l<<endl;return 0;}int hcf(int u,int v){int t,r;if (v>u){t=u;u=v;v=t;}while ((r=u%v)!=0){u=v;v=r;}return(v);}int lcd(int u,int v,int h){return(u*v/h);}4.2题#include <iostream>#include <math.h>using namespace std;float x1,x2,disc,p,q;int main(){void greater_than_zero(float,float); void equal_to_zero(float,float);void smaller_than_zero(float,float); float a,b,c;cout<<"input a,b,c:";cin>>a>>b>>c;disc=b*b-4*a*c;cout<<"root:"<<endl;if (disc>0){greater_than_zero(a,b);cout<<"x1="<<x1<<",x2="<<x2<<endl; }else if (disc==0){equal_to_zero(a,b);cout<<"x1="<<x1<<",x2="<<x2<<endl;}else{smaller_than_zero(a,b);cout<<"x1="<<p<<"+"<<q<<"i"<<endl;cout<<"x2="<<p<<"-"<<q<<"i"<<endl;}return 0;}void greater_than_zero(float a,float b) /* 定义一个函数,用来求disc>0时方程的根*/{x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);}void equal_to_zero(float a,float b) /* 定义一个函数,用来求disc=0时方程的根*/{x1=x2=(-b)/(2*a);}void smaller_than_zero(float a,float b) /* 定义一个函数,用来求disc<0时方程的根*/{p=-b/(2*a);q=sqrt(-disc)/(2*a);}4.3题#include <iostream>using namespace std;int main(){int prime(int); /* 函数原型声明*/int n;cout<<"input an integer:";cin>>n;if (prime(n))cout<<n<<" is a prime."<<endl;elsecout<<n<<" is not a prime."<<endl;return 0;}int prime(int n){int flag=1,i;for (i=2;i<n/2 && flag==1;i++)if (n%i==0)flag=0;return(flag);}4.4题#include <iostream>using namespace std;int main(){int fac(int);int a,b,c,sum=0;cout<<"enter a,b,c:";cin>>a>>b>>c;sum=sum+fac(a)+fac(b)+fac(c);cout<<a<<"!+"<<b<<"!+"<<c<<"!="<<sum<<e ndl;return 0;}int fac(int n){int f=1;for (int i=1;i<=n;i++)f=f*i;return f;}4.5题#include <iostream>#include <cmath>using namespace std;int main(){double e(double);double x,sinh;cout<<"enter x:";cin>>x;sinh=(e(x)+e(-x))/2;cout<<"sinh("<<x<<")="<<sinh<<endl;return 0;}double e(double x){return exp(x);}4.6题#include <iostream>#include <cmath>using namespace std;int main(){doublesolut(double ,double ,double ,double ); double a,b,c,d;cout<<"input a,b,c,d:";cin>>a>>b>>c>>d;cout<<"x="<<solut(a,b,c,d)<<endl;return 0;}double solut(double a,double b,double c,double d){double x=1,x0,f,f1;do{x0=x;f=((a*x0+b)*x0+c)*x0+d;f1=(3*a*x0+2*b)*x0+c;x=x0-f/f1;}while(fabs(x-x0)>=1e-5);return(x);}4.7题#include <iostream>#include <cmath>using namespace std;int main(){void godbaha(int);int n;cout<<"input n:";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<<n<<"="<<a<<"+"<<b<<endl;}}}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 <iostream>using namespace std;int main(){int x,n;float p(int,int);cout<<"input n & x:";cin>>n>>x;cout<<"n="<<n<<",x="<<x<<endl;;cout<<"P"<<n<<"(x)="<<p(n,x)<<endl; return 0;}float p(int n,int x){if (n==0)return(1);else if (n==1)return(x);elsereturn(((2*n-1)*x*p((n-1),x)-(n-1)*p((n-2),x))/n);}4.9题#include <iostream>using namespace std;int main(){void hanoi(int n,char one,char two,char three);int m;cout<<"input the number of diskes:"; cin>>m;cout<<"The steps of moving "<<m<<" disks:"<<endl;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<<x<<"-->"<<y<<endl;}4.10题#include <iostream>using namespace std;int main(){void convert(int n);int number;cout<<"input an integer:";cin>>number;cout<<"output:"<<endl;if (number<0){cout<<"-";number=-number;}convert(number);cout<<endl;return 0;}void convert(int n){int i;char c;if ((i=n/10)!=0)convert(i);c=n%10+'0';cout<<" "<<c;}4.11题#include <iostream>using namespace std;int main(){int f(int);int n,s;cout<<"input the number n:";cin>>n;s=f(n);cout<<"The result is "<<s<<endl;return 0;}int f(int n){;if (n==1)return 1;elsereturn (n*n+f(n-1));}4.12题#include <iostream>#include <cmath>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<<"input a,b,c:";cin>>a>>b>>c;if (a+b>c && a+c>b && b+c>a)cout<<"area="<<AREA(a,b,c)<<endl; elsecout<<"It is not a triangle!"<<endl; return 0;}4.14题#include <iostream>using namespace std;//#define LETTER 1int main(){char c;cin>>c;#if LETTERif(c>='a' && c<='z')c=c-32;#elseif(c>='A' && c<='Z')c=c+32;#endifcout<<c<<endl;return 0;}4.15题#include <iostream>using namespace std;#define CHANGE 1int main(){char ch[40];cout<<"input text:"<<endl;;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;}}#endifcout<<"output:"<<endl<<ch<<endl;return 0;}4.16题file#include <iostream>using namespace std;int a;int main(){extern int power(int);int b=3,c,d,m;cout<<"enter an integer a and its power m:"<<endl;cin>>a>>m;c=a*b;cout<<a<<"*"<<b<<"="<<c<<endl;d=power(m);cout<<a<<"**"<<m<<"="<<d<<endl; return 0;}4.16题fileextern int a;int power(int n){int i,y=1;for(i=1;i<=n;i++)y*=a;return y;}第五章5.1题#include <iostream>#include <iomanip>using namespace std;#include <math.h>int main(){int i,j,n,a[101];for (i=1;i<=100;i++)a[i]=i;a[1]=0;for (i=2;i<sqrt(100);i++)for (j=i+1;j<=100;j++){if(a[i]!=0 && a[j]!=0)if (a[j]%a[i]==0)a[j]=0; }cout<<endl;for (i=1,n=0;i<=100;i++){if (a[i]!=0){cout<<setw(5)<<a[i]<<" ";n++;}if(n==10){cout<<endl;n=0;}}cout<<endl;return 0;}5.2题#include <iostream>using namespace std;//#include <math.h>int main(){int i,j,min,temp,a[11];cout<<"enter data:"<<endl;for (i=1;i<=10;i++){cout<<"a["<<i<<"]=";cin>>a[i]; //输入10个数}cout<<endl<<"The original numbers:"<<endl;;for (i=1;i<=10;i++)cout<<a[i]<<" "; // 输出这10个数cout<<endl;;for (i=1;i<=9;i++) //以下8行是对10个数排序{min=i;for (j=i+1;j<=10;j++)if (a[min]>a[j]) min=j;temp=a[i]; //以下3行将a[i+1]~a[10]中最小者与a[i] 对换a[i]=a[min];a[min]=temp;}cout<<endl<<"The sorted numbers:"<<endl;for (i=1;i<=10;i++) // 输出已排好序的10个数cout<<a[i]<<" ";cout<<endl;return 0;}5.3题#include <iostream>using namespace std;int main(){int a[3][3],sum=0;int i,j;cout<<"enter data:"<<endl;;for (i=0;i<3;i++)for (j=0;j<3;j++)cin>>a[i][j];for (i=0;i<3;i++)sum=sum+a[i][i];cout<<"sum="<<sum<<endl;return 0;}5.4题#include <iostream>using namespace std;int main(){int a[11]={1,4,6,9,13,16,19,28,40,100};int num,i,j;cout<<"array a:"<<endl;for (i=0;i<10;i++)cout<<a[i]<<" ";cout<<endl;;cout<<"insert data:";cin>>num;if (num>a[9])a[10]=num;else。

C语言程序设计第三版谭浩强课后习题答案完整版

C语言程序设计第三版谭浩强课后习题答案完整版

C语言程序设计第三版谭浩强课后习题答案完整版第一章1.5请参照本章例题,编写一个C程序,输出以下信息:**************************Very Good!**************************解:mian(){printf(“**************************”);printf(“\n”);printf(“Very Good!\n”);printf(“\n”);printf(“**************************”);}1.6 编写一个程序,输入a、b、c三个值,输出其中最大值。

解:mian(){int a,b,c,max;printf(“请输入三个数a,b,c:\n”);scanf(“%d,%d,%d”,&a,&b,&c);max=a;if(max<b)max=b;if(max<c)max=c;printf(“最大数为:“%d”,max);}第三章3.6写出以下程序运行的结果。

main(){char c1=’a’,c2=’b’,c3=’c’,c4=’\101’,c5=’\116’;printf(“a%cb%c\tc%c\tabc\n”,c1,c2,c3);printf(“\t\b%c %c”,c4,c5);}解:aaㄩbbㄩㄩㄩccㄩㄩㄩㄩㄩㄩabcAㄩN3.7要将"China"译成密码,译码规律是:用原来字母后面的第4个字母代替原来的字母.例如,字母"A"后面第4个字母是"E"."E"代替"A"。

因此,"China"应译为"Glmre"。

请编一程序,用赋初值的方法使cl、c2、c3、c4、c5五个变量的值分别为,’C’、’h’、’i’、’n’、’a’,经过运算,使c1、c2、c3、c4、c5分别变为’G’、’l’、’m’、’r’、’e’,并输出。

C程序设计谭浩强完整版课后习题答案

C程序设计谭浩强完整版课后习题答案
a=5;
b=4;
sum=a+b;
printf("The sum is %d .\n",sum);
return0;
}
P008调用函数比较两个数的大小.
#include<>
intmain()
{
intmax(intx,inty);inta,b,c;
scanf("%d,%d",&a,&b);c=max(a,b);printf("The max is %d .\n",c);
{
doublea,b,c,disc,x1,x2,p,q;
scanf("%lf %lf %lf",&a,&b,&c);
disc=b*b-4*a*c;
p=-b/*a);
q=sqrt(disc)/*a);
x1=p+q;
x2=p-q;
printf("x1=%\nx2=%",x1,x2);
{
floatoutput_fah;
output_fah=5)*input_cen+32;returnoutput_fah;
}
intmain()
{
intchoice;
floatinput_fah,input_cen,output_fah,output_cen;printf("F_to_C press <1> and C_to_F press <2> !\n");
scanf("%d",&n);
if(n%3==0&&n%5==0)printf ("Can be devide by 3 and 5 !\n");

C语言程序设计第三版谭浩强课后习题答案完整版

C语言程序设计第三版谭浩强课后习题答案完整版

C语⾔程序设计第三版谭浩强课后习题答案完整版C语⾔程序设计第三版谭浩强课后习题答案完整版第⼀章1.5请参照本章例题,编写⼀个C程序,输出以下信息:**************************值,输出其中最⼤值。

解:mian(){int a,b,c,max;printf(“请输⼊三个数a,b,c:\n”);scanf(“%d,%d,%d”,&a,&b,&c);max=a;if(maxmax=b;if(maxmax=c;\1aaㄩbbㄩㄩㄩccㄩㄩㄩㄩㄩㄩabc AㄩN3.7 要将"China"译成密码,译码规律是:⽤原来字母后⾯的第4个字母代替原来的字母.例如,字母"A"后⾯第4个字母是"E"."E"代替"A"。

因此,"China"应译为"Glmre"。

请编⼀程序,⽤赋初值的⽅法使cl、c2、c3、c4、c5五个变量的值分别为,’C’、’h’、’i’、’n’、’a’,经过运算,使c1、c2、c3、c4、c5分别变为’G’、’l’、’m’、’r’、’e’,并输{是%c%c%c%c%c\n",c1,c2,c3,c4,c5); }运⾏结果:密码是Glmre3.9求下⾯算术表达式的值。

(1)x+a%3*(int)(x+y)%2/4设x=2.5,a=7,y=4.7(2)(float)(a+b)/2+(int)x%(int)y 设a=2,b=3,x=3.5,y=2.5(1)2.5(2)3.5设量。

(1)a+=a (2) a-=2 (3) a*=2+3 (4)a/=a+a(5) a%=(n%=2),n的值等于5(6)a+=a-=a*=a解:(1) 24 (2) 10 (3) 60 (4) 0 (5) 0 (6) 0第四章4.4若a=3,b=4,c=5,x=1.2,y=2.4,z=-3.6,u=51274,n=128765,c1=’a ’,c2=c1=c2=printf("a=%2d b=%2dc=%2d\n",a,b,c);printf("x=%f,y=%f,z=%f\n",x,y,z); printf("x+y=%5.2fy+z=%5.2fz+x=%5.2f\n",x+y,y+z,z+x);printf("u=%6ld n=%9ld\n",u,n);printf("%s %s %d%s\n","c1=’a’","or",c1,"(ASCII)");printf("%s %s %d%s\n","c2=’a’","or",c2,"(ASCII)"); }ˊA ˊ,;); 4.8设圆半径r=1.5,圆柱⾼h=3,求圆周长、圆⾯积、圆球表⾯积、圆球体积、圆柱体积。

《C语言程序设计》课后习题答案(第四版)谭浩强

《C语言程序设计》课后习题答案(第四版)谭浩强

《C语言程序设计》课后习题答案(第四版)谭浩强第1章程序设计和c语言11.1什么是计算机程序11.2什么是计算机语言11.3c语言的发展及其特点31.4最简单的c语言程序51.4.1最简单的c语言程序举例61.4.2c语言程序的结构101.5运转c程序的步骤与方法121.6程序设计的任务141-5#includeintmain(){printf(\printf(\verygood!\\n\\n\printf(\return0;}1-6#includeintmain(){inta,b,c,max;printf(\scanf(\max=a;if(maxprintf(\return0;}第2章算法――程序的灵魂162.1什么是算法162.2简单的算法举例172.3算法的特性212.4怎样则表示一个算法222.4.1用自然语言则表示算法222.4.2用流程图则表示算法222.4.3三种基本结构和改进的流程图262.4.4用ns流程图表示算法282.4.5用伪代码表示算法312.4.6用计算机语言表示算法322.5结构化程序设计方法34习题36第章最简单的c程序设计――顺序程序设计373.1顺序程序设计举例373.2数据的表现形式及其运算393.2.1常量和变量393.2.2数据类型423.2.3整型数据443.2.4字符型数据473.2.5浮点型数据493.2.6怎样确定常量的类型513.2.7运算符和表达式523.3c语句573.3.1c语句的促进作用和分类573.3.2最基本的语句――赋值语句593.4数据的输入输出653.4.1输入输出举例653.4.2有关数据输入输出的概念673.4.3用printf函数输入数据683.4.4用scanf函数输出数据753.4.5字符数据的输入输出78习题823-1#include#includeintmain(){floatp,r,n;r=0.1;n=10;p=pow(1+r,n);printf(\return0;}3-2-1#include#includeintmain(){floatr5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;p=1000;r5=0.0585;r3=0.054;r2=0.0468;r1=0.0414;r0=0.0072;p1=p*((1+r5)*5);//一次存5年期p2=p*(1+2*r2)*(1+3*r3);//先存2年期,到期后将本息再存有3年期p3=p*(1+3*r3)*(1+2*r2);//先存3年期,到期后将本息再存有2年期p4=p*pow(1+r1,5);//存1年期,到期后将本息存再存1年期,连续存5次p5=p*pow(1+r0/4,4*5);//存活期存款。

C程序设计(第三版)谭浩强 清华大学出版社 课后习题答案

C程序设计(第三版)谭浩强 清华大学出版社 课后习题答案

C程序设计(第三版)谭浩强清华大学出版社课后习题答案第一章1.5# include <stdio.h>void main(){printf("* * * * * * * * *\n V ery good!\n* * * * * * * * *\n");}1.6(法一)#include <stdio.h>main(){int a,b,c,max;scanf("%d,%d,%d",&a,&b,&c);max=(a>b)?a:b;max=(max>c)?max:c;printf("max=%d\n",max);}(法二)main(){int a,b,c,max;scanf("%d,%d,%d",&a,&b,&c);if(a>b&&a>c)max=a;else if (b>c)max=b;elsemax=c;printf("max=%d\n",max);}(法三)# include <stdio.h>void main(){int max(int x,int y,int z);int a,b,c,d;scanf("%d,%d,%d",&a,&b,&c);d=max(a,b,c);printf("max=%d\n",d);}int max(int x,int y,int z){int A,B;if(x>y) A=x;else A=y;if(z>A) B=z;else B=A;return(B);}第三章3.3(1)(10)10=(12)8=(a)16(2)(32)10=(40)8=(20)16(3)(75)10=(113)8=(4b)16(4)(-617)10=(176627)8=(fd97)16(5)(-111)10=(177621)8=(ff91)16(6)(2483)10=(4663)8=(963)16(7)(-28654)10=(110022)8=(9012)16(8)(21003)10=(51013)8=(520b)163.6aabb (8)cc (8)abc(7)AN3.7main(){char c1='C',c2='h',c3='i',c4='n',c5='a';c1+=4, c2+=4, c3+=4, c4+=4, c5+=4; printf("%c%c%c%c%c\n",c1,c2,c3,c4,c5); }3.8main(){int c1,c2;c1=97;c2=98;printf("%c %c",c1,c2);}(1)=2.5(2)=3.53.109,11,9,103.12(1)24 (2)10 (3)60 (4)0 (5)0 (6)0第四章4.4main(){int a,b,c;long int u,n;float x,y,z;char c1,c2;a=3;b=4;c=5;x=1.2;y=2.4;z=-3.6;u=51274;n=128765;c1='a';c2='b';printf("\n");printf("a=%2d b=%2d c=%2d\n",a,b,c);printf("x=%8.6f,y=%8.6f,z=%9.6f\n",x,y,z);printf("x+y=%5.2f y+z=%5.2f z+x=%5.2f\n",x+y,y+z,z+x); printf("u=%6ld n=%9ld\n",u,n);printf("c1='%c'or %d(ASCII)\n",c1,c1);printf("c2='%c'or %d(ASCII)\n",c2,c2);}4.5575 767.856400,-789.12396267.856400,-789.12396267.86 -789.12,67.856400,-789.123962,67.856400,-789.123962 6.785640e+001,-7.89e+002A,65,101,411234567,4553207,d68765535,177777,ffff,-1COMPUTER, COMa=3 b=7/x=8.5 y=71.82/c1=A c2=a/4.710 20Aa1.5 -3.75 +1.4,67.8/(空3)10(空3)20Aa1.5(空1)-3.75(空1)(随意输入一个数),67.8回车4.8main(){float pi,h,r,l,s,sq,sv,sz;pi=3.1415926;printf("input r,h\n");scanf("%f,%f",&r,&h);l=2*pi*r;s=r*r*pi;sq=4*pi*r*r;sv=4.0/3.0*pi*r*r*r;sz=pi*r*r*h;printf("l=%6.2f\n",l);printf("s=%6.2f\n",s);printf("sq=%6.2f\n",sq);printf("vq=%6.2f\n",sv);printf("vz=%6.2f\n",sz);}4.9main(){float c,f;scanf("%f",&f);c=(5.0/9.0)*(f-32);printf("c=%5.2f\n",c);}4.10#include"stdio.h"main(){char c1,c2;scanf("%c,%c",&c1,&c2);putchar(c1);putchar(c2);printf("\n");printf("%c%c\n",c1,c2);第五章5.3(1)0 (2)1 (3)1 (4)0 (5)15.4main(){int a,b,c;scanf("%d,%d,%d",&a,&b,&c); if(a<b)if(b<c)printf("max=%d\n",c);elseprintf("max=%d\n",b); else if(a<c)printf("max=%d\n",c); elseprintf("max=%d\n",a);}main(){int a,b,c,temp,max;scanf("%d,%d,%d",&a,&b,&c); temp=(a>b)?a:b;max=(c>temp)?c:temp;printf("max=%d",max);}5.5main(){int x,y;scanf("%d",&x);if(x<1)y=x;else if(x<10)y=2*x-1;else y=3*x-11;printf("y=%d",y);}5.6main(){int score,temp,logic;char grade;logic=1;while(logic){scanf("%d",&score);if(score>=0&&score<=100)logic=0;}if(score==100)temp=9;elsetemp=(score-score%10)/10;switch(temp){case 9:grade='A';break;case 8:grade='B';break;case 7:grade='C';break;case 6:grade='D';break;case 5:case 4:case 3:case 2:case 1:case 0:grade='E';}printf"score=%d,grade=%c",score,grade);}5.7main(){long int num;int indiv,ten,hundred,thousand,ten_thousand,place;scanf("%ld",&num);if(num>9999) place=5;else if(num>999) place=4;else if(num>99) place=3;else if(num>9) place=2;else place=1;printf("place=%d\n",place);ten_thousand=num/10000;thousand=(num-ten_thousand*10000)/1000;hundred=(num-ten_thousand*10000-thousand*1000)/100;ten=(num-ten_thousand*10000-thousand*1000-hundred*100)/10;indiv=num-ten_thousand*10000-thousand*1000-hundred*100-ten*10;switch(place){case 5:printf("%d,%d,%d,%d,%d\n",ten_thousand,thousand,hundred,ten,indiv);printf("%d,%d,%d,%d,%d\n",indiv,ten,hundred,thousand,ten_thousand);break;case 4:printf("%d,%d,%d,%d\n",thousand,hundred,ten,indiv);printf("%d,%d,%d,%d\n",indiv,ten,hundred,thousand);break;case 3:printf("%d,%d,%d\n",hundred,ten,indiv);printf("%d,%d,%d\n",indiv,ten,hundred);break;case 2:printf("%d,%d\n",ten,indiv);printf("%d,%d\n",indiv,ten);break;case 1:printf("%d\n",indiv);printf("%d\n",indiv);}}5.8main(){long i;float bonus,bon1,bon2,bon4,bon6,bon10;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;scanf("%ld",&i);if(i<=1e5)bonus=i*0.1;else if(i<=2e5)bonus=bon1+(i-100000)*0.075;else if(i<=4e5)bonus=bon2+(i-200000)*0.05;else if(i<=6e5)bonus=bon4+(i-400000)*0.03;else if(i<=1e6)bonus=bon6+(i-600000)*0.015;else bonus=bon10+(i-1000000)*0.01;printf("bonus=%10.2f",bonus);}main(){long i;float bonus,bon1,bon2,bon4,bon6,bon10;int branch;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;scanf("%ld",&i);branch=i/100000;if(branch>10)branch=10;switch(branch){case 0:bonus=i*0.1;break;case 1:bonus=bon1+(i-100000)*0.075;break; case 2:case 3:bonus=bon2+(i-200000)*0.05;break; case 4:case 5:bonus=bon4+(i-400000)*0.03;break; case 6:case 7case 8:case 9:bonus=bon6+(i-600000)*0.015;break; case 10:bonus=bon10+(i-1000000)*0.01;}printf("bonus=%10.2f",bonus);}4.9main(){int t,a,b,c,d;scanf("%d,%d,%d,%d",&a,&b,&c,&d);if(a>b){t=a;a=b;b=t;}if(a>c){t=a;a=c;c=t;}if(a>d){t=a;a=d;d=t;}if(b>c){t=b;b=c;c=t;}if(b>d){t=b;b=d;d=t;}if(c>d){t=c;c=d;d=t;}printf("%d %d %d %d\n",a,b,c,d);}5.10main(){int h=10;float x,y,x0=2,y0=2,d1,d2,d3,d4;scanf("%f,%f",&x,&y);d1=(x-x0)*(x-x0)+(y-y0)*(y-y0);d2=(x-x0)*(x-x0)+(y+y0)*(y+y0);d3=(x+x0)*(x+x0)+(y-y0)*(y-y0);d4=(x+x0)*(x+x0)+(y+y0)*(y+y0);if(d1>1&&d2>1&&d3>1&&d4>1)h=0; printf("h=%d",h);}第六章循环控制6.1main(){int a,b,num1,num2,temp;scanf("%d,%d",&num1,&num2);if(num1<num2){temp=num1;num1=num2;num2=temp;}a=num1;b=num2;while(b!=0){temp=a%b;a=b;b=temp;}printf("%d\n",a);printf("%d\n",num1*num2/a);}6.2#include"stdio.h"main(){char c;int letters=0,space=0,digit=0,other=0;while((c=getchar())!='\n'){if(c>='a'&&c<='z'||c>='A'&&c<='Z') letters++;else if(c==' ')space++;else if(c>='0'&&c<='9')digit++;else other++;}printf("letters=%d\nspace=%d\ndigit=%d\nother=%d\n",letters,space,digit,other); }6.3main(){int a,n,count=1,sn=0,tn=0;scanf("%d,%d",&a,&n);while(count<=n){tn+=a;sn+=tn;a*=10;++count;}printf("a+aa+aaa+...=%d\n",sn);}6.4main(){float n,s=0,t=1;for(n=1;n<=20;n++){t*=n;s+=t;}printf("s=%e\n",s);}6.5main(){int N1=100,N2=50,N3=10;float k;float s1=0,s2=0,s3=0;for(k=1;k<=N1;k++)s1+=k;for(k=1;k<=N2;k++)s2+=k*k;for(k=1;k<=N3;k++)s3+=1/k;printf("s=%8.2f\n",s1+s2+s3);}6.6main(){int i,j,k,n;for(n=100;n<1000;n++){i=n/100;j=n/10-i*10;k=n%10;if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)printf("n=%d\n",n);}}6.7#define M 1000main(){int k0,k1,k2,k3,k4,k5,k6,k7,k8,k9;int i,j,n,s;for(j=2;j<=M;j++){n=0;s=j;for(i=1;i<j;i++){if((j%i)==0){n++;s=s-i;switch(n){case 1:k0=i;break;case 2:k1=i;break;case 3:k2=i;break;case 4:k3=i;break;case 5:k4=i;break;case 6:k5=i;break;case 7:k6=i;break;case 8:k7=i;break;case 9:k8=i;break;case 10:k9=i;break;}}}if(s==0){printf("j=%d\n",j);if(n>1)printf("%d,%d",k0,k1);if(n>2)printf(",%d",k2);if(n>3)printf(",%d",k3);if(n>4)printf(",%d",k4);if(n>5)printf(",%d",k5);if(n>6)printf(",%d",k6);if(n>7)printf(",%d",k7);if(n>8)printf(",%d",k8);if(n>9)printf(",%d\n",k9);}}}main(){static int k[10];int i,j,n,s;for(j=2;j<=1000;j++){n=-1;s=j;for(i=1;i<j;i++){if((j%i)==0){n++;s=s-i;k[n]=i;}}if(s==0){printf("j=%d\n",j);for(i=0;i<n;i++)printf("%d,",k[i]);printf("%d\n",k[n]);}}}6.8main(){int n,t,number=20; float a=2;b=1;s=0;for(n=1;n<=number;n++) {s=s+a/b;t=a,a=a+b,b=t;}printf("s=%9.6f\n",s);}6.9main(){float sn=100.0,hn=sn/2; int n;for(n=2;n<=10;n++){sn=sn+2*hn;hn=hn/2;}printf("sn=%f\n",sn); printf("hn=%f\n",hn);}6.10main(){int day,x1,x2;day=9;x2=1;while(day>0){x1=(x2+1)*2;x2=x1;day--;}printf("x1=%d\n",x1);}6.11#include"math.h"main(){float a,xn0,xn1;scanf("%f",&a);xn0=a/2;xn1=(xn0+a/xn0)/2;do{xn0=xn1;xn1=(xn0+a/xn0)/2;}while(fabs(xn0-xn1)>=1e-5);printf("a=%5.2f\n,xn1=%8.2f\n",a,xn1); }6.12#include"math.h"main(){float x,x0,f,f1;x=1.5;do{x0=x;f=((2*x0-4)*x0+3)*x0-6;f1=(6*x0-8)*x0+3;x=x0-f/f1;}while(fabs(x-x0)>=1e-5);printf("x=%6.2f\n",x);}6.13#include"math.h"main(){float x0,x1,x2,fx0,fx1,fx2;do{scanf("%f,%f",&x1,&x2);fx1=x1*((2*x1-4)*x1+3)-6;fx2=x2*((2*x2-4)*x2+3)-6;}while(fx1*fx2>0);do{x0=(x1+x2)/2;fx0=x0*((2*x0-4)*x0+3)-6;if((fx0*fx1)<0){x2=x0;fx2=fx0;}else{x1=x0;fx1=fx0;}}while(fabs(fx0)>=1e-5);printf("x0=%6.2f\n",x0);}6.14main(){int i,j,k;for(i=0;i<=3;i++){for(j=0;j<=2-i;j++)printf(" ");for(k=0;k<=2*i;k++)printf("*");printf("\n");}for(i=0;i<=2;i++){for(j=0;j<=i;j++)printf(" ");for(k=0;k<=4-2*i;k++)printf("*");printf("\n");}}6.15main(){char i,j,k;for(i='x';i<='z';i++)for(j='x';j<='z';j++){if(i!=j)for(k='x';k<='z';k++){if(i!=k&&j!=k){if(i!='x'&&k!='x'&&k!='z')printf("\na--%c\tb--%c\tc--%c\n",i,j,k);}}}}第七章数组7.1#include <math.h>#define N 101main(){ int i,j,line,a[N];for (i=2;i<N;i++) a[i]=i;for (i=2;i<sqrt(N);i++)for (j=i+1;j<N;j++){if(a[i]!=0 && a[j]!=0)if (a[j]%a[i]==0)a[j]=0; } printf("\n");for (i=2,line=0;i<N;i++){ if(a[i]!=0){ printf("%5d",a[i]);line++; }if(line==10){ printf("\n");line=0; }}}7.2#define N 10main(){int i,j,min,temp,a[N];for(i=0;i<N;i++)scanf("%d",&a[i]);for(i=0;i<N-1;i++){min=i;for(j=i+1;j<N;j++)if(a[min]>a[j])min=j;temp=a[i];a[i]=a[min];a[min]=temp;}for(i=0;i<N;i++)printf("%5d",a[i]);}7.3main(){float a[3][3],sum;int i,j;for(i=0;i<3;i++)for(j=0;j<3;j++){scanf("%f",&sum);a[i][j]=sum;}for(i=0;i<3;i++)sum=sum+a[i][i];printf("sum=%f",sum);}7.4main(){int a[11]={1,4,6,9,13,16,19,28,40,100}; int temp1,temp2,number, end,i,j;scanf("%d",&number);end=a[9];if(number>end) a[10]=number;else{for(i=0;i<10;i++){if(a[i]>number){temp1=a[i];a[i]=number;for(j=i+1;j<11;j++){temp2=a[j];a[j]=temp1;temp1=temp2;}break;}}}for(i=0;i<11;i++)printf("%6d",a[i]);}7.5#define N 5main(){int a[N]={8,6,5,4,1},i,temp;for(i=0;i<N/2;i++){temp=a[i];a[i]=a[N-i-1];a[N-i-1]=temp;}for(i=0;i<N;i++)printf("%4d",a[i]);}7.6#define N 11main(){int i,j,a[N][N];for(i=1;i<N;i++){a[i][i]=1;a[i][1]=1;}for(i=3;i<N;i++)for(j=2;j<i;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];for(i=1;i<N;i++){for(j=1;j<=i;j++)printf("%6d",a[i][j]);printf("\n");}}7.7main(){int a[16][16],i,j,k,p,m,n;p=1;while(p==1){scanf("%d",&n);if((n!=0)&&(n<=15)&&(n%2!=0))p=0;}for(i=1;i<=n;i++)for(j=1;j<=n;j++)a[i][j]=0;j=n/2+1;a[1][j]=1;for(k=2;k<=n*n;k++){i=i-1;j=j+1;if((i<1)&&(j>n)){i=i+2;j=j-1;}else{if(i<1)i=n;if(j>n)j=1;}if(a[i][j]==0)a[i][j]=k;else{i=i+2;j=j-1;a[i][j]=k;}}for(i=1;i<=n;i++){for(j=1;j<=n;j++)printf("%3d",a[i][j]);printf("\n");}}7.8#define N 10#define M 10main(){int i,j,k,m,n,flag1,flag2,a[N][M],max,maxi,maxj; scanf("%d,%d",&n,&m);for(i=0;i<n;i++)for(j=0;j<m;j++)scanf("%d",&a[i][j]);flag2=0;for(i=0;i<n;i++){max=a[i][0];for(j=0;j<m;j++)if(max<a[i][j]){max=a[i][j];maxj=j;}for(k=0,flag1=1;k<n&&flag1;k++)if(max>a[k][maxj])flag1=0;if(flag1){ printf("\na[%d][%d]=%d\n",i,maxj,max);flag2=1;}}if(!flag2) printf("NOT");}7.9#include<stdio.h>#define N 15main(){int i,j,number,top,bott,min,loca,a[N],flag;char c;for(i=0;i<=N;i++)scanf("%d",&a[i]);flag=1;while(flag){scanf("%d",&number);loca=0;top=0;bott=N-1;if((number<a[0])||(number>a[N-1]))loca=-1;while((loca==0)&&(top<=bott)){min=(bott+top)/2;if(number==a[min]){loca=min;printf("number=%d,loca=%d\n",number,loca+1);}else if(number<a[min])bott=min-1;elsetop=min+1;}if(loca==0||loca==-1)printf("%d not in table\n",number);printf("continue Y/N or y/n\n");c=getchar();if(c=='N'||c=='n')flag=0;}}7.10main(){int i,j,uppn,lown,dign,span,othn;char text[3][80];uppn=lown=dign=span=othn=0;for(i=0;i<3;i++){gets(text[i]);for(j=0;j<80&&text[i][j]!='\0';j++){if(text[i][j]>='A'&&text[i][j]<='Z')uppn++;else if(text[i][j]>='a'&&text[i][j]<='z')lown++;else if(text[i][j]>='0'&&text[i][j]<='9')dign++;else if(text[i][j]==' ')span++;elseothn++;}}for(i=0;i<3;i++)printf("%s\n",text[i]);printf("uppn=%d\n",uppn);printf("lown=%d\n",lown);printf("dign=%d\n",dign);printf("span=%d\n",span);printf("othn=%d\n",othn);}7.11main(){static char a[5]={'*','*','*','*','*'};int i,j,k;char space=' ';for(i=0;i<=5;i++){printf("\n");for(j=1;j<=3*i;j++)printf("%1c",space);for(k=0;k<=5;k++)printf("%3c",a[k]);}}7.12#include<stdio.h>main(){int i,n;char ch[80],tran[80];gets(ch);i=0;while(ch[i]!='\0'){if((ch[i]>='A')&&(ch[i]<='Z'))tran[i]=26+64-ch[i]+1+64;else if((ch[i]>='a')&&(ch[i]<='z'))tran[i]=26+96-ch[i]+1+96;elsetran[i]=ch[i];i++;n=i;for(i=0;i<n;i++)putchar(tran[i]);}7.13main(){char s1[80],s2[40];int i=0,j=0;scanf("%s",s1);scanf("%s",s2);while(s1[i]!='\0')i++;while(s2[j]!='\0')s1[i++]=s2[j++];s1[i]='\0';printf("s=%s\n",s1);}7.14#include<stdio.h>main(){int i,resu;char s1[100],s2[100];gets(s1);gets(s2);i=0;while((s1[i]==s2[i])&&(s1[i]!='\0'))i++;if(s1[i]=='\0'&&s2[i]=='\0')resu=0;elseresu=s1[i]-s2[i];printf("s1=%s,s2=%s,resu=%d\n",s1,s2,resu); }7.15#include"stdio.h"main(){char from[80],to[80];;int i;scanf("%s",from);for(i=0;i<=strlen(from);i++)to[i]=from[i];printf("%s\n",to);}8.1hcf(u,v)int u,v;{int a,b,t,r;if(u>v){t=u;u=v;v=t;}a=u;b=v;while((r=b%a)!=0){b=a;a=r;}return(a);}lcd(u,v,h)int u,v,h;{return(u*v/h);}main(){int u,v,h,l;scanf("%d,%d",&u,&v);h=hcf(u,v);printf("H.C.F=%d\n",h);l=lcd(u,v,h);printf("L.C.D=%d\n",l);}8.2#include"math.h"float x1,x2,disc,p,q;greater_than_zero(a,b)float a,b;{x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);}equal_to_zero(a,b)flaot a,b;{x1=x2=-b/(2*a);}smaller_than_zero(a,b)float a,b;{p=-b/(2*a);q=sqrt(-disc)/(2*a);}main(){float a,b,c;scanf("%f,%f,%f",&a,&b,&c); disc=b*b-4*a*c;if(fabs(disc)<=1e-5){equal_to_zero(a,b);printf("x1=%5.2f\tx2=%5.2f\n",x1,x2);}else if(disc>0){greater_than_zero(a,b);printf("x1=%5.2f\tx2=%5.2f\n",x1,x2);}else{smaller_than_zero(a,b);printf("x1=%5.2f+%5.2fi\tx2=%5.2f-%5.2fi\n",p,q,p,q);}}8.3main(){int number;scanf("%d",&number);if(prime(number))printf("yes");elseprintf("no");}int prime(number)int number;{int flag=1,n;for(n=2;n<number/2&&flag==1;n++)if(number%n==0)flag=0;return(flag);}8.4#define N 3int array[N][N];convert(array)int array[3][3];{int i,j,t;for(i=0;i<N;i++)for(j=i+1;j<N;j++){t=array[i][j];array[i][j]=array[j][i];array[j][i]=t;}}main(){int i,j;for(i=0;i<N;i++)for(j=0;j<N;j++)scanf("%d",&array[i][j]);convert(array);for(i=0;i<N;i++){printf("\n");for(j=0;j<N;j++)printf("%5d",array[i][j]);}}8.5main(){char str[100];scanf("%s",str);inverse(str);printf("%s\n",str);}inverse(str)char str[];{char t;int i,j;for(i=0,j=strlen(str);i<strlen(str)/2;i++,j--) {t=str[i];str[i]=str[j-1];str[j-1]=t;}}8.6char concate(str1,str2,str)char str1[],str2[],str[];{int i,j;for(i=0;str1[i]!='\0';i++)str[i]=str1[i];for(j=0;str2[j]!='\0';j++)str[i+j]=str2[j];str[i+j]='\0';}main(){char s1[100],s2[100],s[100];scanf("%s",s1);scanf("%s",s2);concate(s1,s2,s);printf("\ns=%s",s);}8.7main(){char str[80],c[80];void cpy();gets(str);cpy(str,c);printf("\n%s\n",c);}void cpy(s,c)char s[],c[];{int i,j;for(i=0,j=0;s[i]!='\0';i++)if(s[i]=='a'||s[i]=='A'||s[i]=='e'||s[i]=='E'||s[i]=='i'|| s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||s[i]=='U'){c[j]=s[i];j++;}c[j]='\0';}8.8main(){char str[80];scanf("%s",str);insert(str);}insert(str)char str[];{int i;for(i=strlen(str);i>0;i--){str[i*2]=str[i];str[i*2-1]=' ';}printf("%s\n",str);}8.9int alph,digit,space,others;main(){char text[80];gets(text);alph=0,digit=0,space=0,others=0;count(text);printf("\nalph=%d,digit=%d,space=%d,others=%d\n",alph,digit,space,others); }count(str)char str[];{int i;for(i=0;str[i]!='\0';i++)if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))alph++;else if(str[i]>='0'&&str[i]<='9')digit++;else if(strcmp(str[i],' ')==0)space++;elseothers++;}8.10int alph(c)char c;{if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))return(1);elsereturn(0);}int longest(string)char string[];{int len=0,i,length=0,flag=1,place,point;for(i=0;i<=strlen(string);i++)if(alph(string[i]))if(flag){point=i;flag=0;}elselen++;else{flag=1;if(len>length){length=len;place=point;len=0;}return(place);}main(){int i;char line[100];gets(line);for(i=longest(line);alph(line[i]);i++) printf("%c",line[i]);printf("\n");}8.11#define N 10char str[N];main(){int i,flag;for(flag=1;flag==1;){scanf("%s",str);if(strlen(str)>N)printf("input error");elseflag=0;}sort(str);for(i=0;i<N;i++)printf("%c",str[i]);}sort(str)char str[N];{int i,j;char t;for(j=1;j<N;j++)for(i=0;(i<N-j)&&(str[i]!='\0');i++)if(str[i]>str[i+1]){t=str[i];str[i]=str[i+1];str[i+1]=t;}}8.12#include<math.h>float solut(a,b,c,d){float x=1,x0,f,f1;do{x0=x;f=((a*x0+b)*x0+c)*x0+d;f1=(3*a*x0+2*b)*x0+c;x=x0-f/f1;}while(fabs(x-x0)>=1e-5);return(x);}main(){float a,b,c,d;scanf("%f,%f,%f,%f",&a,&b,&c,&d);printf("x=%10.7f\n",solut(a,b,c,d));}8.13main(){int x,n;float p();scanf("%d,%d",&n,&x);printf("P%d(%d)=%10.2f\n",n,x,p(n,x));}float p(tn,tx)int tn,tx;{if(tn==0)return(1);else if(tn==1)return(tx);elsereturn(((2*tn-1)*tx*p((tn-1),tx)-(tn-1)*p((tn-2),tx))/tn); }8.14#define N 10#define M 5float score[N][M];float a_stu[N],a_cor[M];main(){int i,j,r,c;float h;float s_diff();float highest();r=0;c=1;input_stu();avr_stu();avr_cor();printf("\n number class 1 2 3 4 5 avr");for(i=0;i<N;i++){printf("\nNO%2d",i+1);for(j=0;j<M;j++)printf("%8.2f",score[i][j]);printf("%8.2f",a_stu[i]);}printf("\nclassavr");for(j=0;j<M;j++)printf("%8.2f",a_cor[j]);h=highest(&r,&c);printf("\n\n%8.2f %d %d\n",h,r,c); printf("\n %8.2f\n",s_diff());}input_stu(){int i,j;float x;for(i=0;i<N;i++){for(j=0;j<M;j++){scanf("%f",&x);score[i][j]=x;}}}avr_stu(){int i,j;float s;for(i=0;i<N;i++){for(j=0,s=0;j<M;j++)s+=score[i][j];a_stu[i]=s/5.0;}}avr_cor(){int i,j;float s;for(j=0;j<M;j++){for(i=0,s=0;i<N;i++)s+=score[i][j];a_cor[j]=s/(float)N;}}float highest(r,c)int *r,*c;{float high;int i,j;high=score[0][0];for(i=0;i<N;i++)for(j=0;j<M;j++)if(score[i][j]>high){high=score[i][j];*r=i+1;*c=j+1;}return(high);}float s_diff(){int i,j;float sumx=0.0,sumxn=0.0;for(i=0;i<N;i++){sumx+=a_stu[i]*a_stu[i];sumxn+=a_stu[i];}return(sumx/N-(sumxn/N)*(sumxn/N)); }8.15#include<stdio.h>#define N 10void input_e(num,name)int num[];char name[N][8];{int i;for(i=0;i<N;i++){scanf("%d",&num[i]);gets(name[i]);}}void sort(num,name)int num[];char name[N][8];{int i,j,min,temp1;char temp2[8];for(i=0;i<N-1;i++){min=i;for(j=i;j<N;j++)if(num[min]>num[j])min=j;temp1=num[i];num[i]=num[min];num[min]=temp1;strcpy(temp2,name[i]);strcpy(name[i],name[min]);strcpy(name[min],temp2);}for(i=0;i<N;i++)printf("\n%5d%10s",num[i],name[i]);}void search(n,num,name)int n,num[];char name[N][8];{int top,bott,min,loca;loca=0;top=0;bott=N-1;if((n<num[0])||(n>num[N-1]))loca=-1;while((loca==0)&&(top<=bott)){min=(bott+top)/2;if(n==num[min]){loca=min;printf("number=%d,name=%s\n",n,name[loca]);}else if(n<num[min])bott=min-1;elsetop=min+1;}if(loca==0||loca==-1)printf("number=%d is not in table\n",n);}main(){int num[N],number,flag,c,n;char name[N][8];input_e(num,name);sort(num,name);for(flag=1;flag;){scanf("%d",&number);search(number,num,name);printf("continue?Y/N!");c=getchar();if(c=='N'||c=='n')flag=0;}}8.16#include<stdio.h>#define MAX 1000main(){int c,i,flag,flag1;char t[MAX];i=0;flag=0;flag1=1;while((c=getchar())!='\0'&&i<MAX&&flag1){if(c>='0'&&c<='9'||c>='A'&&c<='F'||c>='a'&&c<='f') {flag=1;t[i++]=c;}else if(flag){t[i]='\0';printf("\nnumber=%d\n",htoi(t));printf("continue?");c=getchar();if(c=='n'||c=='N')flag1=0;else{flag=0;i=0;}}}}htoi(s)char s[];{int i,n;n=0;for(i=0;s[i]!='\0';i++){if(s[i]>='0'&&s[i]<='9')n=n*16+s[i]-'0';if(s[i]>='a'&&s[i]<='f')。

C语言程序设计 第三版 部分习题 (谭浩强 著) 清华大学出版社 课后答案

C语言程序设计 第三版 部分习题 (谭浩强 著) 清华大学出版社 课后答案

【习题1】一、简答题(在课本中寻找答案,略)1.1 C程序的基本结构包括哪些内容?1.2 如何定义标识符?1.3 输入格式、输出格式的组成包括哪些内容?1.4 C语言函数分为哪两类?1.5 计算表达式的值应该考虑哪些方面?1.6 上机调试程序的步骤如何?二、判断并改错1.7 C程序执行的入口是main()函数,所以main函数必须放在程序的开头。

错误:main函数可以放在程序的任何位置。

并不限定在程序的开头。

1.8 定义一个函数包括数据说明部分和执行语句部分,两者可以交叉出现。

错误:不可以交叉出现,数据说明部分在执行语句部分的前面。

1.9 编辑与编译不是一回事。

错误:不是一回事。

编辑完成源程序的输入和修改;编译是对源程序进行语法检查,如果无语法错误,则生成目标程序。

1.10 scanf("%d,%d",&x,&y);的正确输入格式:3,4<回车>。

正确1.11 注释内容太多会影响程序的执行效率。

错误:不会影响程序的执行效率。

因为在编译时,编译系统将注释内容删除或用空格代替,所以注释内容不会编译到目标程序中。

1.12 所有的数学公式可以直接出现在源程序中。

错误:数学公式需要转换成C语言能够接受的公式才能出现在源程序中。

三、编程题1.13 在屏幕上输出自己名字的拼音。

提示:中文名字叫“张三”,对应的拼音为“Zhang San”,输出用printf()函数。

1.14 输入圆的半径,求圆的周长,并将结果保留两位小数输出到屏幕上。

提示:定义圆的半径r,圆的周长:2*3.14*r,输出结果保留2位小数可以用%.2f1.15 输入两个整数,输出其中最大者。

提示:模仿例1.31.16 输入三个整数,输出其中最小者。

提示: min(min(a,b),c);【习题2】一、简答题(在课本中寻找答案,略)2.1 常量与变量的区别?2.2 C语言中的标识符的定义规则?2.3 变量为什么要“先定义,后使用”?2.4 字符常量'a'与字符串常量"a"有何区别?2.5 大小写字母转换的规则是什么?2.6 变量的具有哪三种属性?二、单项选择题2.7 C语言中,int型数据在内存中的存储形式是(D)。

(完整版)《C语言程序设计》课后习题答案(第四版)谭浩强

(完整版)《C语言程序设计》课后习题答案(第四版)谭浩强

第1章程序设计和C语言11.1什么是计算机程序11.2什么是计算机语言11.3C语言的发展及其特点31。

4最简单的C语言程序51.4。

1最简单的C语言程序举例61.4.2C语言程序的结构101.5运行C程序的步骤和方法121.6程序设计的任务141—5 #include 〈stdio.h〉int main ( ){printf (”**************************\n\n"); printf(" Very Good!\n\n");p rintf (”**************************\n");return 0;}1—6#include 〈stdio.h〉int main(){int a,b,c,max;printf("please input a,b,c:\n”);scanf(”%d,%d,%d”,&a,&b,&c);max=a;if (max〈b)max=b;if (max〈c)max=c;printf(”The largest number is %d\n”,max);return 0;}第2章算法——程序的灵魂162.1什么是算法162.2简单的算法举例172。

3算法的特性212。

4怎样表示一个算法222。

4。

1用自然语言表示算法222。

4。

2用流程图表示算法222。

4。

3三种基本结构和改进的流程图262。

4.4用N S流程图表示算法282。

4.5用伪代码表示算法312。

4.6用计算机语言表示算法322.5结构化程序设计方法34习题36第章最简单的C程序设计——顺序程序设计373。

1顺序程序设计举例373.2数据的表现形式及其运算393.2。

1常量和变量393.2.2数据类型423.2.3整型数据443。

2。

4字符型数据473。

2.5浮点型数据493。

2。

6怎样确定常量的类型513。

2.7运算符和表达式523。

C程序设计谭浩强_课后习题答案_共11章

C程序设计谭浩强_课后习题答案_共11章

1-5 #include <>int main ( ){ printf ("**************************\n\n"); printf(" Very Good!\n\n");printf ("**************************\n"); return 0;}1-6#include <>int main(){int a,b,c,max;printf("please input a,b,c:\n");scanf("%d,%d,%d",&a,&b,&c);max=a;if (max<b)max=b;if (max<c)max=c;printf("The largest number is %d\n",max);return 0;}3-1 #include <>#include <>int main(){float p,r,n;r=;n=10;p=pow(1+r,n);printf("p=%f\n",p);return 0;}3-2-1#include <>#include <>int main(){float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;p=1000;r5=;r3=;r2=;r1=;r0=;p1=p*((1+r5)*5); #include <>#include <>int main(){float d=300000,p=6000,r=,m;m=log10(p/(p-d*r))/log10(1+r);printf("m=%\n",m);return 0;}3-4#include <>int main(){int c1,c2;c1=197;c2=198;printf("c1=%c,c2=%c\n",c1,c2);printf("c1=%d,c2=%d\n",c1,c2);return 0;}3-5#include <>int main(){int a,b;float x,y;char c1,c2;scanf("a=%d b=%d",&a,&b);scanf("%f %e",&x,&y);scanf("%c%c",&c1,&c2);printf("a=%d,b=%d,x=%f,y=%f,c1=%c,c2=%c\n",a,b,x,y,c1,c2); return 0;}3-6#include <>int main(){char c1='C',c2='h',c3='i',c4='n',c5='a';c1=c1+4;c2=c2+4;c3=c3+4;c4=c4+4;c5=c5+4;printf("passwor is %c%c%c%c%c\n",c1,c2,c3,c4,c5);return 0;}3-7#include <>int main (){float h,r,l,s,sq,vq,vz;float pi=;printf("请输入圆半径r,圆柱高h∶");scanf("%f,%f",&r,&h); #include <> int main(){ int x,y;printf("输入x:");scanf("%d",&x);if(x<1) /* x<1 */{ y=x;printf("x=%3d, y=x=%d\n" ,x,y);}else if(x<10) /* 1=<x<10 */{ y=2*x-1;printf("x=%d, y=2*x-1=%d\n",x,y);}else /* x>=10 */{ y=3*x-11;printf("x=%d, y=3*x-11=%d\n",x,y);}return 0;}4-8#include <>int main(){ float score;char grade;printf("请输入学生成绩:");scanf("%f",&score);while (score>100||score<0){printf("\n 输入有误,请重输");scanf("%f",&score);}switch((int)(score/10)){case 10:case 9: grade='A';break;case 8: grade='B';break;case 7: grade='C';break;case 6: grade='D';break;case 5:case 4:case 3:case 2:case 1:case 0: grade='E';}printf("成绩是 %,相应的等级是%c\n ",score,grade);return 0;}4-10-1#include <>int main(){int i;double bonus,bon1,bon2,bon4,bon6,bon10;bon1=100000*;bon2=bon1+100000*;bon4=bon2+100000*;bon6=bon4+100000*;bon10=bon6+400000*;printf("请输入利润i:");scanf("%d",&i);if (i<=100000)bonus=i*;else if (i<=200000)bonus=bon1+(i-100000)*;else if (i<=400000)bonus=bon2+(i-200000)*;else if (i<=600000)bonus=bon4+(i-400000)*;else if (i<=1000000)bonus=bon6+(i-600000)*;elsebonus=bon10+(i-1000000)*;printf("奖金是: %\n",bonus);return 0;}4-10-2#include <>int main(){int i;double bonus,bon1,bon2,bon4,bon6,bon10;int branch;bon1=100000*;bon2=bon1+100000*;bon4=bon2+200000*;bon6=bon4+200000*;bon10=bon6+400000*;printf("请输入利润i:");scanf("%d",&i);branch=i/100000;if (branch>10) branch=10;switch(branch){ case 0:bonus=i*;break;case 1:bonus=bon1+(i-100000)*;break; case 2:case 3: bonus=bon2+(i-200000)*;break; case 4:case 5: bonus=bon4+(i-400000)*;break; case 6:case 7:case 8:case 9: bonus=bon6+(i-600000)*;break; case 10: bonus=bon10+(i-1000000)*;}printf("奖金是 %\n",bonus);return 0;}4-11#include <>int main(){int t,a,b,c,d;printf("请输入四个数:");scanf("%d,%d,%d,%d",&a,&b,&c,&d);printf("a=%d,b=%d,c=%d,d=%d\n",a,b,c,d); if (a>b){ t=a;a=b;b=t;}if (a>c){ t=a;a=c;c=t;}if (a>d){ t=a;a=d;d=t;}if (b>c){ t=b;b=c;c=t;}if (b>d){ t=b;b=d;d=t;}if (c>d){ t=c;c=d;d=t;}printf("排序结果如下: \n");printf("%d %d %d %d \n" ,a,b,c,d); return 0;}4-12#include <>int main(){int h=10;float x1=2,y1=2,x2=-2,y2=2,x3=-2,y3=-2,x4=2,y4=-2,x,y,d1,d2,d3,d4; printf("请输入一个点(x,y):");scanf("%f,%f",&x,&y);d1=(x-x4)*(x-x4)+(y-y4)*(y-y4); /*求该点到各中心点距离*/ d2=(x-x1)*(x-x1)+(y-y1)*(y-y1);d3=(x-x2)*(x-x2)+(y-y2)*(y-y2);d4=(x-x3)*(x-x3)+(y-y3)*(y-y3);if (d1>1 && d2>1 && d3>1 && d4>1) h=0; /*判断该点是否在塔外*/ printf("该点高度为 %d\n",h);return 0;}5-3#include <>int main(){int p,r,n,m,temp;printf("请输入两个正整数n,m:");scanf("%d,%d,",&n,&m);if (n<m){temp=n;n=m;m=temp;}p=n*m;while(m!=0){r=n%m;n=m;m=r;}printf("它们的最大公约数为:%d\n",n);printf("它们的最小公约数为:%d\n",p/n);return 0;}5-4#include <>int main(){char c;int letters=0,space=0,digit=0,other=0;printf("请输入一行字符:\n");while((c=getchar())!='\n'){if (c>='a' && c<='z' || c>='A' && c<='Z')letters++;else if (c==' ')space++;else if (c>='0' && c<='9')digit++;elseother++;}printf("字母数:%d\n空格数:%d\n数字数:%d\n其它字符数:%d\n",letters,space,digit,other);return 0;}5-5#include <>int main(){int a,n,i=1,sn=0,tn=0;printf("a,n=:");scanf("%d,%d",&a,&n);while (i<=n){tn=tn+a; /*赋值后的tn为i个 a组成数的值*/sn=sn+tn; /*赋值后的sn为多项式前i项之和*/a=a*10;++i;}printf("a+aa+aaa+...=%d\n",sn);return 0;}5-6#include <>int main(){double s=0,t=1;int n;for (n=1;n<=20;n++){t=t*n;s=s+t;}printf("1!+2!+...+20!=%\n",s);return 0;}5-7#include <>int main(){int n1=100,n2=50,n3=10;double k,s1=0,s2=0,s3=0;for (k=1;k<=n1;k++) /*计算1到100的和*/{s1=s1+k;}for (k=1;k<=n2;k++) /*计算1到50各数的平方和*/ {s2=s2+k*k;}for (k=1;k<=n3;k++) /*计算1到10的各倒数和*/ {s3=s3+1/k;}printf("sum=%\n",s1+s2+s3);return 0;}5-8#include <>int main(){int i,j,k,n;printf("parcissus numbers are ");for (n=100;n<1000;n++){i=n/100;j=n/10-i*10;k=n%10;if (n==i*i*i + j*j*j + k*k*k)printf("%d ",n);}printf("\n");return 0;}5-9#include <>int main(){int m,s,i;for (m=2;m<1000;m++){s=0;for (i=1;i<m;i++)if ((m%i)==0) s=s+i;if(s==m){printf("%d,its factors are ",m);for (i=1;i<m;i++)if (m%i==0) printf("%d ",i);printf("\n");}}return 0;}5-10#include <>int main(){int i,n=20;double a=2,b=1,s=0,t;for (i=1;i<=n;i++){s=s+a/b;t=a,a=a+b,b=t;}printf("sum=%\n",s);return 0;}5-11#include <>int main(){double sn=100,hn=sn/2;int n;for (n=2;n<=10;n++){sn=sn+2*hn; /*第n次落地时共经过的米数*/ hn=hn/2; /*第n次反跳高度*/}printf("第10次落地时共经过%f米\n",sn);printf("第10次反弹%f米\n",hn);return 0;}5-12#include <>int main(){int day,x1,x2;day=9;x2=1;while(day>0){x1=(x2+1)*2; /*第1天的桃子数是第2天桃子数加1后的2倍.*/ x2=x1;day--;}printf("total=%d\n",x1);return 0;}5-13#include <>#include <>int main(){float a,x0,x1;printf("enter a positive number:");scanf("%f",&a);x0=a/2;x1=(x0+a/x0)/2;do{x0=x1;x1=(x0+a/x0)/2;}while(fabs(x0-x1)>=1e-5);printf("The square root of % is %\n",a,x1);return 0;}5-14#include <>#include <>int main(){double x1,x0,f,f1;x1=;do{x0=x1;f=((2*x0-4)*x0+3)*x0-6;f1=(6*x0-8)*x0+3;x1=x0-f/f1;}while(fabs(x1-x0)>=1e-5);printf("The root of equation is %\n",x1);return 0;}5-15#include <>#include <>int main(){float x0,x1,x2,fx0,fx1,fx2; do{printf("enter x1 & x2:"); scanf("%f,%f",&x1,&x2); fx1=x1*((2*x1-4)*x1+3)-6; fx2=x2*((2*x2-4)*x2+3)-6; }while(fx1*fx2>0);do{x0=(x1+x2)/2;fx0=x0*((2*x0-4)*x0+3)-6; if ((fx0*fx1)<0){x2=x0;fx2=fx0;}else{x1=x0;fx1=fx0;}}while(fabs (fx0)>=1e-5); printf("x=%\n",x0);return 0;}5-16#include <>int main(){int i,j,k;for (i=0;i<=3;i++){for (j=0;j<=2-i;j++)printf(" ");for (k=0;k<=2*i;k++)printf("*");printf("\n");}for (i=0;i<=2;i++){for (j=0;j<=i;j++)printf(" ");for (k=0;k<=4-2*i;k++)printf("*");printf("\n");}return 0;}5-17#include <>int main(){char i,j,k; /*是a的对手;j是b的对手;k是c的对手*/ for (i='x';i<='z';i++)for (j='x';j<='z';j++)if (i!=j)for (k='x';k<='z';k++)if (i!=k && j!=k)if (i!='x' && k!='x' && k!='z')printf("A--%c\nB--%c\nC--%c\n",i,j,k);return 0;}6-1#include <>#include <>int main(){int i,j,n,a[101];for (i=1;i<=100;i++)a[i]=i;a[1]=0;for (i=2;i<sqrt(100);i++)for (j=i+1;j<=100;j++){if(a[i]!=0 && a[j]!=0)if (a[j]%a[i]==0)a[j]=0;}printf("\n");for (i=2,n=0;i<=100;i++){ if(a[i]!=0){printf("%5d",a[i]);n++;}if(n==10){printf("\n");n=0;}}printf("\n");return 0;}6-2#include <>int main(){int i,j,min,temp,a[11];printf("enter data:\n");for (i=1;i<=10;i++){printf("a[%d]=",i);scanf("%d",&a[i]);}printf("\n");printf("The orginal numbers:\n"); for (i=1;i<=10;i++)printf("%5d",a[i]);printf("\n");for (i=1;i<=9;i++){min=i;for (j=i+1;j<=10;j++)if (a[min]>a[j]) min=j;temp=a[i];a[i]=a[min];a[min]=temp;}printf("\nThe sorted numbers:\n"); for (i=1;i<=10;i++)printf("%5d",a[i]);printf("\n");return 0;}6-3#include <>int main(){int a[3][3],sum=0;int i,j;printf("enter data:\n");for (i=0;i<3;i++)for (j=0;j<3;j++)scanf("%3d",&a[i][j]);for (i=0;i<3;i++)sum=sum+a[i][i];printf("sum=%6d\n",sum);return 0;}6-4#include <>int main(){ int a[11]={1,4,6,9,13,16,19,28,40,100};int temp1,temp2,number,end,i,j;printf("array a:\n");for (i=0;i<10;i++)printf("%5d",a[i]);printf("\n");printf("insert data:");scanf("%d",&number);end=a[9];if (number>end)a[10]=number;else{for (i=0;i<10;i++){if (a[i]>number){temp1=a[i];a[i]=number;for (j=i+1;j<11;j++){temp2=a[j];a[j]=temp1;temp1=temp2;}break;}}}printf("Now array a:\n");for (i=0;i<11;i++)printf("%5d",a[i]);printf("\n");return 0;}6-5#include <>#define N 5int main(){ int a[N],i,temp;printf("enter array a:\n");for (i=0;i<N;i++)scanf("%d",&a[i]);printf("array a:\n");for (i=0;i<N;i++)printf("%4d",a[i]);for (i=0;i<N/2;i++) n",number);;printf("continu or not(Y/N)");scanf(" %c",&c);if (c=='N'||c=='n')flag=0;}return 0;}6-10#include <>int main(){int i,j,upp,low,dig,spa,oth;char text[3][80];upp=low=dig=spa=oth=0;for (i=0;i<3;i++){ printf("please input line %d:\n",i+1);gets(text[i]);for (j=0;j<80 && text[i][j]!='\0';j++){if (text[i][j]>='A'&& text[i][j]<='Z') upp++;else if (text[i][j]>='a' && text[i][j]<='z') low++;else if (text[i][j]>='0' && text[i][j]<='9') dig++;else if (text[i][j]==' ')spa++;elseoth++;}}printf("\nupper case: %d\n",upp);printf("lower case: %d\n",low);printf("digit : %d\n",dig);printf("space : %d\n",spa);printf("other : %d\n",oth);return 0;}6-11#include <>int main(){ char a[5]={'*','*','*','*','*'};int i,j,k;char space=' ';for (i=0;i<5;i++){ printf("\n");printf(" ");for (j=1;j<=i;j++)printf("%c",space);for (k=0;k<5;k++)printf("%c",a[k]);}printf("\n");return 0;}6-12a-c#include <>int main(){ int j,n;char ch[80],tran[80];printf("input cipher code:");gets(ch);printf("\ncipher code :%s",ch);j=0;while (ch[j]!='\0'){ if ((ch[j]>='A') && (ch[j]<='Z'))tran[j]=155-ch[j];else if ((ch[j]>='a') && (ch[j]<='z')) tran[j]=219-ch[j];elsetran[j]=ch[j];j++;}n=j;printf("\noriginal text:");for (j=0;j<n;j++)putchar(tran[j]);printf("\n");return 0;}6-12b#include <>int main(){int j,n;char ch[80];printf("input cipher code:\n");gets(ch);printf("\ncipher code:%s\n",ch);j=0;while (ch[j]!='\0'){ if ((ch[j]>='A') && (ch[j]<='Z'))ch[j]=155-ch[j];else if ((ch[j]>='a') && (ch[j]<='z'))ch[j]=219-ch[j];elsech[j]=ch[j];j++;}n=j;printf("original text:");for (j=0;j<n;j++)putchar(ch[j]);printf("\n");return 0;}6-13#include <>int main(){ char s1[80],s2[40];int i=0,j=0;printf("input string1:");scanf("%s",s1);printf("input string2:");scanf("%s",s2);while (s1[i]!='\0')i++;while(s2[j]!='\0')s1[i++]=s2[j++];s1[i]='\0';printf("\nThe new string is:%s\n",s1);return 0;}6-14#include <>int main(){ int i,resu;char s1[100],s2[100];printf("input string1:");gets(s1);printf("\ninput string2:");gets(s2);i=0;while ((s1[i]==s2[i]) && (s1[i]!='\0'))i++; if (s1[i]=='\0' && s2[i]=='\0')resu=0;elseresu=s1[i]-s2[i];printf("\nresult:%d.\n",resu); return 0;}6-15#include <>#include <>int main(){ char s1[80],s2[80];int i;printf("input s2:");scanf("%s",s2);for (i=0;i<=strlen(s2);i++)s1[i]=s2[i];printf("s1:%s\n",s1);return 0;}7-1-1#include <>int main(){int hcf(int,int);int lcd(int,int,int);int u,v,h,l;scanf("%d,%d",&u,&v);h=hcf(u,v);printf("",h);l=lcd(u,v,h);printf("",l);return 0;}int hcf(int u,int v){int t,r;if (v>u){t=u;u=v;v=t;}while ((r=u%v)!=0){u=v;v=r;}return(v);}int lcd(int u,int v,int h){return(u*v/h);}7-1-2#include <>int Hcf,Lcd;int main(){void hcf(int,int);void lcd(int,int);int u,v;scanf("%d,%d",&u,&v);hcf(u,v);lcd(u,v);printf("",Hcf);printf("",Lcd);return 0;}void hcf(int u,int v){int t,r;if (v>u){t=u;u=v;v=t;}while ((r=u%v)!=0){u=v;v=r;}Hcf=v;}void lcd(int u,int v){Lcd=u*v/Hcf;}7-2#include <>#include <>float x1,x2,disc,p,q;int main(){void greater_than_zero(float,float);void equal_to_zero(float,float);void smaller_than_zero(float,float);float a,b,c;printf("input a,b,c:");scanf("%f,%f,%f",&a,&b,&c);printf("equation: %*x*x+%*x+%=0\n",a,b,c);disc=b*b-4*a*c;printf("root:\n");if (disc>0){greater_than_zero(a,b);printf("x1=%f\t\tx2=%f\n",x1,x2);}else if (disc==0){equal_to_zero(a,b);printf("x1=%f\t\tx2=%f\n",x1,x2);}else{smaller_than_zero(a,b);printf("x1=%f+%fi\tx2=%f-%fi\n",p,q,p,q); }return 0;}void greater_than_zero(float a,float b){x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);}void equal_to_zero(float a,float b){x1=x2=(-b)/(2*a);}void smaller_than_zero(float a,float b){p=-b/(2*a);q=sqrt(-disc)/(2*a);}7-3#include <>int main(){int prime(int);int n;printf("input an integer:");scanf("%d",&n);if (prime(n))printf("%d is a prime.\n",n);elseprintf("%d is not a prime.\n",n);return 0;}int prime(int n){int flag=1,i;for (i=2;i<n/2 && flag==1;i++) if (n%i==0)flag=0;return(flag);}7-4#include <>#define N 3int array[N][N];int main(){ void convert(int array[][3]); int i,j;printf("input array:\n");for (i=0;i<N;i++)for (j=0;j<N;j++)scanf("%d",&array[i][j]);printf("\noriginal array :\n"); for (i=0;i<N;i++){for (j=0;j<N;j++)printf("%5d",array[i][j]);printf("\n");}convert(array);printf("convert array:\n");for (i=0;i<N;i++){for (j=0;j<N;j++)printf("%5d",array[i][j]); printf("\n");}return 0;}void convert(int array[][3]){int i,j,t;for (i=0;i<N;i++)for (j=i+1;j<N;j++){t=array[i][j];array[i][j]=array[j][i];array[j][i]=t;}}#include <>#include <>int main(){void inverse(char str[]);char str[100];printf("input string:");scanf("%s",str);inverse(str);printf("inverse string:%s\n",str);return 0;}void inverse(char str[]){char t;int i,j;for (i=0,j=strlen(str);i<(strlen(str)/2);i++,j--){t=str[i];str[i]=str[j-1];str[j-1]=t;}}7-6#include <>int main(){void concatenate(char string1[],char string2[],char string[]); char s1[100],s2[100],s[100];printf("input string1:");scanf("%s",s1);printf("input string2:");scanf("%s",s2);concatenate(s1,s2,s);printf("\nThe new string is %s\n",s);return 0;}void concatenate(char string1[],char string2[],char string[]) {int i,j;for (i=0;string1[i]!='\0';i++)string[i]=string1[i];for(j=0;string2[j]!='\0';j++)string[i+j]=string2[j];string[i+j]='\0';}7-7#include <>int main(){void cpy(char [],char []);char str[80],c[80];printf("input string:");gets(str);cpy(str,c);printf("The vowel letters are:%s\n",c);return 0;}void cpy(char s[],char c[]){ int i,j;for (i=0,j=0;s[i]!='\0';i++)if (s[i]=='a'||s[i]=='A'||s[i]=='e'||s[i]=='E'||s[i]=='i'|| s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||s[i]=='U'){c[j]=s[i];j++;}c[j]='\0';}7-8#include <>#include <>int main(){char str[80];void insert(char []);printf("input four digits:");scanf("%s",str);insert(str);return 0;}void insert(char str[]){int i;for (i=strlen(str);i>0;i--){str[2*i]=str[i];str[2*i-1]=' ';}printf("output:\n%s\n",str);}7-9#include <>int letter,digit,space,others;int main(){void count(char []);char text[80];printf("input string:\n");gets(text);printf("string:");puts(text);letter=0;digit=0;space=0;others=0;count(text);printf("\nletter:%d\ndigit:%d\nspace:%d\nothers:%d\n",letter,digit,space,others );return 0;}void count(char str[]){int i;for (i=0;str[i]!='\0';i++)if ((str[i]>='a'&& str[i]<='z')||(str[i]>='A' && str[i]<='Z'))letter++;else if (str[i]>='0' && str [i]<='9')digit++;else if (str[i]==32)space++;elseothers++;}7-10#include <>#include <>int main(){int alphabetic(char);int longest(char []);int i;char line[100];printf("input one line:\n");gets(line);printf("The longest word is :");for (i=longest(line);alphabetic(line[i]);i++) printf("%c",line[i]);printf("\n");return 0;}int alphabetic(char c){if ((c>='a' && c<='z')||(c>='A'&&c<='z'))return(1);elsereturn(0);}int longest(char string[]){int len=0,i,length=0,flag=1,place=0,point;for (i=0;i<=strlen(string);i++)if (alphabetic(string[i]))if (flag){point=i;flag=0;}elselen++;else{flag=1;if (len>=length){length=len;place=point;len=0;}}return(place);}7-11#include <>#include <>#define N 10char str[N];int main(){void sort(char []);int i,flag;for (flag=1;flag==1;){printf("input string:\n");scanf("%s",&str);if (strlen(str)>N)printf("string too long,input again!"); elseflag=0;}sort(str);printf("string sorted:\n");for (i=0;i<N;i++)printf("%c",str[i]);printf("\n");return 0;}void sort(char str[]){int i,j;char t;for(j=1;j<N;j++)for (i=0;(i<N-j)&&(str[i]!='\0');i++)if(str[i]>str[i+1]){t=str[i];str[i]=str[i+1];str[i+1]=t;}}7-12#include <>#include <>int main(){float solut(float a,float b,float c,float d); float a,b,c,d;printf("input a,b,c,d:");scanf("%f,%f,%f,%f",&a,&b,&c,&d);printf("x=%\n",solut(a,b,c,d));return 0;}float solut(float a,float b,float c,float d) {float x=1,x0,f,f1;do{x0=x;f=((a*x0+b)*x0+c)*x0+d;f1=(3*a*x0+2*b)*x0+c;x=x0-f/f1;}while(fabs(x-x0)>=1e-3);return(x);}7-16#include <>#define MAX 1000int main(){ int htoi(char s[]);int c,i,flag,flag1;char t[MAX];i=0;flag=0;flag1=1;printf("input a HEX number:");while((c=getchar())!='\0' && i<MAX&& flag1){if (c>='0' && c<='9'||c>='a' && c<='f'||c>='A' && c<='F') {flag=1;t[i++]=c;}else if (flag){t[i]='\0';printf("decimal number %d\n",htoi(t));printf("continue or not");c=getchar();if (c=='N'||c=='n')flag1=0;else{flag=0;i=0;printf("\ninput a HEX number:");}}}return 0;}int htoi(char s[]){ int i,n;n=0;for (i=0;s[i]!='\0';i++){if (s[i]>='0'&& s[i]<='9')n=n*16+s[i]-'0';if (s[i]>='a' && s[i]<='f')n=n*16+s[i]-'a'+10;if (s[i]>='A' && s[i]<='F')n=n*16+s[i]-'A'+10;}return(n);}7-17#include <>int main(){ void convert(int n);int number;printf("input an integer: ");scanf("%d",&number);printf("output: ");if (number<0){putchar('-');putchar(' '); /* 先输出一个‘-’号和空格 */ number=-number;}convert(number);printf("\n");return 0;}void convert(int n){ int i;if ((i=n/10)!=0)convert(i);putchar(n%10+'0');putchar(32);}7-18#include <>int main(){int sum_day(int month,int day);int leap(int year);int year,month,day,days;printf("input date(year,month,day):");scanf("%d,%d,%d",&year,&month,&day);printf("%d/%d/%d ",year,month,day);days=sum_day(month,day); /* 调用函数sum_day */ if(leap(year)&&month>=3) /* 调用函数leap */days=days+1;printf("is the %dth day in this year.\n",days);return 0;}int sum_day(int month,int day) /* 函数sum_day:计算日期 */ {int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};int i;for (i=1;i<month;i++)day+=day_tab[i]; /* 累加所在月之前天数 */return(day);} /* 函数leap:判断是否为闰年 */int leap(int year){int leap;leap=year%4==0&&year%100!=0||year%400==0;return(leap);}8-1#include <>int main(){ void swap(int *p1,int *p2);int n1,n2,n3;int *p1,*p2,*p3;printf("input three integer n1,n2,n3:");scanf("%d,%d,%d",&n1,&n2,&n3);p1=&n1;p2=&n2;p3=&n3;if(n1>n2) swap(p1,p2);if(n1>n3) swap(p1,p3);if(n2>n3) swap(p2,p3);printf("Now,the order is:%d,%d,%d\n",n1,n2,n3);return 0;}void swap(int *p1,int *p2){int p;p=*p1; *p1=*p2; *p2=p;}8-2#include <>#include <>int main(){void swap(char *,char *);char str1[20],str2[20],str3[20];printf("input three line:\n");gets(str1);gets(str2);gets(str3);if(strcmp(str1,str2)>0) swap(str1,str2); if(strcmp(str1,str3)>0) swap(str1,str3); if(strcmp(str2,str3)>0) swap(str2,str3); printf("Now,the order is:\n");printf("%s\n%s\n%s\n",str1,str2,str3);return 0;}void swap(char *p1,char *p2){char p[20];strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p); }8-3#include <>int main(){ void input(int *);void max_min_value(int *);void output(int *);int number[10];input(number); max_min_value(number); output(number);return 0;}void input(int *number){int i;printf("input 10 numbers:");for (i=0;i<10;i++)scanf("%d",&number[i]);}void max_min_value(int *number) { int *max,*min,*p,temp;max=min=number;for (p=number+1;p<number+10;p++)if (*p>*max) max=p;else if (*p<*min) min=p;。

c语言程序设计谭浩强课后习题答案

c语言程序设计谭浩强课后习题答案

c语言程序设计谭浩强课后习题答案1. 一个C语言程序是由()。

[单选题] *一个主程序和若干子程序组成函数组成(正确答案)若干过程组成若干子程序组成2. 一个C程序的执行是从()。

[单选题] *本程序的main函数开始,到main函数结束(正确答案)本程序文件的第一个函数开始,到本程序文件的最后一个函数结束本程序的main函数开始,到本程序文件的最后一个函数结束本程序文件的第一个函数开始,到本程序main函数结束3. 同时定义相同类型的多个变量要使用()。

[单选题] *句号分号逗号(正确答案)冒号4. 标准C语言程序编译生成的文件后缀为()。

[单选题] *.c.cpp.obj(正确答案).exe5. 下面的注释那个写法是正确的()。

[单选题] */*this is a comment*\/*this is a comment/* it is error*//*this is a comment*/ (正确答案)/*this one seems like a comment doesn’t it6. 以下叙述中正确的是()。

[单选题] *用C程序实现的算法必须要有输入和输出操作用C程序实现的算法可以没有输出但必须要有输入用C程序实现的算法可以没有输入但必须要有输出(正确答案)用C程序实现的算法可以既没有输入也没有输出7. C语言中3种基本结构是()。

[单选题] *顺序结构、选择结构、循环结构(正确答案)if、switch、breakfor、while、do-whileif、for、continue8. 以下叙述中错误的是()。

[单选题] *用户所定义的标识符允许使用关键字(正确答案)用户所定义的标识符应尽量做到“见名知意”用户所定义的标识符必须以字母或下划线开头用户定义的标识符中,大、小写字母代表不同标识9. 以下不能定义为用户标识符的是()。

[单选题] *Main_0_intsizeof(正确答案)10. 以下选项中合法的用户标识符是()。

C语言程序设计(谭浩强) 课后答案

C语言程序设计(谭浩强) 课后答案
pi开始代表多项式的值最后代表的值代表分母term代表当前项的值whilefabsterm1e8检查当前项term的绝对值是否大于或等于10的6次方n2是下一项的分母signsign
第一章 程序设计和 C 语言 【第 15 页】 1-5 #include <stdio.h> int main ( ) { printf ("**************************\n\n"); printf(" Very Good!\n\n"); printf ("**************************\n"); return 0; }
//定义字符型变量 //定义整型变量
//赋值给整型变量
printf("按字符输出结果:\n"); printf("%c,%c\n",i1,i2); printf("按整数输出结果:\n"); printf("%d,%d\n",c1,c2); return 0; } 3-8-3 #include <stdio.h> int main() { char c1,c2; int i1,i2; printf("请输入两个整数 i1,i2:"); scanf("%d,%d",&i1,&i2); c1=i1; c2=i2; printf("按字符输出结果:\n"); printf("%c,%c\n",c1,c2); printf("按整数输出结果:\n"); printf("%d,%d\n",c1,c2); return 0; }
#include <stdio.h> int main () {float h,r,l,s,sq,vq,vz; float pi=3.141526; printf("请输入圆半径 r,圆柱高 h∶"); scanf("%f,%f",&r,&h); //要求输入圆半径 r 和圆柱高 h l=2*pi*r; //计算圆周长 l s=r*r*pi; //计算圆面积 s sq=4*pi*r*r; //计算圆球表面积 sq vq=3.0/4.0*pi*r*r*r; //计算圆球体积 vq vz=pi*r*r*h; //计算圆柱体积 vz printf("圆周长为: l=%6.2f\n",l); printf("圆面积为: s=%6.2f\n",s); printf("圆球表面积为: sq=%6.2f\n",sq); printf("圆球体积为: v=%6.2f\n",vq); printf("圆柱体积为: vz=%6.2f\n",vz); return 0; }
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第1章程序设计和C语言1什么是计算机程序1什么是计算机语言1语言的发展及其特点3最简单的C语言程序5最简单的C语言程序举例6语言程序的结构10运行C程序的步骤与方法12程序设计的任务141-5 #include <>int main ( ){ printf ("**************************\n\n"); printf(" Very Good!\n\n");printf ("**************************\n"); return 0;}1-6#include <>int main(){int a,b,c,max;printf("please input a,b,c:\n");scanf("%d,%d,%d",&a,&b,&c);max=a;if (max<b)max=b;if (max<c)max=c;printf("The largest number is %d\n",max); return 0;}第2章算法——程序的灵魂16什么是算法16简单的算法举例17算法的特性21怎样表示一个算法22用自然语言表示算法22用流程图表示算法22三种基本结构和改进的流程图26用N S流程图表示算法28用伪代码表示算法31用计算机语言表示算法32结构化程序设计方法34习题36第章最简单的C程序设计——顺序程序设计37顺序程序设计举例37数据的表现形式及其运算39常量和变量39数据类型42整型数据44字符型数据47浮点型数据49怎样确定常量的类型51运算符和表达式52语句57语句的作用和分类57最基本的语句——赋值语句59数据的输入输出65输入输出举例65有关数据输入输出的概念67用printf函数输出数据68用scanf函数输入数据75字符数据的输入输出78习题823-1 #include <>#include <>int main(){float p,r,n;r=;n=10;p=pow(1+r,n);printf("p=%f\n",p);return 0;}3-2-1#include <>#include <>int main(){float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5; p=1000;r5=;r3=;r2=;r1=;r0=;p1=p*((1+r5)*5); #include <> #include <>int main(){float d=300000,p=6000,r=,m;m=log10(p/(p-d*r))/log10(1+r);printf("m=%\n",m);return 0;}3-4#include <>int main(){int c1,c2;c1=197;c2=198;printf("c1=%c,c2=%c\n",c1,c2);printf("c1=%d,c2=%d\n",c1,c2);return 0;3-5#include <>int main(){int a,b;float x,y;char c1,c2;scanf("a=%d b=%d",&a,&b);scanf("%f %e",&x,&y);scanf("%c%c",&c1,&c2);printf("a=%d,b=%d,x=%f,y=%f,c1=%c,c2=%c\n",a,b,x,y,c1,c2); return 0;}3-6#include <>int main(){char c1='C',c2='h',c3='i',c4='n',c5='a';c1=c1+4;c2=c2+4;c3=c3+4;c4=c4+4;c5=c5+4;printf("passwor is %c%c%c%c%c\n",c1,c2,c3,c4,c5);return 0;}#include <>int main (){float h,r,l,s,sq,vq,vz;float pi=;printf("请输入圆半径r,圆柱高h∶");scanf("%f,%f",&r,&h); #include <> int main(){ int x,y;printf("输入x:");scanf("%d",&x);if(x<1) /* x<1 */{ y=x;printf("x=%3d, y=x=%d\n" ,x,y);}else if(x<10) /* 1=<x<10 */{ y=2*x-1;printf("x=%d, y=2*x-1=%d\n",x,y);}else /* x>=10 */{ y=3*x-11;printf("x=%d, y=3*x-11=%d\n",x,y);}return 0;}#include <>int main(){int x,y;printf("enter x:");scanf("%d",&x);y=-1;if(x!=0)if(x>0)y=1;elsey=0;printf("x=%d,y=%d\n",x,y); return 0;}4-7-2#include <>int main(){int x,y;printf("please enter x:"); scanf("%d",&x);y=0;if(x>=0)if(x>0) y=1;else y=-1;printf("x=%d,y=%d\n",x,y); return 0;}4-8#include <>int main(){ float score;char grade;printf("请输入学生成绩:"); scanf("%f",&score);while (score>100||score<0) {printf("\n 输入有误,请重输"); scanf("%f",&score);}switch((int)(score/10)){case 10:case 9: grade='A';break;case 8: grade='B';break;case 7: grade='C';break;case 6: grade='D';break;case 5:case 4:case 3:case 2:case 1:case 0: grade='E';}printf("成绩是 %,相应的等级是%c\n ",score,grade); return 0;}4-9#include <>#include <>int main(){int num,indiv,ten,hundred,thousand,ten_thousand,place;位,万位和位数printf("请输入一个整数(0-99999):");scanf("%d",&num);if (num>9999)place=5;else if (num>999)place=4;else if (num>99) .=%d\n",sn);return 0;}5-6#include <>int main(){double s=0,t=1;int n;for (n=1;n<=20;n++){t=t*n;s=s+t;}printf("1!+2!+...+20!=%\n",s);return 0;}5-7#include <>int main(){int n1=100,n2=50,n3=10;double k,s1=0,s2=0,s3=0;for (k=1;k<=n1;k++) /*计算1到100的和*/{s1=s1+k;}for (k=1;k<=n2;k++) /*计算1到50各数的平方和*/ {s2=s2+k*k;}for (k=1;k<=n3;k++) /*计算1到10的各倒数和*/ {s3=s3+1/k;}printf("sum=%\n",s1+s2+s3);return 0;}5-8#include <>int main(){int i,j,k,n;printf("parcissus numbers are ");for (n=100;n<1000;n++){i=n/100;j=n/10-i*10;k=n%10;if (n==i*i*i + j*j*j + k*k*k)printf("%d ",n);}printf("\n");return 0;}5-9-1#define M 1000 /*定义寻找范围*/ #include <>int main(){int k1,k2,k3,k4,k5,k6,k7,k8,k9,k10;int i,a,n,s;for (a=2;a<=M;a++) /* a是2-1000之间的整数,检查它是否完数 */{n=0; /* n用来累计a的因子的个数 */s=a; /* s用来存放尚未求出的因子之和,开始时等于a */ for (i=1;i<a;i++) /* 检查i是否a的因子 */if (a%i==0) /* 如果i是a的因子 */{n++; /* n加1,表示新找到一个因子 */s=s-i; /* s减去已找到的因子,s的新值是尚未求出的因子之和 */ switch(n) /* 将找到的因子赋给k1...k9,或k10 */{case 1:k1=i; break; /* 找出的笫1个因子赋给k1 */case 2:k2=i; break; /* 找出的笫2个因子赋给k2 */case 3:k3=i; break; /* 找出的笫3个因子赋给k3 */case 4:k4=i; break; /* 找出的笫4个因子赋给k4 */case 5:k5=i; break; /* 找出的笫5个因子赋给k5 */case 6:k6=i; break; /* 找出的笫6个因子赋给k6 */case 7:k7=i; break; /* 找出的笫7个因子赋给k7 */case 8:k8=i; break; /* 找出的笫8个因子赋给k8 */case 9:k9=i; break; /*找出的笫9个因子赋给k9 */case 10:k10=i; break; /* 找出的笫10个因子赋给k10 */}}if (s==0){printf("%d ,Its factors are ",a);if (n>1) printf("%d,%d",k1,k2); /* n>1表示a至少有2个因子 */if (n>2) printf(",%d",k3); /* n>2表示至少有3个因子,故应再输出一个因子 */if (n>3) printf(",%d",k4); /* n>3表示至少有4个因子,故应再输出一个因子 */if (n>4) printf(",%d",k5); /* 以下类似 */if (n>5) printf(",%d",k6);if (n>6) printf(",%d",k7);if (n>7) printf(",%d",k8);if (n>8) printf(",%d",k9);if (n>9) printf(",%d",k10);printf("\n");}}return 0;}5-9-2#include <>int main(){int m,s,i;for (m=2;m<1000;m++){s=0;for (i=1;i<m;i++)if ((m%i)==0) s=s+i;if(s==m){printf("%d,its factors are ",m); for (i=1;i<m;i++)if (m%i==0) printf("%d ",i); printf("\n");}}return 0;}5-10#include <>int main(){int i,n=20;double a=2,b=1,s=0,t;for (i=1;i<=n;i++){s=s+a/b;t=a,a=a+b,b=t;}printf("sum=%\n",s);return 0;}5-11#include <>int main(){double sn=100,hn=sn/2;int n;for (n=2;n<=10;n++){sn=sn+2*hn; /*第n次落地时共经过的米数*/ hn=hn/2; /*第n次反跳高度*/}printf("第10次落地时共经过%f米\n",sn); printf("第10次反弹%f米\n",hn);return 0;}5-12#include <>int main(){int day,x1,x2;day=9;x2=1;while(day>0){x1=(x2+1)*2; /*第1天的桃子数是第2天桃子数加1后的2倍.*/ x2=x1;day--;}printf("total=%d\n",x1);return 0;}5-13#include <>#include <>int main(){float a,x0,x1;printf("enter a positive number:");scanf("%f",&a);x0=a/2;x1=(x0+a/x0)/2;do{x0=x1;x1=(x0+a/x0)/2;}while(fabs(x0-x1)>=1e-5);printf("The square root of % is %\n",a,x1); return 0;}5-14#include <>#include <>int main(){double x1,x0,f,f1;x1=;do{x0=x1;f=((2*x0-4)*x0+3)*x0-6;f1=(6*x0-8)*x0+3;x1=x0-f/f1;}while(fabs(x1-x0)>=1e-5);printf("The root of equation is %\n",x1); return 0;}5-15#include <>#include <>int main(){float x0,x1,x2,fx0,fx1,fx2;do{printf("enter x1 & x2:"); scanf("%f,%f",&x1,&x2);fx1=x1*((2*x1-4)*x1+3)-6; fx2=x2*((2*x2-4)*x2+3)-6; }while(fx1*fx2>0);do{x0=(x1+x2)/2;fx0=x0*((2*x0-4)*x0+3)-6; if ((fx0*fx1)<0){x2=x0;fx2=fx0;}else{x1=x0;fx1=fx0;}}while(fabs (fx0)>=1e-5); printf("x=%\n",x0);return 0;}5-16#include <>int main(){int i,j,k;for (i=0;i<=3;i++){for (j=0;j<=2-i;j++)printf(" ");for (k=0;k<=2*i;k++)printf("*");printf("\n");}for (i=0;i<=2;i++){for (j=0;j<=i;j++)printf(" ");for (k=0;k<=4-2*i;k++)printf("*");printf("\n");}return 0;}5-17#include <>int main(){char i,j,k; /*是a的对手;j是b的对手;k是c的对手*/ for (i='x';i<='z';i++)for (j='x';j<='z';j++)if (i!=j)for (k='x';k<='z';k++)if (i!=k && j!=k)if (i!='x' && k!='x' && k!='z')printf("A--%c\nB--%c\nC--%c\n",i,j,k); return 0;}第6章利用数组处理批量数据142怎样定义和引用一维数组142怎样定义一维数组143怎样引用一维数组元素144一维数组的初始化145一维数组程序举例146怎样定义和引用二维数组148怎样定义二维数组149怎样引用二维数组的元素150二维数组的初始化151二维数组程序举例152字符数组154怎样定义字符数组154字符数组的初始化155怎样引用字符数组中的元素155字符串和字符串结束标志156字符数组的输入输出159使用字符串处理函数161字符数组应用举例165习题1686-1#include <>#include <>int main(){int i,j,n,a[101];for (i=1;i<=100;i++)a[i]=i;a[1]=0;for (i=2;i<sqrt(100);i++) for (j=i+1;j<=100;j++) {if(a[i]!=0 && a[j]!=0) if (a[j]%a[i]==0)a[j]=0;}printf("\n");for (i=2,n=0;i<=100;i++) { if(a[i]!=0){printf("%5d",a[i]);n++;}if(n==10){printf("\n");n=0;}}printf("\n");return 0;}6-2#include <>int main(){int i,j,min,temp,a[11];printf("enter data:\n");for (i=1;i<=10;i++){printf("a[%d]=",i);scanf("%d",&a[i]);}printf("\n");printf("The orginal numbers:\n"); for (i=1;i<=10;i++)printf("%5d",a[i]);printf("\n");for (i=1;i<=9;i++){min=i;for (j=i+1;j<=10;j++)if (a[min]>a[j]) min=j;temp=a[i];a[i]=a[min];a[min]=temp;}printf("\nThe sorted numbers:\n");for (i=1;i<=10;i++)printf("%5d",a[i]);printf("\n");return 0;}6-3#include <>int main(){int a[3][3],sum=0;int i,j;printf("enter data:\n");for (i=0;i<3;i++)for (j=0;j<3;j++)scanf("%3d",&a[i][j]);for (i=0;i<3;i++)sum=sum+a[i][i];printf("sum=%6d\n",sum);return 0;}6-4#include <>int main(){ int a[11]={1,4,6,9,13,16,19,28,40,100}; int temp1,temp2,number,end,i,j;for (i=0;i<10;i++)printf("%5d",a[i]);printf("\n");printf("insert data:"); scanf("%d",&number);end=a[9];if (number>end)a[10]=number;else{for (i=0;i<10;i++){if (a[i]>number){temp1=a[i];a[i]=number;for (j=i+1;j<11;j++) {temp2=a[j];a[j]=temp1;temp1=temp2;}break;}}}printf("Now array a:\n"); for (i=0;i<11;i++)printf("\n");return 0;}6-5#include <>#define N 5int main(){ int a[N],i,temp;printf("enter array a:\n");for (i=0;i<N;i++)scanf("%d",&a[i]);printf("array a:\n");for (i=0;i<N;i++)printf("%4d",a[i]);for (i=0;i<N/2;i++) n",number);; printf("continu or not(Y/N)?");scanf(" %c",&c);if (c=='N'||c=='n')flag=0;}return 0;}6-10#include <>int main(){int i,j,upp,low,dig,spa,oth;char text[3][80];upp=low=dig=spa=oth=0;for (i=0;i<3;i++){ printf("please input line %d:\n",i+1);gets(text[i]);for (j=0;j<80 && text[i][j]!='\0';j++){if (text[i][j]>='A'&& text[i][j]<='Z')upp++;else if (text[i][j]>='a' && text[i][j]<='z') low++; else if (text[i][j]>='0' && text[i][j]<='9') dig++; else if (text[i][j]==' ')spa++;elseoth++;}}printf("\nupper case: %d\n",upp);printf("lower case: %d\n",low);printf("digit : %d\n",dig);printf("space : %d\n",spa);printf("other : %d\n",oth);return 0;}#include <>int main(){ char a[5]={'*','*','*','*','*'}; int i,j,k;char space=' ';for (i=0;i<5;i++){ printf("\n");printf(" ");for (j=1;j<=i;j++)printf("%c",space);for (k=0;k<5;k++)printf("%c",a[k]);}printf("\n");return 0;}6-12a-c#include <>int main(){ int j,n;char ch[80],tran[80];printf("input cipher code:");gets(ch);printf("\ncipher code :%s",ch);while (ch[j]!='\0'){ if ((ch[j]>='A') && (ch[j]<='Z')) tran[j]=155-ch[j];else if ((ch[j]>='a') && (ch[j]<='z')) tran[j]=219-ch[j];elsetran[j]=ch[j];j++;}n=j;printf("\noriginal text:");for (j=0;j<n;j++)putchar(tran[j]);printf("\n");return 0;}6-12b#include <>int main(){int j,n;char ch[80];printf("input cipher code:\n");gets(ch);printf("\ncipher code:%s\n",ch);while (ch[j]!='\0'){ if ((ch[j]>='A') && (ch[j]<='Z'))ch[j]=155-ch[j];else if ((ch[j]>='a') && (ch[j]<='z')) ch[j]=219-ch[j];elsech[j]=ch[j];j++;}n=j;printf("original text:");for (j=0;j<n;j++)putchar(ch[j]);printf("\n");return 0;}6-13#include <>int main(){ char s1[80],s2[40];int i=0,j=0;printf("input string1:");scanf("%s",s1);printf("input string2:");scanf("%s",s2);while (s1[i]!='\0')i++;while(s2[j]!='\0')s1[i++]=s2[j++];s1[i]='\0';printf("\nThe new string is:%s\n",s1); return 0;}6-14#include <>int main(){ int i,resu;char s1[100],s2[100];printf("input string1:");gets(s1);printf("\ninput string2:");gets(s2);i=0;while ((s1[i]==s2[i]) && (s1[i]!='\0'))i++; if (s1[i]=='\0' && s2[i]=='\0')resu=0;elseresu=s1[i]-s2[i];printf("\nresult:%d.\n",resu);return 0;}6-15#include <>#include <>int main(){ char s1[80],s2[80];int i;printf("input s2:");scanf("%s",s2);for (i=0;i<=strlen(s2);i++)s1[i]=s2[i];printf("s1:%s\n",s1);return 0;}第7章用函数实现模块化程序设计170为什么要用函数170怎样定义函数172为什么要定义函数172定义函数的方法173调用函数174函数调用的形式174函数调用时的数据传递175函数调用的过程177函数的返回值178对被调用函数的声明和函数原型179函数的嵌套调用182函数的递归调用184数组作为函数参数192数组元素作函数实参193数组名作函数参数194多维数组名作函数参数197局部变量和全局变量199局部变量199全局变量200变量的存储方式和生存期204动态存储方式与静态存储方式204局部变量的存储类别205全局变量的存储类别208存储类别小结212关于变量的声明和定义214内部函数和外部函数215内部函数215外部函数215习题2187-1-1#include <>int main(){int hcf(int,int);int lcd(int,int,int);int u,v,h,l;scanf("%d,%d",&u,&v);h=hcf(u,v);printf("",h);l=lcd(u,v,h);printf("",l);return 0;}int hcf(int u,int v){int t,r;if (v>u){t=u;u=v;v=t;}while ((r=u%v)!=0){u=v;v=r;}return(v);}int lcd(int u,int v,int h) {return(u*v/h);}7-1-2#include <>int Hcf,Lcd;int main(){void hcf(int,int); void lcd(int,int);int u,v;scanf("%d,%d",&u,&v); hcf(u,v);lcd(u,v);printf("",Hcf);printf("",Lcd); return 0;}void hcf(int u,int v) {int t,r;if (v>u){t=u;u=v;v=t;}while ((r=u%v)!=0) {u=v;v=r;}Hcf=v;}void lcd(int u,int v) {Lcd=u*v/Hcf;}7-2#include <>#include <>float x1,x2,disc,p,q;int main(){void greater_than_zero(float,float);void equal_to_zero(float,float);void smaller_than_zero(float,float);float a,b,c;printf("input a,b,c:");scanf("%f,%f,%f",&a,&b,&c);printf("equation: %*x*x+%*x+%=0\n",a,b,c); disc=b*b-4*a*c; printf("root:\n");if (disc>0){greater_than_zero(a,b);printf("x1=%f\t\tx2=%f\n",x1,x2);}else if (disc==0){equal_to_zero(a,b);printf("x1=%f\t\tx2=%f\n",x1,x2);}else{smaller_than_zero(a,b);printf("x1=%f+%fi\tx2=%f-%fi\n",p,q,p,q);}return 0;}void greater_than_zero(float a,float b) {x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);}void equal_to_zero(float a,float b){x1=x2=(-b)/(2*a);}void smaller_than_zero(float a,float b) {p=-b/(2*a);q=sqrt(-disc)/(2*a);}7-3#include <>int main(){int prime(int);int n;printf("input an integer:");scanf("%d",&n);if (prime(n))printf("%d is a prime.\n",n);elseprintf("%d is not a prime.\n",n); return 0;}int prime(int n){int flag=1,i;for (i=2;i<n/2 && flag==1;i++)if (n%i==0)flag=0;return(flag);}7-4#include <>#define N 3int array[N][N];int main(){ void convert(int array[][3]); int i,j;printf("input array:\n");for (i=0;i<N;i++)for (j=0;j<N;j++)scanf("%d",&array[i][j]);printf("\noriginal array :\n"); for (i=0;i<N;i++){for (j=0;j<N;j++)printf("%5d",array[i][j]);printf("\n");}convert(array);printf("convert array:\n"); for (i=0;i<N;i++){for (j=0;j<N;j++)printf("%5d",array[i][j]); printf("\n");}return 0;}void convert(int array[][3]) {int i,j,t;for (i=0;i<N;i++)for (j=i+1;j<N;j++){t=array[i][j];array[i][j]=array[j][i]; array[j][i]=t;}}#include <>#include <>int main(){void inverse(char str[]); char str[100];printf("input string:");scanf("%s",str);inverse(str);printf("inverse string:%s\n",str);return 0;}void inverse(char str[]){char t;int i,j;for (i=0,j=strlen(str);i<(strlen(str)/2);i++,j--){t=str[i];str[i]=str[j-1];str[j-1]=t;}}7-6#include <>int main(){void concatenate(char string1[],char string2[],char string[]); char s1[100],s2[100],s[100];printf("input string1:");scanf("%s",s1);printf("input string2:");scanf("%s",s2);concatenate(s1,s2,s);printf("\nThe new string is %s\n",s);return 0;}void concatenate(char string1[],char string2[],char string[]) {int i,j; for (i=0;string1[i]!='\0';i++)string[i]=string1[i];for(j=0;string2[j]!='\0';j++)string[i+j]=string2[j];string[i+j]='\0';}7-7#include <>int main(){void cpy(char [],char []);char str[80],c[80];printf("input string:");gets(str);cpy(str,c);printf("The vowel letters are:%s\n",c);return 0;}void cpy(char s[],char c[]){ int i,j;for (i=0,j=0;s[i]!='\0';i++)if (s[i]=='a'||s[i]=='A'||s[i]=='e'||s[i]=='E'||s[i]=='i'||s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||s[i]=='U') {c[j]=s[i];j++;}c[j]='\0';}7-8#include <>#include <>int main(){char str[80];void insert(char []);printf("input four digits:");scanf("%s",str);insert(str);return 0;本文档下载自360文档中心,更多营销,职业规划,工作简历,入党,工作报告,总结,学习资料,学习总结,PPT模板下载,范文等文档下载;转载请保留出处。

相关文档
最新文档