当前位置:文档之家› 高等教育出版社出版社c语言程序设计实践教程习题参考答案

高等教育出版社出版社c语言程序设计实践教程习题参考答案

附录习题参考答案附录习题参考答案方法2:#include <stdio.h>void main(){int a,b,c,sum;for (a=100;a<=999;a++){sum=0;b=a;while(b!=0){c=b%10;sum+=c*c*c;b=b/10;}if(sum==1099)printf("%5d",a);}}3.#include <stdio.h>void main(){int i,j,k;for (i=1;i<=6;i++){for (j=1;j<=20-3*i;j++) printf(" ");for (k=1;k<=i;k++) printf("%3d",k);for (k=i-1;k>0;k--) printf("%3d",k);printf("\n");}}4.#include <stdio.h>void main(){float x,fmax,fmin;scanf("%f",&x);fmax=fmin=x;while (x>=0){if (x>fmax)fmax=x;elseif (x<fmin)fmin=x;scanf("%f",&x);}printf("\nmax=%f,min=%f",fmax,fmin);}7.2 练习题p89-96一、选择题1~5.BCCAD 6~10.CDDCD11~15.DDABC 16~17.BBAAD21~26.BDCDDC二、填空题1.(1)’a’ (2)’\o’2.(1)a[k] (2)a[k] (3)a[k]3.6 4.A B C D EA5.1,6 6.k=p;7.i<j 8.189.4,some string *test 10. you&me三、读程序,写结果1.5 4 8 2 0 2.10 24 15 63.love china! H w l4.66 66 555v. Front四、程序设计题1.#include <stdio.h>void main(){int i,j,a[10],k;for(i=0;i<=9;i++)scanf("%d",&a[i]);for(i=0,j=9;i<=j;i++,j--){k=a[i];a[i]=a[j];a[j]=k;}for(i=0;i<=9;i++)printf("%d",a[i]);}2.#include <stdio.h>#include <ctype.h>void main(){char i,a[80];scanf("%s",a);for(i=0;i<strlen(a);i++)if (islower(a[i]))a[i]=a[i]-32;else附录习题参考答案if (isupper(a[i]))a[i]=a[i]+32;printf("%s",a);}3.#include <stdio.h>void main(){char a[80],i;gets(a);for(i=strlen(p)-1;i>=0;i--)putchar(a[i]);}4.#include <stdio.h>void main(){int a[3][6],i,j;int d,d1=0,d2=0,x,x1=0,x2=0;for(i=0;i<3;i++)for(j=0;j<6;j++){scanf("%d",&a[i][j]);if (i= =0&&j= =0) x=d=a[i][j];if (d<a[i][j) {d=a[i][j];d1=i;d2=j;}else if (x>a[i][j]) {x=a[i][j];x1=i;x2=j;}p++;}printf("d=%d,d1=%d,d2=%d\n",d,d1,d2);printf("x=5d,x1=%d,x2=%d\n",x,x1,x2);}5.#include <stdio.h>void main(){int a[10],i,d,d1=0,x,x1=0;for(i=0;i<10;i++,p++){scanf("%d",&a[i]);if (i= =0) {d=a[i];x=a[i];}if (d<a[i]) {d=a[i];d1=i;}else if (x>a[i]) {x=a[i];x1=i;}}a[d1]=a[0];a[0]=d;a[x1]=a[9];a[9]=x;for(i=9;i>=0;i--,p--)printf("%d,",*p);} 8.2练习题p106-114一、选择题1~5.AABBB 6~10.ADCAA11~15.ABBAB 16~17.BC二、填空题1.12 2.(1)Itis3.(1)n*fun(n-1) (2)fun(k) 4.s[i]-t[i]5.(1)n%m (2)i-1 (3)x[d];6.(1)x=n; (2)n%10 (3) x7.m= f(a,4)+f(b,4)-f(a+b,3) 8.a=1,b=1;三、读程序,写结果1.s=7 2.s=37 3.bij12四、程序设计题1.int nian(int y){if((y%4==0)&&(y%100!=0)||(y%400==0)) return 1;else return 0;}2.void fun(float a[],float b[]){int i;float s=0;b[0]=b[1]=a[0];for(i=0;i<n;i++){s=s+a[i];if (b[0]<a[i]) b[0=a[i];else if (b[1]>a[i]) b[1]=a[i];}b[2]=s/n;}3.int cout(char str[],char sub[]){int i,j,k;int count=0;for(i=0;str[i];i++)for(j=i,k=0;sub[k]= =str[j];k++,j++)if(sub[k+1]=='\0'){count++;break;}return count;}附录习题参考答案4.#include "stdio.h"void pp(int m){int j;if(j=m/10) pp(j);putchar(n%10+'0');}void main(){int n;scanf("%d",&n);if(n<0){putchar('-');n=-n;}pp(n);}5.#include "stdio.h"int add(int n){int r,s=0;do{r=n%10;s+=r;n=n/10;}while(n>0);return s;}void main(){int a;scanf("%d",&a);printf("%d", add(a));}6.#include <math.h>#include "stdio.h"void prime(int n){int i,f=1;for(i=2;i<=sqrt(n);i++)if(n%i= =0){f=0;break;}}void main(){int m;scanf("%d",&m);if(prime(m)) printf("%d", m);else printf("不是素数");}7.参考程序int total(char s[ ],char ch){int i=0,n=0;while(s[i])if(s[i++]==ch) n++;return(n);}#include "stdio.h"void main(){char s[20],ch;gets(s);ch=getchar();printf("%d", total(s,ch));}8.void move(char *x,int n,int m) {int i,j;for(j=0;j<m;j++){char w=*(x+n-1);for(i=0;i<n-1;i++)*(x+n-1-i)=*(x+n-2-i);*x=w;}}9.void print(char *string){char *str=string;if(*str=='\0')return;while(*str)str++;putchar(*--str);*str='\0';print(string);}。

相关主题