宁波大学《vc++6.0环境的高级语言程序设计C》参考答案2#includestdio.h#includemath.hvoidmain(){floatx,y;printf(输入x(-5x10):\n);scanf(%f,&x);if(-5x&&x0)y=x;elseif(x=0)y=x-1;elsey=x+1;printf(y=%f\n,y);}3.1#includestdio.h#includemath.hvoidmain(){floatx,y;printf(输入x\n);scanf(%f,&x);if(x=0)y=sqrt(x);elsey=pow((x+1),2)+2*x+1/x;printf(y=%.2f,y);}3.2#includestdio.h#includemath.hvoidmain(){doublex,y;printf(输入x(-5x10):\n);scanf(%lf,&x);if((x0)&&x10)y=x+1;elseif(0==x)y=x-1;elseif((x-5)&&(x0))y=x;printf(y=%f\n,y);}4.1#includestdio.hmain(){inti=1;intsum=0;while(i=1000){sum=sum+i;i=i+2;}printf(sum=%d\n,sum);}#includestdio.hmain(){inti=1;intsum=0;do{sum=sum+i;i=i+2;}while(i=1000);printf(sum=%d\n,sum);}#includestdio.hvoidmain(){inti=1;intsum=0;for(i=1;i=1000;i=i+2){sum=sum+i;}printf(sum=%d\n,sum);}4.2#includestdio.h#includemath.hvoidmain(){floati=1,x=1,sum=0,n;printf(请输入n\n);scanf(%f,&n);while(x=n){sum=sum+1/i;i=i+2;x=x+1;}printf(sum=%.6f\n,sum);}4.3#includestdio.h#includemath.hvoidmain(){inta,n,i;longsum=0;printf(Pleaseinputa(0a=9)andn:);scanf(%d%d,&a,&n);if(a9||a=0){printf(aisnotin(1,2,3,4,5,6,7,8,9)!\npleaseinputaagain:);scanf(%d,&a);}for(i=1;i=n;i++){sum+=(pow(10,i)-1)*a/9.0;}printf(sum=%ld,sum);}5.1#includestdio.hintf1(inta){intresult;result=a*a*a;returnresult;}intf2(intx,inty){intans;ans=x+y;returnans;}voidmain(){inti=1;intsum;for(sum=0;i=5;i++){sum=f2(sum,f1(i));}printf(sum=%d\n,sum);}5.2#includestdio.hintfactor(intm){intfactor;inta,sum=0;for(a=1;am;a++){if(m%a==0)sum=sum+a;}return(sum==m);}voidmain(){intm;for(m=2;m=10000;m++){if(factor(m)==0)continue;printf(%d\t,m);}}99乘法表打印程序#includestdio.hvoidmain(){inti,j;for(i=1;i=9;i++){for(j=1;j=i;j++){printf(%d*%d=%d,i,j,i*j);}printf(\n);}}最大值最小值#includestdio.h#defineN10voidmain(){inti,j,k=0,max,min,a[N];printf(Inputyourdateplease!\n);for(i=0;iN;i++)scanf(%d,&a[i]);max=min=a[0];for(i=0;iN;i++){if(maxa[i]){max=a[i];j=i;}elseif(mina[i]){min=a[i];k=j;}}printf(max:a[%d]=%d,min:a[%d]=%d\n,j,max,k,min);}数组倒置#includestdio.h#defineN8voidmain(){inti,j,temp,a[N];printf(Inputyourdateplease!\n);for(i=0;iN;i++)scanf(%d,&a[i]);for(i=0,j=N-1;i=j;i++,j--){temp=a[i];a[i]=a[j];a[j]=temp;}for(i=0;iN;i++)printf(%d,a[i]);}7.1数组转制#includestdio.hvoidmain(){inti,j,t;ints[3][3];printf(beforearraybetransposed:\n);for(i=0;i3;i++){for(j=0;j3;j++){s[i][j]=i*3+j+1;printf(%3d,s[i][j]);}printf(\n);}for(i=0;i=3;i++)for(j=0;j3;j++)if(ij){t=s[i][j];s[i][j]=s[j][i];s[j][i]=t;}printf(afterarraybetransposed:\n);for(i=0;i3;i++){for(j=0;j3;j++)printf(%3d,s[i][j]);printf(\n);}}7.2#includestdio.h#defineM5#defineN3voidmain(){inti,j;intscores[M][N];floatave[N],sum=0;printf(请输入5个同学,3门课的成绩\n);for(i=0;iM;i++){printf(inputNO.%dstudents'scores:,i+1);for(j=0;jN;j++)scanf(%d,&scores[i][j]);}for(j=0;jN;j++){ave[j]=0.0;for(i=0;iM;i++)ave[j]=ave[j]+scores[i][j];ave[j]=ave[j]/M;sum=sum+ave[j];}printf(各科平均成绩分别为:);{for(j=0;jN;j++)printf(%4.0f,ave[j]);}sum=sum/3;printf(各科总平均成绩为:%4.0f,sum);}8.1#includestdio.hintmax,min;voidmax_min(int*array,intn){int*p,*array_end;array_end=array+n;max=min=*array;for(p=array+1;parray_end;p++)if(*pmax)max=*p;elseif(*pmin)min=*p;}voidmain(){inti,number[10],*p;p=number;printf(\nEnter10date:\n);for(i=0;i10;i++)scanf(%d,&number[i]);max_min(p,10);printf(最大值=%d,最小值=%d\n,max,min);}8.2#includestdio.hvoidmain(){inta[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};int*p;for(p=a[0];pa[0]+12;p++){if((p-a[0])%4==0)printf(\n);printf(%4d,*p);}}输入字符写到磁盘#includestdio.h#includestdlib.hvoidmain(){FILE*fp;charch,filename[10];scanf(%s,filename);if((fp=fopen(filename,w))==NULL){printf(cannotopenthisfile\n);exit(0);}ch=getchar();ch=getchar();while(ch!='#'){fputc(ch,fp);putchar(ch);ch=getchar();}fclose(fp);}9.1#includestdio.h#includestring.hintfun(charstr[]){inti,j,n;n=strlen(str);for(i=0,j=n-1;i=(n-1)/2;i++,j--)if(str[i]!=str[j])break;if(ij)return1;return0;}voidmain(){chara[80];printf(pleaseinputn:);gets(a);if(fun(a)==1)printf(YES\n);elseprintf(NO\n);}9.2#includestring.h#includestdio.h#defineN81voidfun(char*s){inti=0,t,n=strlen(s);for(;s+is+n-1-i;i++){t=*(s+i);*(s+i)=*(s+n-1-i);*(s+n-1-i)=t;}}voidmain(){chara[N];printf(Enterastring:);gets(a);printf(Theoriginalstringis:);puts(a);fun(a);printf(\n);printf(Thestringaftermodified:);puts(a);}9.3#includestdio.hvoidmain(){intn,i,j;charm;m='A';printf(pleaseinputn:);scanf(%d,&n);for(i=1;i=n;i++){for(j=1;j=(2*i-1);j++){printf(%c,'A'+i-1);}printf(\n);}}#includestdio.hvoidmain(){intn,i,j;charm;m='A';printf(pleaseinputn:);scanf(%d,&n);for(i=1;i=n;i++){for(j=1;j=(n-i);j++){printf();}for(j=0;j(2*i-1);j++){printf(%c,'A'+i-1);}printf(\n);}}#includestdio.hvoidmain(){intn,i,j;charm;m='A';printf(pleaseinputn:);scanf(%d,&n);for(i=1;i=n;i++){for(j=1;j=(2*i-1);j++){printf(%c,'A'+i-1);}printf(\n);}}10.1#includestdio.hvoidmain(){intn,i,j;charm;m='A';printf(pleaseinputn:);scanf(%d,&n);for(i=1;i=n;i++){for(j=1;j=(n-i);j++){printf();}for(j=0;j(2*i-1);j++){printf(%c,'A'+i-1);}printf(\n);}}10.2#includestdio.htypedefs