第一篇:C语言苏小红版第六章习题答案[推荐]
第六章习题 1.(1)#include
int i,j,k;
char space=' ';
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf(“%c”,space);} for(k=1;k<=6;k++){
printf(“*”);}
printf(“n”);
} }
(2)#include
if(n % 2 == 0)continue;
k--;} printf(“k = %d,n = %dn”,k,n);}
(3)#include
int k = 4, n;
} for(n = 0;n < k;n++){ if(n % 2 ==0)
break;k--;} printf(“k = %d, n = %dn”, k, n);2.
(1)计算1+3+5+……99+101=
#include
int i,sum=0;
for(i=1;i<=101;i=i+2)
{
sum=sum+i;
}
printf(“sum=%dn”,sum);}
(2)计算1*2*3+3*4*5+...+99*100*101= #include
long i;
long term,sum=0;
for(i=1;i<=99;i=i+2)
{ term=i*(i+1)*(i+2);
sum=sum+term;
}
printf(“sum=%ld”,sum);}
(3)计算a+aa+aaa+...+aa...a(n个a)的值
#include
long term=0,sum=0;
int a,i,n;
printf(“input a,n:”);
scanf(“%d,%d”,&a,&n);
for(i=1;i<=n;i=i++)
{ term=term*10+a;
sum=sum+term;
}
printf(“sum=%ldn”,sum);}(4)计算1—1/2+1/3-...+1/99-1/100+...,直到最后一项的绝对值小于10#include
int n=1;
float term=1.0,sign=1,sum=0;
while(fabs(term)>=1e-4)
{
term=sign*1.0/n;
sum=sum+term;
sign=sign*(-1);
n++;
}
printf(“sum=%fn”,sum);}
-4。
(5)#include
int n=1,count=1;
float x;
double sum,term;
printf(“input x:”);
scanf(“%f”,&x);
sum=x;
term=x;
do{
term=(-1)*term*x*x/((n+1)*(n+2));
sum=sum+term;
n=n+2;
count++;
}while(fabs(term)>=1e-5);
printf(“sin(x)=%f,count=%dn”,sum,count);}
3.#include
while(!find)
/*当没找到时继续执行循环*/ {
x++;
if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0)
{
printf(“x=%dn”,x);
find=1;
} } }
4. #include
m=i*i;
p=i*i*i;
printf(“m=%ld,p=%ldn”,m,p);} }
5.#include
f=9/5*c+32;
printf(“f=%fn”,f);
}
}
6.#include
x=x*pow(1+c,12)-1000;
n++;}while(x>0);
} printf(“x=%dn”,x);7.#include
x=x*(1+a);
n++;}while(x<=200.0);printf(“n=%dn”,n);}.#include
int n=1,count=1;
float sum=0,term=1.0,sign=1;
do{
term=sign*1.0/n;
sum=sum+term;
sign=sign*(-1);
n=n+2;
count++;
}while(fabs(term)>=1e-4);
sum=4*sum;
printf(“sum=%f,count=%dn”,sum,count);}
9. #include
int n=1,count=1;
float sum=1,term=1.0;
do{
term=term/n;
sum=sum+term;
n=n+1;
count++;
}while(fabs(term)>=1e-5);
printf(“e=%f,count=%dn”,sum,count);}
10.#include
int m;
for(m=100;m<=999;m++)
{
if(m==pow(m/100,3)+pow(m/10%10,3)+pow(m%10,3))
printf(“m=%dn”,m);
} }
11.#include
int i=0,n;
long sum=0,term=1;
printf(“Inputn:”);
scanf(“%d”,&n);
do{
i++;
term=term*i;
sum=sum+term;
}while(sum printf(“%dn”,i-1);} 12.#include int i,n,m,sum=0,count=0; printf(“input n:”); scanf(“%d”,&n); for(i=1;i<=n;i++) { printf(“please enter m:”); scanf(“%d”,&m); if(m>0) { sum=sum+m; count++; } else { break; } printf(“sum=%d,count=%dn”,sum,count);} printf(“program is over!n”);} 13.#include int i,n,m,sum=0,count=0; printf(“input n:”); scanf(“%d”,&n); for(i=1;i<=n;i++) { printf(“please enter m:”); scanf(“%d”,&m); if(m>0) { sum=sum+m; count++; } else if(m<0) { continue; } else { break; } printf(“sum=%d,count=%dn”,sum,count);} printf(“program is over!n”);} 14.#include for(y=0;y<=25;y++) { for(z=0;z<=30;z++) } { if(x+y+z==30&&3*x+2*y+1*z==50)printf(“x=%d,y=%d,z=%dn”,x,y,z); } } } 15.#include for(y=0;y<97;y++) { if(x+y==98&&2*x+4*y==386) printf(“x=%d,y=%dn”,x,y); } } } 16.#include for(y=0;y<=33;y++) { for(z=0;z<=100;z++) { if(x+y+z==100&&5*x+3*y+z/3.0==100) } printf(“x=%d,y=%d,z=%dn”,x,y,z); } } } 17.#include for(y=0;y<=20;y++) { for(z=0;z<=100;z++) { if(x+y+z==50&&10*x+5*y+z==100) printf(“x=%d,y=%d,z=%dn”,x,y,z); } } } } 18.#include int i,j,k,c=0; for(i=1;i<=9;i++) printf(“%6d”,i); printf(“n”); for(i=1;i<=9;i++) printf(“ -”); printf(“n”); for(int x=1;x<=9;x++){ for(int y=1;y<=9;y++) printf(“%6d”,x*y); printf(“n”); } } #include int i,j,k,c=0; for(i=1;i<=9;i++) printf(“%6d”,i); printf(“n”); for(i=1;i<=9;i++) printf(“ -”); printf(“n”); for(int x=1;x<=9;x++){ for(int y=1;y<=x;y++) printf(“%6d”,x*y); printf(“n”); } } #include int i,j,k,c=0; for(i=1;i<=9;i++) printf(“%6d”,i); printf(“n”); for(i=1;i<=9;i++) printf(“ -”); printf(“n”); for(i=1;i<=9;i++) { for(k=1;k<=c;k++) printf(“ ”); c++; j=0; while(j<9) { j++; if(j printf(“%6d”,i*j); } printf(“n”); } } 2.2 #include #include #include #include a=a; printf(“a=%fn”,a);} else { a=-a; printf(“a=%fn”,a);} } 5.2 #include printf(“a是偶数”);} else { printf(“a是奇数”);} } 5.3 #include s=(a+b+c)/2; area=(float)sqrt(s*(s-a)*(s-b)*(s-c)); printf(“area=%fn”,area);} else { printf(“不是三角形”);} } 5.4 #include printf(“该方程不是一元二次方程n”);} if(b*b-4*a*c>0){ x=(-b+sqrt(b*b-4*a*c))/(2*a); y=(-b-sqrt(b*b-4*a*c))/(2*a); printf(“x=%f,y=%fn”,x,y);} else if(b*b-4*a*c==0){ x=-b/(2*a); y=-b/(2*a); printf(“x=%f,y=%fn”,x,y);} else { printf(“该方程无实根n”);} } 5.5 #include flag=1;} else { flag=0;} if(flag==1){ printf(“%d is a leap year!n”,year);} else { printf(“%d is not a leap year!n”,year);} } 5.6 #include printf(“%d is a leap year!n”,year);} else { printf(“%d is not a leap year!n”,year);} } 5.7 #include ch=getchar(); ch=ch-32; printf(“%c,%dn”,ch,ch);} else if(ch>='A'&&ch<='Z'){ ch=getchar(); ch=ch+32; printf(“%c,%dn”,ch,ch);} else { printf(“%c”,ch);} } 5.8 #include if(ch>=48&&ch<=57) { printf(“ch是数字字符n”); } else if(ch>=65&&ch<=90) { printf(“ch是大写字母n”); } else if(ch>=97&&ch<=122) { printf(“ch是小写字母n”); } else if(ch==32) { printf(“ch是空格n”); } else { printf(“ch是其他字符n”); } } 5.9 #include grade=score/10;if(score<0||score>100){ printf(“Input errorn”);} if(score>=90&&score<=100) { printf(“%d--An”,score); } else if(score>=80&&score<90) { printf(“%d--Bn”,score); } else if(score>=70&&score<80) { printf(“%d--Cn”,score); } else if(score>=60&&score<70) { printf(“%d--Dn”,score); } else if(score>=0&&score<60) { printf(“%d--En”,score); } } 5.10 #include if(month>12||month<=0){ printf(“error monthn”);} else { switch(year,month){ case 12: case 10: case 8: case 7: case 5: case 3: case 1: printf(“31天n”); break;case 11: case 9: case 6: case 4: printf(“30天n”); break;case 2: if(year%4==0&&year!=0||year%400==0) { printf(“29天n”); } else { printf(“28天n”); } break; default: printf(“Input errorn”);} } } 6.1(1)#include for(j=1;j<=i;j++) { printf(“%c”,space); } for(k=1;k<=6;k++) { printf(“*”); } printf(“n”);} } 6.1(2) #include if(n%2==0) continue; k--;} printf(“k=%dn,n=%dn”,k,n);} 6.1(3)#include if(n%2==0) break; k--;} printf(“k=%d,n=%dn”,k,n);} 6.2(1) #include sum=sum+i;} printf(“sum=%dn”,sum);} 6.2(2)#include term=i*(i+1)*(i+2); sum=sum+term;} printf(“sum=%ldn”,sum);} 6.2(4) #include term=1.0/sign; sum=sum+term; sign=sign+n; n++;} printf(“sum=%fn”,sum);} 6.2(5) #include term=-term*x*x/((n+1)*(n+2)); sum=sum+term; n=n+2; count++;}while(fabs(term)>=1e-5);printf(“sin(x)=%f,count=%dn”,sum,count);} 6.3 #include while(!find){ if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0) { printf(“x=%dn”,x); find=1; x++; } } } /* int x,find=0;for(x=1;!find;x++){ if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0) { printf(“x=%dn”,x); find=1; } } }*/ 6.4 #include p=i*i; m=i*i*i; printf(“p=%d,m=%dn”,i,p,i,m);} } 6.5 #include f=9/5*c+32; printf(“f=%fn”,f);} } 6.6 #include x=x*pow(1+c,12)-1000; n++;}while(x>0); printf(“x=%dn”,x);} 6.7 #include a=a*(1+c); n++;}while(a<=200); printf(“n=%dn”,n);} 6.8 #include term=pow(-1,count)*(1.0/(n+2)); sum=sum+term; n=n+2; count++;} sum=4*sum;printf(“sum=%f,count=%dn”,sum,count);} 6.9 #include term=term*(1.0/n); sum=sum+term; n++; count++;} printf(“sum=%f,count=%dn”,sum,count);} 6.10 #include if(x==pow(x/100,3)+pow(x/10%10,3)+pow(x%10,3)) printf(“x=%dn”,x);} } 6.11 #include i++; term=term*i; sum=sum+term;}while(sum for(i=1;i<=n;i++) { printf(“Inputm:”); scanf(“%d”,&m); if(m>0) { sum=sum+m; count++; } else { break; } printf(“sum=%d,count=%dn”,sum,count); } } 6.13 #include printf(“Inputm:”); scanf(“%d”,&m); if(m>0||m<0) { sum=sum+m; count++; } else { break; } printf(“sum=%d,count=%dn”,sum,count);} } 6.14 #include for(y=0;y<=25;y++) { 3*x+2*y+z==50; z=30-x-y; if(3*x+2*y+z==50&&x+y+z==30) printf(“x=%d,y=%d,z=%dn”,x,y,z); } } } 6.15 #include y=98-x; 2*x+4*y==386; if(x+y==98&&2*x+4*y==386) { printf(“x=%d,y=%dn”,x,y); } } } 6.16 #include for(y=0;y<=33;y++) { 3*y+5*x+z/3.0==100; z=100-x-y; if(5*x+3*y+z/3.0==100&&z+x+y==100) { printf(“x=%d,y=%d,z=%dn”,x,y,z); } } } } 6.17 #include for(y=1;y<=17;y++) { 10*x+5*y+z==100; z=50-x-y; if(10*x+5*y+z==100&&x+y+z==50&&z>0) { printf(“x=%d,y=%d,z=%dn”,x,y,z); } } } } 7.1 #include static int i=1; i+=Square(i); printf(“%d,”,i);} printf(“%dn”,i);return 0;} 7.2 #include second=0; minute++;} if(minute==60){ minute=0; hour++;} if(hour==24) hour=0;} void display(){ printf(“%d,%d,%dn”,hour,minute,second); } void delay(){ int t;for(t=0;t<100000000;t++);} int main(){ int i;void updaye(),display(),delay();for(i=0;i<1000000;i++){ update(); display(); delay();} return 0;} 7.3 #include return m;else return n;} 7.4 #include if(x%n==0&&x%m==0) { find=1; } } return x-1;} 7.5 #include printf(“%d!=%ldn”,a,Fact(a));} return 0;} long Fact(int n){ int i;long result=1;for(i=2;i<=n;i++) result*=i;return result;} 7.6 #include result*=i; sum=sum+result;} return sum;} 7.7(1)#include a%t==0; b%t==0; if(a%t==0&&b%t==0) return t;} find=1;} 7.7(2) #include return b;else do{ temp=b; b=r; a=temp; r=a%b; }while(r!=0); return b;} 8.1(1)#include #include b[j]=j;} } int main(){ static int a[]={5,6,7,8},i;Func(a);for(i=0;i<4;i++){ printf(“%d”,a[i]);} return 0;} 8.2(1) int PositiveNum(int a[],int n){ int i,count=0;for(i=0;i if(a[i]>0) count++;} return 0;} 8.2(2) void Fib(long f[],long n){ int i;f[0]=0;f[1]=1;for(i=2;i f[i]=f[i-1]+f[i-2];} } 8.2(3) #include scanf(“%d”,&a[n]);} max=min=a[0];maxPos=minPos=0;for(n=0;n<10;n++){ if(a[n]>max) { max=a[n]; maxPos=n; } else if(a[n] { min=a[n]; minPos=n; } } printf(“max=%d,pos=%dn”,max,maxPos);printf(“min=%d,pos=%dn”,min,minPos);return 0;} 8.3 void DivArray(int *pArray,int n){ int i;for(i=0;i pArray[i]/=pAttay[0];} } 8.4 #include if(score[i]<60) count++;} return count;} void ReadScore(int score[],int n){ int i;printf(“Input score:”);for(i=0;i scanf(“%d”,&score[i]);} } 8.5 #include sum=sum+score[i]; aver=sum/n;} for(i=0;i if(score[i]>aver) count++;} return count;} int ReadScore(int score[]){ int i=-1;do{ i++; printf(“Input score:”); scanf(“%d”,&score[i]);}while(score[i]>=0);return i;} 8.6 #include i++; printf(“Input student's ID and score:”); scanf(“%ld%d”,&num[i],&score[i]);}while(num[i]>0&&score[i]>=0);return i;} int FindMax(int score[],long num[],int n){ int max,i;max=score[0];for(i=1;i if(score[i]>max) max=score[i];} return i;} 8.7 #include printf(“Input a:”); scanf(“%d”,&a[i]);return i;} int ChangeArry(int a[],int n){ int max,min,i,maxpos,minpos,term;max=min=score[0];for(i=1;i if(a[i]>max) max=score[i]; maxpos=n; else(a[i] min=a[i]; minpos=n; temp=maxpos; maxpos=minpos; minpos=temp;} return a[i];} 8.10 #include int a[N][N];int sum = 0;int i, j; printf(“请输入一个%d*%d的矩阵n”,N,N);for(i = 0;i < N;i++){ for(j = 0;j < N;j++) { scanf(“%d”,&a[i][j]); } } for(i = 0;i < N;i++){ for(j = 0;j < N;j++) { if(i == j || i + j == N-1) sum=sum+a[i][j]; } } printf(“对角线元素之和为:%dn”,sum);} 8.11 #include for(j = 0;j< M;j++) { scanf(“%d”,&a[i][j]); sum1=sum1+a[i][j]; } } printf(“请输入一个%d*%d的矩阵n”,N,M);for(i = 0;i for(j = 0;j { scanf(“%d”,&b[i][j]); sum2=sum2+a[i][j]; } } sum=sum1+sum2;printf(“sum=%dn”,sum);} 8.12 #include for(j=0;j<=i;j++) { printf(“%4d”,b[i][j]); } printf(“n”);} } void YangHui(int a[N][N],int n){ int i,j;for(i=0;i a[i][0]=1; a[i][i]=1;} for(i=2;i for(j=1;j a[i][j]=a[i-1][j-1]+a[i-1][j];} } 8.13 #include f[i]=f[i-1]+f[i-2]; printf(“%d:%dn”,i+1,f[i]);} } 8.14 #include b1++;if(a[i]==2) b2++;if(a[i]==3) b3++;if(a[i]==4) b4++;if(a[i]==5) b5++;if(a[i]==6) b6++;p1=(float)(b1/6000.0);p2=(float)(b2/6000.0);p3=(float)(b3/6000.0);p4=(float)(b4/6000.0);p5=(float)(b5/6000.0);p6=(float)(b6/6000.0);} printf(“骰子出现1的概率:%.2fn”,p1);printf(“骰子出现2的概率:%.2fn”,p2);printf(“骰子出现3的概率:%.2fn”,p3);printf(“骰子出现4的概率:%.2fn”,p4);printf(“骰子出现5的概率:%.2fn”,p5);printf(“骰子出现6的概率:%.2fn”,p6);} 9.1(1) void Swap(int *x,int *y){ int *pTemp;//*pTemp未进行初始化,*pTemp指向哪里位置 *pTemp=*x;*x=*y;*y=*pTemp;} 9.1(2) void Swap(int *x,int *y){ int *pTemp;pTemp=x;x=y;y=pTemp;} //错误,x,y没有指向类型 9.2 #include printf(“ %dn”,a[i]);} for(i=0;i<6;i++){ printf(“%dn”,b[i]);} for(i=0;i<6;i++){ Swap(&a[i],&b[i]); } printf(“After Array n”);for(i=0;i<6;i++) { printf(“%dn”,a[i]); } for(i=0;i<6;i++) { printf(“%dn”,b[i]); } return 0;} void Swap(int *x,int *y){ int temp; temp=*x; *x=*y; *y=temp;} 9.4 #include scanf(“%d”,&a[i]);} FindMaxMin(a,&maxa,&maxnum,&mina,&minnum);printf(“maxa=%d,maxnum=%d,mina=%d,minnum=%dn”,maxa,maxnum,mina,minnum);return 0;} void FindMaxMin(int a[],int *pMaxa,int *pMaxnum,int *pMina,int *pMinnum){ int i;*pMaxa=a[0];*pMina=a[0];*pMaxnum=0;*pMinnum=0;for(i=0;i<10;i++){ if(a[i]>*pMaxa) { *pMaxa=a[i]; *pMaxnum=i; } else if(a[i]<*pMina) { *pMina=a[i]; *pMinnum=i; } } } 10.1 void MYStrcpy(char *dstStr,const char *srcStr){ while((*dstStr++=*srcStr++)!=' '){ } } 10.2 #include len++;} return len;} 10.3(2) unsigned int MyStrlen(char s[]){ char *p=s;while(*p!=' '){ p++;} return(p-*p);} 10.3(3)int MyStrcmp(char *p1,char *p2){ for(;*p1==*p2;p1++,p2++){ if(*p1==' ') return-1;} return 1;} 10.3(4)int MyStrcmp(char s[],char t[]){ int i;for(i=0;s[i]==t[i];i++){ if(s[i]==' ')return 0;} return i;} 10.3(5) #include printf(“Coorrect password!Welcome to the system...n”);else if(strcmp(userInput,password)<0) printf(“Invalid password!user input printf(”Invalid password!user input>password...n“);return 0;} 10.4 #include if(*p!=' ') flag=1; else if(flag==1) { c++; flag=0; } p++;} return c+1;} 12.1 #include #include gets(name[i]); if(strcmp(name[i],stu[0].peopleName)==0) sum1++; else if(strcmp(name[i],stu[1].peopleName)==0) sum2++; else if(strcmp(name[i],stu[2].peopleName)==0) sum3++; else sum4++;} printf(”stu:%4s%2dn“,stu[i].peopleName,sum[j]); printf(”stu:%4s%2dn“,stu[1].peopleName,sum2);printf(”stu:%4s%2dn“,stu[2].peopleName,sum3);printf(”%2dn",sum4);return 0;} 第五章 5.1 #include printf(“%fn”,x);} else { printf(“%fn”,-x);} } 5.2 #include printf(“%d为偶数n”,a);} else { printf(“%d为奇数n”,a);} } 5.3 #include scanf(“%f,%f,%f”,&a,&b,&c);s=(a+b+c)/2;area=(float)sqrt(s*(s-a)*(s-b)*(s-c));if((a+b>c)&&(a+c>b)&&(b+c>a)){ printf(“这个三角形的面积为%fn”,area);} else { printf(“这三条边不能组成三角形n”);} } 5.4 #include double a,b,c,d,e,x1,x2; printf(“请输入a,b,c:”); scanf(“%lf,%lf,%lf”,&a,&b,&c); if(a==0) { printf(“该方程不是一元二次方程.n”); } else if(b*b-4*a*c>0) { d=sqrt(b*b-4*a*c); x1=(-b+d)/(2*a); x2=(-b-d)/(2*a); printf(“x1=%f,x2=%fn”,x1,x2); } else if(b*b-4*a*c==0) { x1=(-b)/(2*a); printf(“x1=x2=%fn”,x1); } else if(b*b-4*a*c<0) { printf(“该方程没有实数解.n”); } } 5.5 #include flag=1;} else { flag=0;} if(flag==1){ printf(“%d是闰年n”,year);} else { printf(“%d不是闰年n”,year);} } 5.6 #include if(flag==1){ printf(“%d年是闰年n”,year);} else { printf(“%d年不是闰年n”,year);} } 5.7 #include ch=ch+32; printf(“%c,%dn”,ch,ch);} else if((ch>=97)&&(ch<=122)){ ch=ch-32; printf(“%c,%dn”,ch,ch);} else { printf(“%cn”,ch);} } 5.8 #include printf(“请输入一个字符:”);ch=getchar();if((ch>=48)&&(ch<=57)){ printf(“这是一个数字字符.n”);} else if((ch>=65)&&(ch<=90)){ printf(“这是一个大写字母.n”);} else if((ch>=97)&&(ch<=122)){ printf(“这是一个小写字母.n”);} else if(ch==32){ printf(“这是一个空格字符.n”);} else { printf(“这是一个其它字符.n”);} } 5.9 #include if(score<0||score>100){ printf(“Input error!n”);} else if(score>=90&&score<=100){ printf(“%d--An”,score);} else if(score>=80&&score<90){ printf(“%d--Bn”,score);} else if(score>=70&&score<80){ printf(“%d--Cn”,score);} else if(score>=60&&score<70){ printf(“%d--Dn”,score);} else { printf(“%d--En”,score);} } 5.10 #include printf(“你输入的年份不在合法范围内n”); exit(0);} else { flag=(year%4==0 && year%100!=0||year% 400 ==0)?1:0;switch(flag){ case 1: printf(“闰年”); break;case 0: printf(“平年”); break;} if(month==2&&flag==1)day=day3;else if(month==2&&flag==0)day=day4;switch(month){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: printf(“%d年,%d月拥有的天数是31.n”,year,month); break; case 4: case 6: case 9: case 11: printf(“%d年,%d月拥有的天数是30.n”,year,month); break; default: printf(“%d年,%d月拥有的天数是%d: n”,year,month,day);} } } 本章实验题1 #include double faheight,maheight;char sex,sports,diet,F,M,Y,N;printf(“请输入父母的身高:n”);scanf(“%lf,%lf”,&faheight,&maheight); printf(“请输入孩子是男孩还是女孩(F/M):n”);scanf(“ %c”,&sex);printf(“请输入孩子是否喜欢锻炼身体(Y/N):n”);scanf(“ %c”,&sports);printf(“请输入孩子是否有良好的饮食习惯(Y/N):n”);scanf(“ %c”,&diet);if(sex==77&&sports==89&&diet==89){ printf(“该孩子成人时身高约为:%.2f.n”,(faheight+maheight)*0.54*1.02*1.015);} else if(sex==77&&sports==89&&diet==78){ printf(“该孩子成人时身高约为:%.2f.n”,(faheight+maheight)*0.54*1.02);} else if(sex==77&&sports==78&&diet==89){ printf(“该孩子成人时身高约为:%.2f.n”,(faheight+maheight)*0.54*1.015);} else if(sex==77&&sports==78&&diet==78){ printf(“该孩子成人时身高约为:%.2f.n”,(faheight+maheight)*0.54);} else if(sex==70&&sports==89&&diet==89){ printf(“该孩子成人时身高约为:%.2f.n”,(faheight*0.923+maheight)/2*1.02*1.015);} else if(sex==70&&sports==89&&diet==78){ printf(“该孩子成人时身高约为:%.2f.n”,(faheight*0.923+maheight)/2*1.02);} else if(sex==70&&sports==78&&diet==89){ printf(“该孩子成人时身高约为:%.2f.n”,(faheight*0.923+maheight)/2*1.015);} else if(sex==70&&sports==78&&diet==78){ printf(“该孩子成人时身高约为:%.2f.n”,(faheight*0.923+maheight)/2);} } 本章实验题2 #include printf(“请输入体重身高:n”);scanf(“%lf,%lf”,&w,&h);t=w/(h*h);if(t<18){ printf(“低体重.n”);} if(t>=18&&t<25){ } printf(“正常体重.n”);} if(t>=25&&t<27){ printf(“超重体重.n”);} if(t>=27){ printf(“肥胖.n”);} 第六章 6.1(1)#include for(j=1;j<=i;j++) { printf(“%c”,space); } for(k=1;k<=6;k++) { printf(“*”); } printf(“n”);} } 6.1(2)#include if(n%2==0)continue; k--;} printf(“k=%dn,n=%dn”,k,n);} 6.1(3)#include if(n%2 ==0)break; k--;} printf(“k=%d,n=%dn”,k,n);} 6.2(1)#include sum=sum+i;} printf(“sum = %dn”,sum);} 6.2(2)#include term=i*(i+1)*(i+2); sum=sum+term;} printf(“sum =%ldn”,sum);} 6.2(3)#include term = term * 10 + a; sum=sum+term;} printf(“sum = %ldn”,sum);} 6.2(4)#include term = sign / n; sum = sum + term; sign =-sign; n++;} printf(“sum = %fn”);} 6.2(5)#include n = 1,count = 1;float x;double sum,term;printf(“Input x:”);scanf(“%f”,&x);sum = x;term = x;do{ term =-term*x*x/((n+1)*(n+2)); sum = sum + term; n = n + 2; count++;}while(fabs(term)>=1e-5);printf(“sin(x)= %f,coumt = %d n”,sum,count);} 6.3 #include if(x%2==1 && x%3==2 && x%5==4 && x%6==5 && x%7==0) { find=1; } x++;} printf(“x=%dn”,x-1);} 6.4 #include p=i*i; j=i*i*i; printf(“%d * %d = %ldn”,i,i,p); printf(“%d * %d * %d= %ldn”,i,i,i,j);} } 6.5 #include C =(F-32)*5/9; printf(“F=%f C=%fn”,F,C);} } 6.6 #include sum = sum *(1+d); count++;}while(sum <= 200);printf(“count = %fn”,count);} 6.8 #include term =-i/n; sum = sum + term;n = n + 2;i =-i;count++;}while(fabs(term)>= 1e-5);printf(“∏ = %lf,count = %d.n”,4 * sum,count);} 6.9 #include term = term / n; sum = sum + term; n++; count++;} printf(“e = %lf,count = %d.n”,sum,count);} 6.10 #include a = i /100; c = i % 10; b =(ic)/ 10; d = a * a * a + b * b * b + c * c * c; if(i == d) { printf(“i = %dn”,i); } } } 6.11 #include term = term * m; sum = sum + term; m++;}while(sum <= n);printf(“m = %d.n”,m1);} 6.13 #include printf(“Input term : ”); scanf(“%d”,&term); if(term > 0) { sum = sum + term; count++; } else if(term < 0) goto begin; else goto end;}while(term!= 0);end : printf(“sum = %d.count = %d.n”,sum,count);} 6.14 #include for(b = 0;b <= 25;b++) { c = 30b; if(a * 3 + b * 2 + c == 50) printf(“男 = %d,女 = %d,孩子 = %d.n”,a,b,c); } } 6.15 #include b = 98xx-y; if(x * 10 + y * 5 + z == 100) printf(“x = %d, y = %d, z = %dn”,x ,y ,z); } } } 6.18 上: #include for(j = 1;j <= 9;j++) { printf(“%-5d”,i * j); } printf(“n”);} } 左: #include for(j = 1;j <= i;j++) { printf(“%-5d”,i * j); } printf(“n”);} } 右: #include for(i = 1;i <= 9;i++){ for(k = 1;k <= i;k++) { printf(“%5c”,b); } for(j = i;j <= 9;j++) { a=j * i; printf(“%5d”,a); } printf(“n”);} } 6.19 #include term = term * 2.0; sumfu = sumfu + term; } printf(“陌生人付%ld,富翁付%lf.n”,summo,sumfu);} 本章实验题 2.1 #include printf(“%d * %d = n”,x1, x2); scanf(“%d”,&answer); if(c == answer) { printf(“Right!n”); goto begin; } else { printf(“Wrrong!Please try again.n”); } }while(c!= answer);} 2.2 #include printf(“%d * %d = n”,x1, x2); scanf(“%d”,&answer); if(c == answer) { printf(“Right!n”); goto begin; } else { printf(“Wrrong!Please try again.n”); count++; } }while((c!= answer)&&(count <= 2));printf(“Wrong!You have tried three times!Test over!n”);} 2.3 #include for(i = 1;i <= 10;i++){ srand(time(NULL));x1 = rand()% 10 + 1; x2 = rand()% 10 + 1; c = x1 * x2; printf(“%d * %d = n”,x1 , x2); scanf(“%d”,&answer); if(c == answer) { printf(“Right!n”); count1++; } else { printf(“Wrong!n”); count2++; } } printf(“你得了%d分.n正确率为%d %%.n”,count1,count1 *10);} 数据库原理之关系数据库SQL语言课后习题及答案 http://www.xiexiebang.comAME,TEACHER) 试用SQL的查询语句表达下列查询: (1)检索LIU老师所授课程的课程号和课程名。 SELECT C#,CNAME FROM C WHERE TEACHER=„LIU‟ (2)检索年龄大于23岁的男学生的学号和姓名。 SELECT S#,SNAME FROM S WHERE(AGE>23)AND(SEX=„M‟) (3)检索至少选修LIU老师所授课程中一门课程的女学生姓名。 SELECT SNAME FROM S WHERE SEX=„F‟ AND S# IN (SELECT S# FROM SC WHERE C# IN (SELECT C# FROM C WHERE TEACHER=„LIU‟) NOTICE:有多种写法,比如联接查询写法: SELECT SNAME FROM S,SC,C WHERE SEX=„F‟ AND SC.S#=S.S# AND SC.C#=C.C# AND TEACHER='LIU' 但上一种写法更好一些。 (4)检索WANG同学不学的课程的课程号。 SELECT C# FROM C WHERE C# NOT IN (SELECT C# FROM SC WHERE S# IN (SELECT S# FROM S WHERE SNAME='WANG')) (5)检索至少选修两门课程的学生学号。 SELECT DISTINCT X.SNO FROM SC X,SC Y WHERE X.SNO=Y.SNO AND X.CNO<>Y.CNO Notice:对表SC进行自连接,X,Y是SC的两个别名。 (6)检索全部学生都选修的课程的课程号与课程名。 SELECT C#,CNAME FROM C WHERE NOT EXISTS (SELECT * FROM S WHERE S# NOT IN (SELECT * FROM SC WHERE SC.C#=C.C#)) 要从语义上分解:(1)选择课程的课程号与课程名,不存在不选这门课的同学。 其中,“不选这门课的同学”可以表示为: 或者 SELECT * FROM S WHERE NOT EXISTS (SELECT * FROM SC WHERE S.S#=C.S# AND SC.C#=C.C#) SELECT * FROM S WHERE S# NOT IN (SELECT * FROM SC WHERE SC.C#=C.C#) (7)检索选修课程包含LIU老师所授课的学生学号。 SELECT DISTINCT S# FROM SC WHERE C# IN (SELECT C# FROM C WHERE TEACHER='LIU')) 3.3 设有两个基本表R(A,B,C)和S(D,E,F),试用SQL查询语句表达下列关系代数表达式: (1)πA(R) (2)σB='17'(R) (3)R×S (4))πA,F(σC=D(R×S)) (1)SELECT A FROM R (2)SELECT * FROM R WHERE B='17'(3)SELECT A,B,C,D,E,F FROM R,S (4)SELECT A,F FROM R,S WHERE R.C=S.D 3.4 3.4 设有两个基本表R(A,B,C)和S(A,B,C)试用SQL查询语句表达下列关系代数表达式: (1)R∪S (2)R∩S (3)R-S (4)πA,B(R)πB,C(S) (1)SELECT A,B,C FROM R UNION SELECT A,B,C FROM S (2)SELECT A,B,C FROM R INTERSECT SELECT A,B,C FROM S (3)SELECT A,B,C FROM R WHERE NOT EXISTS (SELECT A,B,C FROM S WHERE R.A=S.A AND R.B=S.B AND R.C=S.C) (4)SELECT R.A,R.B,S.C FROM R,S WHERE R.B=S.B 3.5 试叙述SQL语言的关系代数特点和元组演算特点。 (P61-62) 3.6 试用SQL查询语句表达下列对教学数据库中三个基本表S、SC、C的查询: (1)统计有学生选修的课程门数。 SELECT COUNT(DISTINCT C#)FROM SC (2)求选修C4课程的学生的平均年龄。 SELECT AVG(AGE) FROM S WHERE S# IN (SELECT S# FROM SC WHERE C#='C4')或者,SELECT AVG(AGE) FROM S,SC WHERE S.S#=SC.S# AND C#='004' (3)求LIU老师所授课程的每门课程的学生平均成绩。 SELECT CNAME,AVG(GRADE) FROM SC ,C WHERE SC.C#=C.C# AND TEACHER='LIU' GROUP BY C# (4)统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列。 SELECT DISTINCT C#,COUNT(S#) FROM SC GROUP BY C# HAVING COUNT(S#)>10 ORDER BY 2 DESC, C# ASC (5)检索学号比WANG同学大,而年龄比他小的学生姓名。 SELECT X.SNAME FROM S AS X, S AS Y WHERE Y.SNAME='WANG' AND X.S#>Y.S# AND X.AGE (6)检索姓名以WANG打头的所有学生的姓名和年龄。 SELECT SNAME,AGE FROM S WHERE SNAME LIKE 'WANG%' (7)在SC中检索成绩为空值的学生学号和课程号。 SELECT S#,C# FROM SC WHERE GRADE IS NULL (8)求年龄大于女同学平均年龄的男学生姓名和年龄。 SELECT SNAME,AGE FROM S AS X WHERE X.SEX='男' AND X.AGE>(SELECT AVG(AGE)FROM S AS Y WHERE Y.SEX='女') (9)求年龄大于所有女同学年龄的男学生姓名和年龄。 SELECT SNAME,AGE FROM S AS X WHERE X.SEX='男' AND X.AGE>ALL(SELECT AGE FROM S AS Y WHERE Y.SEX='女') 3.7 试用SQL更新语句表达对教学数据库中三个基本表S、SC、C的各个更新操作: (1)往基本表S中插入一个学生元组(„S9‟,„WU‟,18)。 INSERT INTO S(S#,SNAME,AGE)VALUES('59','WU',18) (2)在基本表S中检索每一门课程成绩都大于等于80分的学生学号、姓名和性别,并把检索到的值送往另一个已存在的基本表STUDENT(S#,SANME,SEX)。 INSERT INTO STUDENT(S#,SNAME,SEX) SELECT S#,SNAME,SEX FROM S WHERE NOT EXISTS (SELECT * FROM SC WHERE GRADE<80 AND S.S#=SC.S#) (3)在基本表SC中删除尚无成绩的选课元组。 DELETE FROM SC WHERE GRADE IS NULL (4)把WANG同学的学习选课和成绩全部删去。 DELETE FROM SC WHERE S# IN (SELECT S# FROM S WHERE SNAME='WANG') (5)把选修MATHS课不及格的成绩全改为空值。 UPDATE SC SET GRADE=NULL WHERE GRADE<60 AND C# IN (SELECT C# FROM C WHERE CNAME='MATHS')(6)把低于总平均成绩的女同学成绩提高5%。 UPDATE SC SET GRADE=GRADE*1.05 WHERE GRADE<(SELECT AVG(GRADE)FROM SC)AND S# IN(SELECT S# FROM S WHERE SEX='F') (7)在基本表SC中修改C4课程的成绩,若成绩小于等于75分时提高5%,若成绩大于75分时提高4%(用两个UPDATE语句实现)。 UPDATE SC SET GRADE=GRADE*1.05 WHERE C#='C4' AND GRADE<=75 UPDATE SC SET GRADE=GRADE*1.04 WHERE C#='C4' AND GRADE>75 3.8 在第1章例1.4中提到“仓库管理”关系模型有五个关系模式: 零件 PART(P#,PNAME,COLOR,WEIGHT) 项目 PROJECT(J#,JNAME,DATE) 供应商 SUPPLIER(S#,SNAME,SADDR) 供应 P_P(J#,P#,TOTOAL) 采购 P_S(P#,S#,QUANTITY) (1)试用SQL DDL语句定义上述五个基本表,并说明主键和外键。 CREATE TABLE PART (P# CHAR(4)NOT NULL,PNAME CHAR(12)NOT NULL,COLOR CHAR(10),WEIGHT REAL,PRIMARY KEY(P#)) CREATE TABLE PROJECT (J# CHAR(4)NOT NULL,JNAME CHAR(12)NOT NULL,DATE DATE,PRIMARY KEY(J#)) CREATE TABLE SUPLIER (S# CHAR(4)NOT NULL,SNAME CHAR(12),SADDR VARCHAR(20),PRIMARY KEY(S#)) CREATE TABLE P_P (J# CHAR(4),P# CHAR(4),TOTAL INTEGER,PRIMARY KEY(J#,P#),FOREIGN KEY(J#)REFERENCE PROJECT(J#),FOREIGN KEY(P#)REFERENCE PART(P#)) CREATE TABLE P_S (P# CHAR(4),S# CHAR(4),QUANTITY INTEGER,PRIMARY KEY(P#,S#),FOREIGN KEY(P#)REFERENCE PART(P#),FOREIGN KEY(S#)REFERENCE SUPLIER(S#)) (2)试将PROGECT、P_P、PART三个基本表的自然联接定义为一个视图VIEW1,PART、P_S、SUPPLIER 三个基本表的自然联接定义为一个视图VIEW2。 CREATE VIEW VIEW1(J#,JNAME,DATE,P#,PNAME,COLOR,WEIGHT,TOTAL) AS SELECT PROJECT.J#,JNAME,DATE,PART.P#,PNAME,COLOR,WEIGHT,TOTAL FROM PROJECT,PART,P_P WHERE PART.P#=P_P.P# AND P_P.J#=PROJECT.J # CREATE VIEW VIEW2(P#,PNAME,COLOR,WEIGHT,S#,SNAME,SADDR,QUANTITY) AS SELECT PART.P#,PNAME,COLOR,WEIGHT,SUPPLIER.S#,SNAME,SADDR,QUANTITY FROM PART,P_S,SUPPLIER WHERE PART.P#=P_S.P# AND P_S.S#=SUPPLIER.S# (3)试在上述两个视图的基础上进行数据查询: 1)检索上海的供应商所供应的零件的编号和名字。 SELECT P#,PNAME FROM VIEW2 WHERE SADDR='SHANGHAI' 2)检索项目J4所用零件的供应商编号和名字。 SELECT S#,SNAME FROM VIEW2 WHERE P# IN(SELECT P# FROM VIEW1 WHERE J#='J4') 3.9 对于教学数据库中基本表SC,已建立下列视图: CREATE VIEW S_GRADE(S#,C_NUM,AVG_GRADE) AS SELECT S#,COUNT(C#),AVG(GRADE) FROM SC GROUP BY S# 试判断下列查询和更新是否允许执行。若允许,写出转换到基本表SC上的相应操作。 (1)SELECT * FROM S_GRADE 允许 SELECT S#,COUNT(C#),AVG(GRADE)FROM SC GROUP BY S# (2)SELECT S#,C_NUM FROM S_GRADE WHERE AVG_GRADE>80 允许 SELECT S#,COUNT(C#)FROM SC WHERE AVG(GRADE)>80 (3)SELECT S#,AVG_GRADE FROM S_GRADE WHERE C_NUM>(SELECT C_NUM FROM S_GRADE WHERE S#=„S4‟) 允许 SELECT S#,AVG(GRADE) FROM SC AS X WHERE COUNT(X.C#)>(SELECT COUNT(Y.C#)FROM SC AS Y WHERE Y.S#='S4') GROUP BY S# (4)UPDATE S_GRADE SET C_NUM=C_NUM+1 WHERE S#=„S4‟ 不允许 (5)DELETE FROM S_GRADE WHERE C_NUM> 4不允许 3.10 预处理方式对于嵌入式SQL的实现有什么重要意义? 预处理方式是先用预处理程序对源程序进行扫描,识别出SQL语句,并处理成宿主语言的函数调用形式; 然后再用宿主语言的编译程序把源程序编译成目标程序。这样,不用扩充宿主语言的编译程序,就能处理SQL语句。 3.11 在宿主语言的程序中使用SQL语句有哪些规定? 在宿主语言的程序中使用SLQ语句有以下规定: (1)在程序中要区分SQL语句与宿主语言语句 (2)允许嵌入的SQL语句引用宿主语言的程序变量(称为共享变量),但有两条规定: 1)引用时,这些变量前必须加“:”作为前缀标识,以示与数据库中变量有区别。 2)这些变量由宿主语言的程序定义,并用SQL的DECLARE语句说明。 (3)SQL的集合处理方式与宿主语言单记录处理方式之间要协调。需要采用游标机制,把集合操作转换成单记录处理方式。 3.12 SQL的集合处理方式与宿主语言单记录处理方式之间如何协调? 由于SQL语句处理的是记录集合,而宿主语言语句一次只能处理一个记录,因此需要用游标(cousor)机制,把集合操作转换成单记录处理方式。 2.13 嵌入式SQL语句何时不必涉及到游标?何时必须涉及到游标? (1)INSERT、DELETE、UPDATE语句,查询结果肯定是单元组时的SELECT语句,都可直接嵌入在主程序中使用,不必涉及到游标。 (2)当SELECT语句查询结果是多个元组时,此时宿主语言程序无法使用,一定要用游标机制把多个元组一次一个地传送给宿主语言处理。 阅读下面的语段,完成后面的练习,大自然的语言习题精选。 立春过后,大地渐渐地沉睡中_______(A.清醒B.苏醒;C.醒悟;D.复苏)过来。冰雪融化,草木萌发,各种花_________(A.次第;B.连续;C.陆续;D.全部)开放。再过两个月,燕子翩然归来。不久,布谷鸟也来了。于是转入炎热的夏季,这是植物孕育果实的时期。到了秋天,果实成熟,植物的叶子渐渐变黄,在秋风中_________(A.纷纷;B.刷刷;C.簌簌)的落下来。北雁南飞,活跃在田间草际的昆虫也都销声匿迹。到处呈现一片衰草连天的景象,准备迎接风雪载途寒冬。在地球上温带和亚热带区域里,年年如是,周而复始。 几千年来,劳动人民注意了草木荣枯、候鸟去来等自然现象同气候的关系,据以安排农事,语文试题《大自然的语言习题精选》。杏花开了,就好像大自然在传语要赶快耕地;桃花开了,又好像在暗示要赶快种谷子。布谷鸟开始唱歌,劳动人民懂得它在唱什么:“阿公阿婆,割去播禾。”这#者来,花香鸟语,草长莺飞,都是大自然的语言。 1.在语段中横线上填上恰当的词语。 2.解释词语:销声匿迹翩然 3.这两段文字的说明对象是() A.物候现象;B.气候变化;C.物候规律;D.四季景色变化。 4.这两段文字的说明顺序是:____________________________,主要的说明方法是:_____________________________ 5.去掉“温带和亚热带区域里”这几个词语行不行,为什么? 参考答案: 1.B A C 2.销声匿迹:这里指消失了鸣叫声,藏起了形迹。翩然:动作轻快的样子。 3.A 4.时间顺序;举例子 5.不行。以上情况只限于“地球上温带和亚热带区域里”。第二篇:C语言程序设计苏小红版答案
第三篇:C语言程序设计苏小红版第五六单元答案
第四篇:SQL语言习题及答案
第五篇:大自然的语言习题及答案