湘潭大学c语言课后习题答案

合集下载

C语言课后题编程标准答案(可编辑修改word版)

C语言课后题编程标准答案(可编辑修改word版)

C 语言课后题编程答案所有章节的课后习题的选择题和填空题大家必须熟练掌握,编程题掌握到第九章即可。

希望大家通过课后的编程题熟悉典型的编程算法,掌握基本的编程思路,注意编程细节。

第3 章3-17编写程序,把560 分钟换算成用小时和分钟表示,然后进行输出。

#include<stdio.h>main(){int h,m。

h=560/60。

m=560%60。

printf(“560 分钟可转换成%d 小时%d 分钟”,h,m)。

}程序总结:(1)只要在程序中用到系统提供的库函数,必须把库函数所在的头文件用#include 命令包含进来。

否则库函数的使用无效。

输入输出库函数的头文件是:stdio.h。

数学函数的头文件是:math.h。

字符处理函数的头文件是:ctype.h。

字符串处理函数的头文件是:string.h。

(2)注意C 语言中的“/”和“%”两种运算符。

“/”的运算结果取决于两操作数的类型。

比如:1/2=0(与数学中不同,结果与操作数的类型一致,所以结果只取商值),1.0/2=0.5(除之前2 要自动类型转换成2.0,因为只有同类型的操作数才能进行除运算), 1/2.0=0.5, 1.0/2.0=0.5。

“%”的两个操作数都必须是整数。

(3)printf(“560分钟可转换成%d 小时%d 分钟”,h,m)。

printf 第一个参数要尽量详细,这样可以提高输出结果的可读性,恰当好处的添加提示性信息,可以提高程序的质量。

(4)i nt h,m。

变量起名要有艺术性,尽量做到见名知意。

3-18编写程序,输入两个整数,1500 和350,求出它们的商和余数并进行输出。

#include<stdio.h>main(){int a,b,m,n。

printf(“请输入两个整数:\n”)。

scanf(“%d%d”,&a,&b)。

m=a/b。

n=a%b。

printf(“%d 除以%d 商%d 余%d”,a,b,m,n)。

c语言课后习题答案

c语言课后习题答案

c语言课后习题答案C语言课后习题答案C语言作为一门广泛应用于编程领域的高级编程语言,对于学习者来说是必不可少的一门课程。

在学习过程中,课后习题是巩固知识、提高编程能力的重要环节。

然而,有时候我们可能会遇到一些难以解答的问题,或者对于某些题目的解答存在疑惑。

在这篇文章中,我将为大家提供一些C语言课后习题的答案,并解释一些常见问题。

1. 编写一个程序,从键盘输入一个整数,判断该数是否为素数。

解答:```c#include <stdio.h>int isPrime(int num) {if (num <= 1) {return 0;}for (int i = 2; i * i <= num; i++) {if (num % i == 0) {return 0;}}return 1;}int main() {int num;printf("请输入一个整数:");scanf("%d", &num);if (isPrime(num)) {printf("%d是素数。

\n", num);} else {printf("%d不是素数。

\n", num);}return 0;}```以上代码中,我们定义了一个函数`isPrime`,用于判断一个数是否为素数。

在`main`函数中,我们从键盘输入一个整数,并调用`isPrime`函数进行判断。

如果返回值为1,则说明该数是素数,否则不是素数。

2. 编写一个程序,计算并输出1到100之间所有能被3整除但不能被5整除的数之和。

解答:```c#include <stdio.h>int main() {int sum = 0;for (int i = 1; i <= 100; i++) {if (i % 3 == 0 && i % 5 != 0) {sum += i;}}printf("1到100之间所有能被3整除但不能被5整除的数之和为:%d\n", sum);return 0;}```以上代码中,我们使用`for`循环遍历1到100之间的所有整数,使用条件判断语句`if`筛选出能被3整除但不能被5整除的数,并将它们累加到`sum`变量中。

湘潭大学C语言考试部分答案

湘潭大学C语言考试部分答案

答案:1031,1032,1034-1044,1047-1052,1054-1059 共25题1031小明的数学题Ⅰ#include<iostream>using namespace std;__int64 e;__int64 a[950];void cal(__int64 k){__int64 i;for(i=0;i<e;i++) a[i]*=k;for(i=0;i<e+20;i++) { a[i+1]+=a[i]/10; a[i]=a[i]%10; }for(i=e+20;i>=0;i--) if(a[i]!=0) break;e=i+1;}int main(){int cases;__int64 aa,n,i;scanf("%d",&cases);while(cases--){scanf("%I64d%I64d",&aa,&n);if(n==1) { printf("%I64d\n",aa); continue; }if(aa==1) { printf("1\n"); continue; }if(aa==0) { printf("0\n"); continue; }if(aa<0) { if(n%2==1) printf("-"); aa=-aa; }memset(a,0,sizeof(a));a[0]=1;e=1;for(i=1;i<=n;i++) cal(aa);for(i=e-1;i>=0;i--) printf("%I64d",a[i]); printf("\n");}return 0;}1032小明的数学题2#include<iostream>using namespace std;int e,a[400];void cal(int k){int i;for(i=0;i<e;i++) a[i]*=k;for(i=0;i<e+5;i++) { a[i+1]+=a[i]/10; a[i]=a[i]%10;}for(i=e+5;i>=0;i--) if(a[i]!=0) break;e=i+1; }int main(){int cases;int aa,n,i;scanf("%d",&cases);while(cases--){scanf("%d",&n);memset(a,0,sizeof(a));a[0]=1;e=1;for(i=1;i<=n;i++) cal(i);for(i=e-1;i>=0;i--) printf("%d",a[i]); printf("\n");}return 0;}1034小明的数学题Ⅳ#include<stdio.h>#include<string.h>char s1[2010],s2[2010],ss1[2010],ss2[2010];int a[4010],e,i,j;void calculate(int len1,int len2,char ss1[],char ss2[]) {e=0;memset(a,0,sizeof(a));for(i=0;i<len1;i++){for(j=0;j<len2;j++){a[i+j+1]+=(ss1[i]-'0')*(ss2[j]-'0');if(i+j+1>e) e=i+j+1;}}e+=5;for(i=1;i<=e;i++) { a[i+1]+=a[i]/10;a[i]=a[i]%10; } }int main(){int cases,len1,len2,dot1,dot2,i1,i2;scanf("%d",&cases);while(cases--){scanf("%s%s",s1,s2);len1=strlen(s1);len2=strlen(s2);dot1=0;dot2=0;i1=0;i2=0;if(s1[0]=='-') i1=1;for(i=i1;i<len1;i++){if(s1[i]!='.') continue;else { dot1=len1-1-i; for(j=i;j<len1-1;j++) s1[j]=s1[j+1]; len1--; break; }}for(i=len1-1;i>=i1;i--) ss1[len1-1-i]=s1[i]; if (i1==1) len1--;if(s2[0]=='-') i2=1;for(i=0;i<len2;i++){if(s2[i]!='.') continue;else { dot2=len2-1-i; for(j=i;j<len2-1;j++) s2[j]=s2[j+1]; len2--; break; }}for(i=len2-1;i>=0;i--) ss2[len2-1-i]=s2[i]; if( i2==1) len2--;if((i1+i2)%2) printf("-");calculate(len1,len2,ss1,ss2);// for(i=e;i>0;i--) printf("%d",a[i]); printf("\n");for(i=e;i>dot1+dot2;i--) if(a[i]==0) continue;else break;e=i; if(e==dot1+dot2) e=dot1+dot2+1;for(i=e;i>dot1+dot2;i--) printf("%d",a[i]); //c out<<endl; cout<<"dot1:"<<dot1<<"dot2:"<<dot2<<endl;if(dot1+dot2!=0){for(i=1;i<=dot1+dot2;i++) if(a[i]==0) continue;else break;e=i;if(e==dot1+dot2+1) e=dot1+dot2;if(!(e==dot1+dot2 && a[e]==0)) { printf(".");for(i=dot1+dot2;i>=e;i--) printf("%d",a[i]); }}printf("\n");}return 0;}1035列车长的烦恼#include<iostream>using namespace std;int a[110],stack[110],cases,n,i,p,top,aa;int main(){scanf("%d",&cases);while(cases--){scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d",&a[i]);top=0;p=1;aa=1;stack[0]=-1;while(p<n+1){stack[++top]=p;p++;while(stack[top]==a[aa]) { top--;aa++; } }if(top==0) printf("Yes\n");else printf("No\n");}return 0;}1036 远古文明的算术题#include<iostream>using namespace std;int change(char s[]){int sum,i;sum=s[0]-'0';i=1;while(s[i]!='\0') { sum=sum*10+s[i]-'0'; i++; } return sum;}char s[300];long stack[1000];int main(){int cases,sum,top;scanf("%d",&cases);while(cases--){scanf("%s",s);top=0;sum=change(s);stack[++top]=sum;while(getchar()!='\n'){scanf("%s",s);if(s[0]=='+') { stack[top-1]=stack[top-1]+s tack[top]; top--; }else if(s[0]=='-'){ stack[top-1]=stack[top -1]-stack[top];top--; }else if(s[0]=='*'){ stack[top-1]=stack[top -1]*stack[top];top--; }else if(s[0]=='/') { stack[top-1]=stack[top -1]/stack[top];top--; }else if(s[0]=='%') { stack[top-1]=stack[top -1]%stack[top];top--; }else{sum=change(s);stack[++top]=sum;}}printf("%ld\n",stack[top]);}return 0;}1037成对的字符串#include<iostream>#include<cstring>using namespace std;int main(){int cases,l,sum,i;char s[1000],a[300];cin>>cases;while(cases--){scanf("%s",s);l=strlen(s);if(l%2==1) {printf("No\n"); continue;}sum=0;memset(a,0,sizeof(a));for(i=0;i<l;i++){if(a[s[i]]==0) a[s[i]]=i+1;else{if((i-a[s[i]])%2==0) { sum+=2; a[s[i]]=0; } }}if(sum==l) printf("Yes\n"); else printf("No\n");}return 0;}1038括号编码#include<iostream>using namespace std;int a[1000],stack[1000],count[1000];char str[1010];int main(){int cases,n,t,i,j,s,sum,top;cin>>cases;while(cases--){cin>>n;s=0;a[0]=0;sum=0;for(i=1;i<=n;i++){cin>>a[i];t=a[i]-a[i-1];for(j=1;j<=t;j++){str[++s]='(';count[s]=sum;}str[++s]=')';sum++;count[s]=sum;}top=0;for(i=1;i<s;i++){if(str[i]=='(') stack[++top]=i;else printf("%d ",count[i]-count[stack[top --]]);}printf("%d\n",count[i]-count[stack[top--]]);}return 0;}1039恺撒的密码#include<iostream>using namespace std;char s[1010];int cases,i,k;int main(){cin>>cases;getchar();while(cases--){for(k=0;s[k-1]!='\n';k++)s[k]=getchar();k=k-1;i=0;while(i<k){if(s[i]>='F'&&s[i]<='Z') printf("%c",s[i]-5 );else if(s[i]>='A'&&s[i]<='E') printf("%c",s[i]+21);else printf("%c",s[i]);i++;}printf("\n");}return 0;}1040零件#include<iostream>using namespace std;int a[1010];char s1[30][30],s2[30][30];int main(){int k,i,j,n,maxn,sum,len,t;bool flag;scanf("%d",&k);while(k--){scanf("%d",&n);getchar();for(i=0;i<n;i++) gets(s1[i]);for(i=0;i<n;i++) gets(s2[i]);maxn=0;for(i=0;i<n;i++){a[i]=0; len=0;t=0;flag=true;j=0;while(s1[i][j]!='\0') { if(s1[i][j]=='X ') a[i]++; j++; len++; }j=0;while(s2[i][j]!='\0') { if(s2[i][j]==' '&&flag) t++; else flag=false; if(s2[i][j]=='X') a[i]++ ; j++; }len+=j-t;if(maxn<len) maxn=len;}// cout<<maxn<<endl;// for(i=0;i<n;i++) cout<<a[i]<<" ";cout<<endl;sum=maxn*n;for(i=0;i<n;i++) sum-=a[i];printf("%d\n",sum);}return 0;}1041狼群战术#include<iostream>using namespace std;char a[5][6]={{'Q','W','E','R','T'},{'Y','U','I','O','P '},{'A','S','D','F','G'},{'H','J','K','L','Z'},{'X','C' ,'B','N','M'}};char s[1010];int main(){int cases,i;//for(i=0;i<5;i++) cout<<a[i]<<endl;scanf("%d",&cases);getchar();while(cases--){gets(s);i=0;while(s[i]!='\0'){if(s[i]=='F') { printf("V"); i+=2; }else if(s[i]>='A'&&s[i]<='E') { printf("%c",a[s[i]-'A'][s[i+1]-'A']); i+=2; } else printf("%c",s[ i++]); }printf("\n");}return 0;}1042非前缀编码1、#include<iostream>using namespace std;char a[100][1000],s[100000];int check(char *a,char *b){int l1=strlen(a),l2=strlen(b),tag=0,i;for(i=0;i<l1&&i<l2;i++)if(a[i]!=b[i]) break;if(i>=l1||i>=l2) tag=1;else tag=0;return tag;}int main(){int i,n,x,j,p,t,tag,k;scanf("%d",&n);getchar();for(i=0;i<n;i++){gets(s);x=0;t=0;j=0;while(s[x]!='\0'){if(s[x]!=' ') a[t][j++]=s[x];else{if(s[x-1]!=' ') { a[t][j]='\0'; t++; j= 0;}}x++;}if(s[x-1]!=' ') t++;p=t;tag=0;for(j=0;j<p-1;j++){for(k=j+1;k<p;k++){tag=check(a[j],a[k]);if(tag==1)break;}if(tag==1){cout<<"No"<<endl;break;}}if(tag==0)cout<<"Yes"<<endl;}return 0;}2、#include<iostream>#include<algorithm>#include<cstring>using namespace std;char s[1000][1010],len[1000],ss[1000];int cmp(const void *a,const void *b){return strcmp((char *)a,(char *)b);}bool check(int s1){for(int i=0;i<len[s1];i++) if(s[s1][i]!=s[s1+1][i]) return false;return true;}int main(){int cases,n,i,t,j;bool flag;scanf("%d",&cases);getchar();while(cases--){gets(ss);i=0;t=0;j=0;flag=false;while(ss[i]!='\0'){if(ss[i]!=' ') s[t][j++]=ss[i];else{ if(ss[i-1]!=' ') { s[t][j]='\0'; t++; j=0;} }i++;}if(ss[i-1]!=' ') t++;n=t;len[0]=strlen(s[0]);for(i=1;i<n;i++){len[i]=strlen(s[i]);if(len[i-1]>len[i]) continue;else { if(check(i-1)) { flag=true; break; } } }if(flag) printf("No\n"); else printf("Yes\n");}return 0;}1043节约每一个字节#include<iostream>using namespace std;char s[110][1000];int len[110],b[110];int main(){int cases,n,i,j,k,g,sum;scanf("%d",&cases);while(cases--){scanf("%d",&n);for(i=1;i<=n;i++) { scanf("%s",s[i]); len[i]=st rlen(s[i]); b[i]=0; }for(i=1;i<n;i++){for(j=i+1;j<=n;j++){sum=0;k=len[i]-1;g=len[j]-1;while(k>=0&&g>=0) { if(s[i][k]==s[j][g] ) { k--;g--; sum++; } else break; }if(sum>b[i]) b[i]=sum;}}sum=0;for(i=1;i<=n;i++) sum+=b[i];printf("%d\n",sum);}return 0;}1044 John的农场#include<iostream>using namespace std;int a[110][110],lowcost[110];bool s[110];int cases,n,i,j,k,minnum,minx;int main(){scanf("%d",&cases);while(cases--){scanf("%d",&n);for(i=1;i<=n;i++) for(j=1;j<=n;j++) { scanf("%d ",&a[i][j]); if(a[i][j]==0) a[i][j]=999999; }s[1]=true;minnum=0;for(i=2;i<=n;i++) { lowcost[i]=a[1][i]; s[i]=fa lse; }for(i=1;i<n;i++){minx=999999;j=1;for(k=2;k<=n;k++) if(lowcost[k]<minx && !s[ k]) { minx=lowcost[k]; j=k; }s[j]=true;minnum+=minx;for(k=2;k<=n;k++){if(a[j][k]<lowcost[k] && !s[k]) lowcost [k]=a[j][k];}}printf("%d\n",minnum);}return 0;}1047食物链#include<iostream>int f[50001],r[50001];int fs(int i){if(f[i]==i) return i;int t=f[i];f[i]=fs(f[i]);r[i]=(r[t]+r[i])%3;return f[i]; }void un(int x,int y,int h){int a=fs(x),b=fs(y);f[a]=b;r[a]=(r[y]-r[x]+3+h)%3;}int main(){int n,k,i,ans=0;scanf("%d%d\n",&n,&k);for(i=1;i<=n;++i) f[i]=i;for(i=0;i<k;i++){int d,x,y;scanf("%d%d%d",&d,&x,&y);if(x>n||y>n) {++ans;continue;}if (d==1){if (fs(x)==fs(y)){if (r[x]!=r[y]) ++ans;}else un(x,y,0);}else{if (fs(x)==fs(y)){if (r[x]!=(r[y]+1)%3) ++ans;}else un(x,y,1);}}1048黑箱子#include<iostream>using namespace std;int p[100000],top;void inset(int x){int i=0;if(x>p[top]){p[++top]=x;return;}p[top+1]=p[top];for(i=top;i>=0&&x<p[i];i--)p[i]=p[i-1];p[i+1]=x;top++;}int main(){char a[10];int num=0,x;p[0]=0;top=0;while(scanf("%s",a)!=EOF){if(a[0]!='G'){scanf("%d",&x);inset(x);} else {num++;printf("%d\n",p[num]);}}return 0;}1049牛奶的产出#include<iostream>#include<algorithm>using namespace std;long cases,n,i,a[10010];int main(){scanf("%ld",&cases);while(cases--){scanf("%ld",&n);for(i=0;i<n;i++) scanf("%ld",&a[i]);sort(a,a+n);printf("%ld\n",a[n/2]);}return 0;}1050第K个数#include<iostream>int f[50001],r[50001];int fs(int i){if(f[i]==i) return i;int t=f[i];f[i]=fs(f[i]);r[i]=(r[t]+r[i])%3;return f[i];}void un(int x,int y,int h){int a=fs(x),b=fs(y);f[a]=b;r[a]=(r[y]-r[x]+3+h)%3;}int main(){int n,k,i,ans=0;scanf("%d%d\n",&n,&k);for(i=1;i<=n;++i) f[i]=i;for(i=0;i<k;i++){int d,x,y;scanf("%d%d%d",&d,&x,&y);if(x>n||y>n) {++ans;continue;}if (d==1){if (fs(x)==fs(y)){if (r[x]!=r[y]) ++ans;}else un(x,y,0);}else{if (fs(x)==fs(y)){if (r[x]!=(r[y]+1)%3) ++ans;}else un(x,y,1);}}printf("%d\n",ans);return 0;}}1051一个小游戏#include<iostream>using namespace std;int cases,n,m,i,t;int next[1010],prev[1010];int main(){scanf("%d",&cases);while(cases--){scanf("%d%d",&n,&m);for(i=1;i<n;i++) next[i]=i+1; next[n]=1;for(i=2;i<=n;i++) prev[i]=i-1; prev[1]=n;t=1;while(n>1){for(i=1;i<m;i++) t=next[t];next[prev[t]]=next[t];prev[next[t]]=prev[t];t=next[t];n--;if(n<2) break;for(i=1;i<m;i++) t=prev[t];next[prev[t]]=next[t];prev[next[t]]=prev[t];t=prev[t];n--;}printf("%d\n",t);}return 0;}1052数字序列#include<iostream>using namespace std;const int N=3000010;int len,t[100010];bool inset[N];int main(){int n,i,s;t[0]=0; t[1]=1;s=1;memset(inset,0,sizeof(inset));inset[1]=true;for(i=1;i<N,s<100001;i++){if(inset[i]) {t[s++]=i;if(i*2+1<N && !inset[i*2+1]) inset[i*2+1]=t rue;if(i*3+1<N && !inset[i*3+1]) inset[i*3+1]=t rue;}}while(scanf("%d",&n)!=EOF){if(n<0) break;printf("%d\n",t[n]);}return 0;}1054平方数#include<iostream>using namespace std;int cases,n,i,j,sum;int64 a[1010];int main(){while(scanf("%d",&n)!=EOF){if(n==0) break;for(i=1;i<=n;i++) scanf("%I64d",&a[i]);sum=0;for(i=1;i<=n;i++){for(j=i;j<=n;j++) {if(a[j]==a[i]*a[i] || a[i]==a[j]*a[j]) sum++;}}printf("%d\n",sum);}return 0;}1055整数分类#include<iostream>#include<cstring>using namespace std;int find(char a[]){int y=0,l;l=strlen(a);for(int i=0;i<l;i++)y+=a[i]-'0';return y;}int chai(int m){if(m<10) return m;else return m%10+chai(m/10);}int main(){char a[510];int k;scanf("%s",a);while(a[0]!='0'){k=find(a);while(k>=10) { k=chai(k);} cout<<k<<endl;scanf("%s",a);}return 0;}1056猜数字#include<iostream>using namespace std;char a[5],b[5];bool c[130];int n,i,j,x,y;int main(){while(scanf("%s",a)!=EOF){if(a[0]=='0') break;scanf("%d",&n);for(j=1;j<=n;j++){scanf("%s",b);x=0;y=0;memset(c,0,sizeof(c));for(i=0;i<4;i++) if(a[i]==b[i]) x++;for(i=0;i<4;i++) c[a[i]]=1;for(i=0;i<4;i++) if(c[b[i]] && a[i]!=b[i]) y++;if(j!=n) printf("%dA%dB ",x,y); else printf("%d A%dB\n",x,y);}}return 0;}1057 XTU方阵#include<iostream>using namespace std;int main(){int n,num=1;cin>>n;while(n!=0){cout<<"Case "<<num<<":"<<endl;for(int i=0;i<3*n;i++){ for(int j=0;j<n;j++)cout<<"XTU";cout<<endl;}cout<<endl;cin>>n;num++;}return 0;}1058青蛙王子#include<iostream>using namespace std;typedef __int64 llong;llong a,b,c,t;llong gcd(llong a,llong b){if(b==0) return a;llong r=a%b;while(r>0) { a=b; b=r; r=a%b; }return b;}int main(){while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF){if(a==0&&b==0&&c==0) break;t=gcd(a,b);if(t==0){if(c==0) printf("Yes\n"); else printf("No\n ");}else {if(c%t == 0) printf("Yes\n");else printf("No\n");}}return 0;}1059有多少个1?#include<iostream>using namespace std;int find(__int64 n){if(n<2) return 1;return n%2+find(n/2);}int main(){__int64 n=2,x;int num;for(int i=1;i<32;i++) n*=2;while(scanf("%I64d",&x)!=EOF){if(x==0) num=0;else if(x>0) num=find(x);else num=find(n+x);cout<<num<<endl;}return 0;}。

c语言课后习题参考答案

c语言课后习题参考答案

第1章习题参考答案1、选择题(1)C (2)C (3)D (4)B (5)D(6)D (7)C (8)A (9)A (10)C2、填空题(1)函数、一个主函数、函数。

(2) /*、*/ 。

(3) 连接。

(4) # 。

(5) main 。

(6) /*……*/ 。

(7) .h 。

(8) F5 。

(9) .exe (10) .exe 、 .obj 、.exe 。

3、程序设计题(1) #include <stdio.h>void main(){printf(" *\n");printf(" * S *\n");printf(" *\n");}(2) #include <stdio.h>void main(){printf("*****************\n");printf("Your are welcome!\n");printf("-----------------\n");}(3) #include <stdio.h>void main(){char c1,c2;printf("请输入c1和c2的值:");c1=getchar();scanf(" %c",&c2);printf("c1=%c\n",c1);printf("c2=");putchar(c2);printf("\n");}第2章习题参考答案1、简述算法的概念。

答:算法就是为解决一个具体的问题而使用的确定的、有限的方法和步骤。

2、算法应具备哪些特性?答:算法应具备有穷性、确定性、可行性、输入、输出等五种特性。

3、分别说明什么是自然语言描述法?什么是框图语言描述法?什么是伪语言描述法?什么是N-S框图语言描述法?它们的优缺点各是什么?答:①用人们日常使用的语言来描述算法,称为算法的自然语言描述法。

c语言程序设计课后习题及答案

c语言程序设计课后习题及答案

c语言程序设计课后习题及答案C语言程序设计是计算机科学与技术专业的核心课程之一,它不仅教授了编程语言的基本知识,还培养了学生的逻辑思维能力和问题解决能力。

课后习题是巩固课堂学习内容的重要手段,以下是一些C语言程序设计的典型课后习题及答案。

习题1:基本数据类型编写一个程序,输入一个整数和一个浮点数,然后输出它们的和。

```c#include <stdio.h>int main() {int a;float b, sum;printf("请输入一个整数:");scanf("%d", &a);printf("请输入一个浮点数:");scanf("%f", &b);sum = a + b;printf("它们的和是:%f\n", sum);return 0;}```习题2:条件语句编写一个程序,根据输入的成绩判断学生的等级。

```c#include <stdio.h>int main() {float score;printf("请输入学生的成绩:");scanf("%f", &score);if (score >= 90) {printf("等级:A\n");} else if (score >= 80) {printf("等级:B\n");} else if (score >= 70) {printf("等级:C\n");} else if (score >= 60) {printf("等级:D\n");} else {printf("等级:F\n");}return 0;}```习题3:循环结构编写一个程序,计算1到100的整数之和。

2022年湘潭大学公共课《C语言》科目期末试卷B(有答案)

2022年湘潭大学公共课《C语言》科目期末试卷B(有答案)

2022年湘潭大学公共课《C语言》科目期末试卷B(有答案)一、填空题1、设有以下定义和语句,请在printf语句的_______中填上能够正确输出的变量及相应的格式说明。

union{int n;doublex;}num;num.n=10;num.x=10.5;printf("_______",_______);2、在C语言源程序中,一个变量代表【】。

3、一个C语言源程序由若干函数组成,其中至少应含有一个________4、若a是int型变量,则表达式(a=4*5,a*2),a+6的值为_______。

5、在C语言的赋值表达式中,赋值号左边必须是_______6、请读程序段:int x=1;printf("%d\n",~x);上面程序段的输出结果是_______。

7、下面程序段的运行结果是_______。

x=2;do{printf("*");x--;)while(!x==0);8、设有以下宏定义:#define WIDTH80#define LENGTH(WIDTH+40)则执行赋值语句:k=LENGTH*20;(k为int型变量)后,k的值是_______。

9、设有以下宏定义:#define WIDTH 80#define LENGTH WIDTH+40则执行赋值语句:v=LENGTH*20;(v为int型变量)后,v的值是_______。

10、若有以下定义和语句:int*p[3],a[6],i;for(i=0;i<3;i++)p[i]=8&a[2*i];则*p[0]引用的是a数组元素_______,*(p[1]+1)引用的是a数组元素_______。

二、选择题11、若x、i、j和k都是int型变量,则执行表达式x=(i=4,j=16,k=32)后x的值为( )。

A.4B.16C.32D.5212、在C语言中,要求运算数必须是整型的运算符是( )。

C语言课后全部答案

C语言课后全部答案

第一章C语言初步一、选择题1.C2.D3.D4.A5.D6.B7.B 8.B 9.C 10.A二、思考与应用题1.A.字符串常量B.字符串常量C.整型常量D.浮点型常量(double)2.A.long doubleB.long intC.floatD.字符型常量3.A.整型常量B.字符型常量C.字符串常量D.字符串常量4.FirstExample: 10,w is Yz is 5.125.int main(void){return 0;}6.#include<stdio.h>int main(void){print f("Hello World");return 0;}7.#include<stdio.h>int main(void){printf("We are to learn correct");printf("C language here");return 0;}/*main*/8.#include<stdio.h>int main(void){/*Local Definition*/int a;float b;char c;/*Statements*/printf("The end of the program."); return 0;}/*main*/9.#include<stdio.h>int main(void){/*Local Definition*/int a;float b;char c,d;/*Statements*/printf("The end of the program."); return 0;}/*main*/10.#include<stdio.h>int main(void){/*Local Definition*/int a;char b,c,d;float e,f;/*Statements*/printf("The end of the program."); return 0;}/*main*/11.A.char option;B.int sum=0;C.float produce=1;12.A.short code;B.float salesTax=.0825;C.double sum=0;13.#include<stdio.h>int main(void){float cost=172.53;printf("The sales total is: $ %6.2f\n",cost);printf("^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^");return 0;}14.#include<stdio.h>int main(void){printf("******\n");printf("******\n");printf("******\n");printf("******\n");return 0;}15.#include<stdio.h>int main(void){printf("*\n");printf("**\n");printf("***\n");printf("****\n");return 0;}16.#include<stdio.h>int main(void){int x1=1;int x2=10;int x3=100;int x4=1000;int x5=10000;printf("%d %d %d %d %d\n",x1,x2,x 3,x4,x5);printf("%f %f %f %f %f\n",x1,x2,x 3,x4,x5);return 0;}17.#include<stdio.h>int main(void){int x;printf("Please enter an integer:\n");scanf("%d",&x);printf("The number as a character:%c\n",x);printf("The number as a decimal:%d\n",x);printf("The number as a float:%f\n",x);return 0;}三、编程题1.#include<stdio.h>int main(void){int x1;int x2;int x3;printf("Please enter three numbers:");scanf("%d%d%d",&x1, &x2, &x3);printf("\nYour numbers forword:\n");printf("%8d\n%8d\n%8d\n",x1,x2,x3 );printf("Your numbers reversed:\n");printf("%8d\n%8d\n%8d\n",x3,x2,x1 );return 0;}2.#include<stdio.h>int main(void){int x1;int x2;int x3;int x4;int x5;int x6;int x7;int x8;int x9;int x10;printf("Please enter 10 numbers:\n");scanf("%d%d%d%d%d%d%d%d%d%d",&x1, &x2, &x3, &x4, &x5, &x6, &x7, &x8, &x9, &x10);printf("\nYour numbers are:\n");printf("%8d%8d\n",x1,x10);printf("%8d%8d\n",x2,x9);printf("%8d%8d\n",x3,x8);printf("%8d%8d\n",x4,x7);printf("%8d%8d\n",x5,x6);return 0;}3.#include<stdio.h>int main(void){int x1;int x2;int x3;int x4;int x5;int x6;int x7;int x8;int x9;printf("Input:\n");printf(" ");scanf("%d%d%d%d%d%d%d%d%d",&x1, &x2, &x3, &x4, &x5, &x6, &x7, &x8, &x9);printf("\nOutput\n");printf("%8d,%d,%d\n",x1,x2,x3);printf("%8d,%d,%d\n",x4,x5,x6);printf("%8d,%d,%d\n",x7,x8,x9);return 0;}第二章表达式与简单程序设计一、选择题1.A2.D3.C4.B5.B6.D7.B二、思考与应用题1.A.2 B.8 C.8 D.0 E.11 2.A.4,4B.4,4 C.4,4 D.2,3 E.4,53.A.12 B.-27 C.14 D.2 E.4 4.A.22.52 B.4.5 C.13.333333 D.0.882353 E.0.075.ABCEF6.A.5 B.2 C.4 D.1 E.7.A.5 B.2 C.4 D.-3 E.6 8.A.5 B.294 C.4 D.29 E.9 9. 50 50 100 2510.#include<stdio.h>int main(void){float x;float y;float p;float s;float total;printf("Please enter x and y:\n");scanf("%f%f",&x,&y);p=x*y;s=x+y;total=s*s+p*(s-x)*(p+y);printf("total=%f",total);return 0;}11.#include<stdio.h>int main(void){int x,y,z;printf("Please enter two integers:\n");scanf("%d%d",&x,&y);z=x*y;printf("%d*%d=%d",x,y,z);return 0;}12.#include<stdio.h>int main(void){float x;int y;printf("Please enter a floating point number:\n");scanf("%f",&x);y=(int)x%10;printf("%d",y);return 0;}13.#include<stdio.h>int main(void){float x;int y;printf("Please enter a floating point number:\n");scanf("%f",&x);y=(int)x/10%10;printf("%d",y);return 0;}14.#include<stdio.h>int main(void){float x;float y;float c;float s;p rintf("Please enter length and width:\n");s canf("%f%f",&x,&y);c=2*(x+y);s=x*y;p rintf("The circumference is %f\n",c);p rintf("The measure of area is %f",s);r eturn 0;}15.#include<stdio.h>int main(void){int a;f loat r;p rintf("Please enter an angle(integer):\n");s canf("%d",&a);r=a/57.295779;p rintf("%d°is %.5f radians",a,r);r eturn 0;}16.#include<stdio.h>int main(void){float c;float F;printf("Please enter centigrade:\n");scanf("%f",&c);F=32+(c*180.0/100.0);printf("Fahrenheit is %f ",F);return 0;}17.(1)double m,v,KinEn;KinEn=m*v*v/2;(2)double b,c,res;res=(b+c)/(2*b*c);18.(1)#include<stdio.h>int main(void){int x=25;x=x+5;printf("%d\n",x);x=x+5;printf("%d\n",x);return 0;}(2)#include<stdio.h>int main(void){int x=10;x=x+2;printf("%d\n",x);x=x+2;printf("%d\n",x);return 0;}(3)#include<stdio.h>int main(void){int x=32;x=x*2;printf("%d\n",x);x=x*2;printf("%d\n",x);return 0;}三、编程题1.A.#include<stdio.h>int main(void){float x;float y;printf("Please enter an number(inch):\n");scanf("%f",&x);y=x/12;printf("%f inches is %f feet ",x,y);return 0;}B.#include<stdio.h>int main(void){float x;float y;printf("Please enter an number(inch):\n");scanf("%f",&x);y=x/36;printf("%f inches is %f yards ",x,y);return 0;}C.#include<stdio.h>int main(void){float x;float y;printf("Please enter an number(inch):\n");scanf("%f",&x);y=x*2.54;printf("%f inches is %f centimeters ",x,y);return 0;D.#include<stdio.h>int main(void){float x;float y;printf("Please enter an number(inch):\n");scanf("%f",&x);y=x/39.37;printf("%f inches is %f meters ",x,y);return 0;}2.#include<stdio.h>int main(void){int fib1;int fib2;int fib3;fib1=13+21;fib2=21+fib1;fib3=fib1+fib2;printf("%d,%d,%d",fib1,fib2,fib3);return 0;}3.#include<stdio.h>int main(void){int x;int b1;int b2;int b3;int b4;int b5;printf("Please enter an integer(0-32767):\n");scanf("%d",&x);b1=x%10;b2=x/10%10;b3=x/100%10;b4=x/1000%10;b5=x/10000%10;printf("%4d%4d%4d%4d%4d\n",b5,b4,b3,b2,b1);printf("%4d%4d%4d%4d%\n",b4,b3,b2,b1);printf("%4d%4d%4d\n",b3,b2,b1);printf("%4d%4d\n",b2,b1);printf("%4d",b1);return 0;}第三章分支程序设计一、选择题1.C2.D3.A4.B5.B6.A7.C8.C 9.A二、思考与应用题1.A.0 B.1 C.1 D.0 E.0 2.A.x>=y B.x<y C.x!=y D.x==y E.x>y3.A.0 B.1 C.1 D.1 E.0 4.x=4,y=3,z=25.x=4,y=1,z=26.x=4,y=2,z=27.x=4,y=1,z=28.x=0,y=0,z=19.x=2,y=0,z=210.x=0,y=0,z=111.x=0,y=0,z=112.x=-1,y=0,z=013.x=0,y=1,z=014.x=1,y=3,z=115.x=1,y=3,z=116.x=4,y=3,z=017.if(aChar=='E'){c++;printf("Value is E\n");}18.switch(ch){case ‘E’:case ‘e’:countE++;break;case ‘A’:case ‘a’:countA++;break;case ‘I’:case ‘i’:countI++;break;default:printf("Error-Not A,E,or I\a\n"); }三、编程题1.#include<stdio.h>main( ){int x,x1,x2,x3;char grade;printf("please enter three text scores(0-100):\n");scanf("%d%d%d",&x1,&x2,&x3);x=(x1+x2+x3)/3;if(x>=90)grade='A';else if(x>=70&&x<90)if(x3>90)grade='A';elsegrade='B';else if(x>=50&&x<70)if((x2+x3)/2>70)grade='C';elsegrade='D';else if(x<50)grade='F';printf("%c\n",grade);}2.#include<stdio.h>main( ){int year;printf("please enter a year:\n");scanf("%d",&year);if(year%4==0&&year%100!=0||year%400==0)printf("%d is a leap year!\n",year);elseprintf("%d is a non-leap year!\n",year);}3.#include<stdio.h>main( ){i ntyearNow,monthNow,dayNow,year,month,day,x;printf("Please enter the date today(month day year):\n");scanf("%d%d%d",&monthNow,&dayNow,&yearNow);printf("Please enter your birthday(month day year):\n");s canf("%d%d%d",&month,&day,&year);if(monthNow<month)x=yearNow-year-1;else if(monthNow==month)if(dayNow<day)x=yearNow-year-1;elsex=yearNow-year;elsex=yearNow-year;printf("You are %d years old!\n",x);}4.#include<stdio.h>#include<stdlib.h>#include<time.h>main( ){int number,Ynumber;srand(time(NULL));number=rand()%20+1;printf("I am thinking of a number between 1 and 20.\n");printf("Can you guess what it is?");scanf("%d",&Ynumber);if(Ynumber==number)printf("Congratulations!You did it.");else{if(Ynumber>number)printf("Your guess is high.Try again:");elseprintf("Your guess is low.Try again:");scanf("%d",&Ynumber);if(Ynumber==number)printf("Congratulations!You did it.");else{if(Ynumber>number)printf("Your guess is high.Try again:");elseprintf("Your guess is low.Try again:");scanf("%d",&Ynumber);if(Ynumber==number)printf("Congratulations!You did it.");else{if(Ynumber>number)printf("Your guess is high.Try again:");elseprintf("Your guess is low.Try again:");scanf("%d",&Ynumber);if(Ynumber==number)printf("Congratulations!You did it.");else{if(Ynumber>number)printf("Your guess is high.Try again:");elseprintf("Your guess is low.Try again:");scanf("%d",&Ynumber);if(Ynumber==number)printf("Congratulations!You did it.");else{printf("sorry.The number was %d.\n",number);printf("You should have gotten it by now.\n");printf("Better luck next time.");}}}}}}5.#include<stdio.h>main( ){int year,month,day,x=0,y,z;printf("Please enter your birthday:(year month day)\n");scanf("%d%d%d",&year,&month,&day);y=((year-1)*365+((year-1)/4)-((year-1)/100) +((year-1)/400))%7;switch(month){case 12:x=x+30;case 11:x=x+31;case 10:x=x+30;case 9:x=x+31;case 8:x=x+31;case 7:x=x+30;case 6:x=x+31;case 5:x=x+30;case 4:x=x+31;case 3:x=x+28;case 2:x=x+31;case 1:x=x+0;}if(year%4==0&&year%100!=0||year%400==0)day++;z=(x+day+y)%7;switch(z){case 0:printf("The day of your birthday is Sunday!");break;case 1:printf("The day of your birthday is Monday!");break;case 2:printf("The day of your birthday is Tuseday!");break;case 3:printf("The day of your birthday is Wednesday!");break;case 4:printf("The day of your birthday is Thursday!");break;case 5:printf("The day of your birthday is Friday!");break;case 6:printf("The day of your birthday is Saturday!");break;}}6.#include<stdio.h>#include<math.h>main( ){int a,b,c,p;float x1,x2;printf("Please enter a,b,c:\n");scanf("%d%d%d",&a,&b,&c);p=b*b-4*a*c;if(a==0)if(b==0)printf("There is no solution!");else{x1=-(float)c/b;printf("The solution is x1=%f",x1);}elseif(p<0)printf("There is no real solution!");else{x1=-b+sqrt(p)/(2*a);x2=-b-sqrt(p)/(2*a);printf("The solutions are x1=%f,x2=%f",x1,x2);}}。

2022年湘潭大学兴湘学院公共课《C语言》科目期末试卷A(有答案)

2022年湘潭大学兴湘学院公共课《C语言》科目期末试卷A(有答案)

2022年湘潭大学兴湘学院公共课《C语言》科目期末试卷A(有答案)一、填空题1、请读程序段:int x=1;printf("%d\n",~x);上面程序段的输出结果是_______。

2、C语言源程序的基本单位是________3、C语言源程序文件的后缀是;经过编译后,生成文件的后缀是;经过连接后,生成文件的后缀是4、若x和n均是int型变量,且x的初值为12,n的初值为5,则执行表达式x%=(n%=2)后x的值为_______。

5、若a、b和c均是int型变量,则执行表达式a=(b=4)+(c=2)后a的值为_______,b的值为_______,c的值为_______。

6、设main函数的说明为:int main(int argc,char*argv[]),且有命令行为:FILE1123<回车>,则argc的值是_______,argv[1]的值是_______。

7、下面程序段中循环体的执行次数是_______。

a=10;b=0;do{b+=2;a-=2+b;}while(a>=0);8、为建立如图10-5所示的存储结构(即每个结点两个域,data是数据域,next是指向结点的指针域),请将定义补充完整。

struct s{char data;_______}node;9、假设变量a和b均为整型,以下语句可以不借助任何变量把a、b中的值进行交换。

请填空。

a+=_______;b=a-_______;a-=_______;10、若有定义:inta[3][4]={{1,2},{0},{4,6,8,10}};,则初始化后,a[1][2]得到的初值是_______,a[2][1]得到的初值是_______。

二、选择题11、下面4个选项中,是合法整型常量的选项是( )。

A.160B.-0xcdfC.-01D.-0x48a-0xffff 01a 986 0122e5011 0xe 066 80x12、下面不正确的字符串常量是( )A.'abe'B."12'12"C. "o"D.""13、以下针对scanf函数的叙述中,正确的是()。

C语言课程 课后练习 参考答案

C语言课程 课后练习 参考答案

实验6-1课后练习:1.选择题(1)B (2)C(3)A(4)D(5)C (6)C (7)D 2.阅读程序,写出运行结果3020103.阅读程序,写出运行结果20104. 阅读程序,写出运行结果1 2 35.阅读程序,写出运行结果7 8 96.阅读程序,写出运行结果97.阅读程序,写出运行结果#include <stdio.h>int digit(int n,int k);int main(){ int n,k;printf("Enter n,k: ");scanf("%d %d",&n,&k);printf("\nThe result is:%d\n",digit(n,k));return 0;}int digit(int n,int k){ int d;while(k>0){ d=n%10;n=n/10;k--;}return d;}8.程序填空#include <stdio.h>double max(double a,double b);int main(){ double x,y;prinf("please input two double numbers:"); scanf("%lf %lf"),&x,&y;printf("%.2f\n",max(x,y));return 0;}double max(double a,double b){return (a>b?a:b);}9.原版#include <stdio.h>int check(int n,int d);int main(){ int n,d;printf("Enter n,d: ");scanf("%d %d",&n,&d);if(check(n,d)==1)printf("The digit %d is in data %d",d,n); elseprintf("The digit %d is not in data %d",d,n); return 0;}int check(int n,int d){ int temp;while(n>0){ temp=n%10;if(temp==d)return 1;n=n/10;}return 0;}改进版#include <stdio.h>int check(int n,int d);int main(){ int n,d,position;printf("Enter n,d: ");scanf("%d %d",&n,&d);position=check(n,d);if(check(n,d)>0)printf("The digit %d is in data %d,position is %d",d,n,position);elseprintf("The digit %d is not in data %d",d,n);return 0;}int check(int n,int d){ int temp,position=1;while(n>0){ temp=n%10;if(temp==d)return position;position++;n=n/10;}return 0;}实验6-2课后练习1.选择题(1)B (2)C(3)B(4)B(5)C (6)B (7)D (8)D (9)C(10)B(11)A2.填空(1) 函数中声明有局部变量,若声明中带有static,则该局部变量采用静态存储方式,在编译时分配空间,若函数中未显式设定初始值,则系统将其自动初始化,函数被调用结束时,该空间不释放。

C语言课后习题参考答案

C语言课后习题参考答案

C语言课后习题参考答案第一章:基础知识问题1:C语言的发展历史及特点C语言是由贝尔实验室的肯·汤普逊和丹尼斯·里奇在20世纪70年代初开发的一种高级程序设计语言。

它以其简洁、高效和可移植性而闻名。

C语言被广泛应用于系统级编程、嵌入式系统和科学计算等领域,并成为其他编程语言的基础。

问题2:C语言的基本数据类型及其存储范围C语言的基本数据类型包括字符型(char)、整型(int)、浮点型(float)、双精度浮点型(double)和空类型(void)。

它们的存储范围如下:- char:-128 到 127 或 0 到 255- int:-32,768 到 32,767 或 0 到 65,535- float:3.4E-38 到 3.4E38- double:1.7E-308 到 1.7E308- void:无特定存储范围问题3:如何在C语言中输出语句?在C语言中,可以使用printf函数来输出语句。

它的基本语法为:```cprintf("要输出的内容");```问题4:定义变量的语法规则在C语言中,定义变量的语法规则如下:```c数据类型变量名;```例如,定义一个整型变量x:```cint x;```问题5:如何进行变量的赋值?变量的赋值可以使用赋值运算符(=)。

例如,将一个整型常量10赋值给变量x:```cx = 10;```第二章:控制结构问题1:if语句的语法结构if语句用于根据条件执行不同的代码块。

它的基本语法结构如下:```cif (条件) {// 条件为真时执行的代码}```问题2:switch语句的用法及语法结构switch语句用于根据不同的表达式值执行相应的代码块。

它的基本用法及语法结构如下:```cswitch (表达式) {case 值1:// 当表达式的值等于值1时执行的代码break;case 值2:// 当表达式的值等于值2时执行的代码break;default:// 当表达式的值与所有case都不匹配时执行的代码break;}```问题3:循环语句的分类及语法结构C语言中的循环语句包括for循环、while循环和do-while循环。

大学C语言课本课后习题相应答案及详细解答

大学C语言课本课后习题相应答案及详细解答

5-1#include"stdio.h" void main(){char c1,c2;printf("c1:");c1=getchar();if(c1>='a'&&c1<='z') c2=c1-32;else if(c1>='A'&&c1<='Z') c2=c1+32;else c2=c1;printf("=>%c\n",c2);}5-2#include"stdio.h"void main(){char c;printf("c:");c=getchar();if(c>='A'&&c<='Z')if(c=='A') printf("没有前面的字母!");else if(c=='Z') printf("没有后面的字母!!");else printf("前面的字母=%c,后面的字母=%c",c-1,c+1);printf("\n");} 5-3#include"stdio.h"void main(){int s;char g;printf("s:");scanf("%d",&s);if(s>=0&&s<=100){switch(s/10){case 9:case 10: g='A';break;case 8: g='B';break;case 7: g='C';break;case 6: g='D';break;default: g='E';}printf("g=%c\n",g);}else printf("成绩不在百分制范围内!\n"); }5-4#include"stdio.h"void main(){int y,m,d,dok;int yy,mm,dd;printf("y,m,d:");scanf("%d,%d,%d",&y,&m,&d);switch(m){case 1: case 3: case 5: case 7: case 8: case 10: case 12:if(d>0&&d<=31) dok=4;else dok=0;break;case 4: case 6: case 9: case 11:if(d>0&&d<=30) dok=3;else dok=0;break;case 2: if(y%4==0&&y%100!=0||y%400==0)if(d>0&&d<=29)dok=2;else dok=0;elseif(d>0&&d<=28)dok=1;else dok=0;break;default: dok=0;}if(dok==0) printf("月份或日期不对!\n");else{switch(dok){case 1: if(d==28){yy=y;dd=1;mm=m+1;}else{yy=y;dd=d+1;mm=m;}break;case2:if(d==29){yy=y;dd=1;mm=m+1;}else{yy=y;dd=d+1;mm=m;}break;case3:if(d==30){yy=y;dd=1;mm=m+1;}else{yy=y;dd=d+1;mm=m;}break;case 4:if(d==31)if(m==12){yy=y+1;dd=1;mm=1;}else{yy=y;dd=1;mm=m+1;}else{yy=y;dd=d+1;mm=m;}break;}printf("Tomorrow:%d年%d月%d日\n",yy,mm,dd);}}5-5#include"stdio.h"void main(){int a,b,c,t;printf("a,b,c:");scanf("%d,%d,%d",&a,&b,&c);if(a+b>c&&b+c>a&&a+c>b){if(a>b){t=a;a=b;b=t;}if(a>c){t=a;a=c;c=t;}if(b>c){t=b;b=c;c=t;}if(a==b&&b==c) printf("等边三角形.");else if(a==b||b==c||a==c) printf("等腰角形.");else if(c*c==a*a+b*b) printf("直角三角形.");else printf("任意三角形.");printf("\n");}else printf("不能构成三角形!\n");}6-1#include"stdio.h"void main(){int a,b,m,n,k;printf("m,n:");scanf("%d,%d",&m,&n);a=m;b=n;k=a%b;while(k){a=b;b=k;k=a%b;}printf("%d\n",(m*n/b));}6-2#include"stdio.h"void main(){int n;int i,j,s;printf("n=");scanf("%d",&n);for(i=1;i<=n;i++){s=0;for(j=1;j<i;j++)if(i%j==0) s=s+j;if(s==i) {printf("%d:",i);for(j=1;j<i;j++)if(i%j==0) printf("%d ",j);printf("\n");}}} 6-3#include"stdio.h"#include"math.h"void main(){double x,y,zd,zx,x1,x2;zd=zx=50;for(x=0;x<=2;x=x+0.2){y=2*pow(x,3)-3*pow(x,4)+6*pow(x,5)-4*x+50;if(y>zd){ zd=y;x1=x;}if(y<zx){ zx=y;x2=x;}}printf("x=%f,max=%f\n",x1,zd);printf("x=%f,min=%f\n",x2,zx);}6-4#include"stdio.h"void main(){double x,sum=0,a,b,c;int i;printf("x=");scanf("%lf",&x);a=x;b=1.0;c=1.0;for(i=1;i<=10;i++){sum=sum+a/(b*c);a=a*x*x;b=b+2;c=c*i;}printf("y(%.2lf)=%lf\n",x,sum);}7-1/*选择排序*/#include"stdio.h"void main(){int i,j,n,k,temp;int a[10];printf("n(<10):");scanf("%d",&n);printf("Original:");for(i=0;i<n;i++) scanf("%d",&a[i]);for(i=0;i<n-1;i++){ /*趟*/k=i;for(j=i+1;j<n;j++) /*比较次数*/if(a[j]<a[k]) k=j;/*<升序,>?*/if(k!=i){temp=a[i];a[i]=a[k];a[k]=temp;}}printf("Ordered:");for(i=0;i<n;i++) printf("%d ",a[i]);printf("\n");}7-2#include"stdio.h"void main(){int a[3][3];int i,j,s=1;printf("Input:\n");for(i=0;i<3;i++)for(j=0;j<3;j++){scanf("%d",&a[i][j]);if(i==j) s=s*a[i][j];}printf("s=%d\n",s);} 7-3/*杨辉三角*/#include"stdio.h"void main(){int x[7][7];int i,j;for(i=0;i<7;i++) {x[i][0]=1;x[i][i]=1;}for(i=2;i<7;i++)for(j=1;j<i;j++)x[i][j]=x[i-1][j]+x[i-1][j-1];for(i=0;i<7;i++){for(j=0;j<=i;j++)printf("%3d",x[i][j]);printf("\n");}}7-4#include<stdio.h>#include<string.h>void main(){char str[21];int i,j;printf("str:");gets(str);for(i=0,j=strlen(str)-1;i<=j;i++,j--)if(str[i]!=str[j]) break;if(i>j) printf("%s是回文\n",str);else printf("%s不是回文\n",str);}7-5/*输入一维数组的10个元素,并将最小值与第1个数交换,最大值与最后一个数交换,然后输出交换后的结果*/#include<stdio.h>void main(){int a[10],i,zx,zd;printf("Input:\n");zx=zd=0;for(i=0;i<10;i++){scanf("%d",&a[i]);if(a[i]<a[zx]) zx=i;if(a[i]>a[zd]) zd=i;}if(zx!=0){int t;t=a[0];a[0]=a[zx];a[zx]=t;}if(zd!=9){int t;t=a[9];a[9]=a[zd];a[zd]=t;}for(i=0;i<10;i++)printf("%d ",a[i]);printf("\n");} 8-2#include"stdio.h"double xexp(double x,int n){double c;if(n==0) c=1.0;else c=x*xexp(x,n-1);return c;}void main(){int n;double x;printf("x:");scanf("%lf",&x);printf("n:");scanf("%d",&n);printf("Result=%g\n",xexp(x,n));}8-3#include"stdio.h"int find(int x[],int n,int y){int i,pos=-1;for(i=0;i<n;i++)if(x[i]==y) pos=i;return pos;}void main(){int a[10]={11,1,13,15,18,7,19,27,3,8};int i,y,pos;for(i=0;i<10;i++) printf("%d ",a[i]);printf("\ny:");scanf("%d",&y);pos=find(a,10,y);if(pos==-1) printf("Not found!\n");else printf("Position=%d\n",pos);}8-1#include"stdio.h"#include"conio.h" //getch()#include"stdlib.h" //srand(),rand(),system("cls") #include"time.h" //time()void main(){void init(int a[][5],int m,int n);void input(int a[][5],int m,int n);void output(int a[][5],int m,int n);int min(int b[][5],int m,int n);int a[5][5],ch='0';while(1){system("cls"); //清屏printf("1.初始化 2.键盘输入0.结束程序:");printf("\n");if(ch=='0') break;else if(ch=='1'){init(a,5,5);output(a,5,5);}else if(ch=='2'){input(a,5,5);output(a,5,5);}else printf("Error!\n");printf("Min element:%d\n",min(a,5,5));printf("按任意键继续!\n");getch();}}void init(int a[][5],int m,int n){int i,j;srand(time(0)); //time(0)表示以当前的时间做种子,增加每次运行的随机性for(i=0;i<5;i++)for(j=0;j<5;j++)a[i][j]=rand()%100; //随机数范围:0~32767,将它控制在0~99的范围}void input(int a[][5],int m,int n){int i,j;printf("Input Array 5X5:\n");for(i=0;i<m;i++)for(j=0;j<n;j++)scanf("%d",&a[i][j]);}void output(int a[][5],int m,int n){int i,j;printf("Output Array 5X5:\n");for(i=0;i<5;i++){for(j=0;j<5;j++)printf("%2d ",a[i][j]);printf("\n");}}int min(int b[][5],int m,int n){int i,j,zx;zx=b[0][0];for(i=0;i<m;i++)for(j=0;j<n;j++)if(i==j&&b[i][j]<zx) zx=b[i][j];return zx;}8-4#include"stdio.h"float pave,nave;void saver(float a[],int n){int i,z,f;float psum,nsum;psum=nsum=0.0;z=f=0;for(i=0;i<n;i++)if(a[i]<0) {nsum=nsum+a[i];z++;}else if(a[i]>0) {psum=psum+a[i];f++;}else continue;pave=(z!=0?psum/z:0.0);nave=(f!=0?nsum/f:0.0);}void main(){float a[10]={1.0,11.0,3.0,-1.5,-5.5,-2};saver(a,10);printf("pave=%.1f,nave=%.2f\n",pave,nave); } 8-5#include"stdio.h"#include"math.h"void p1(int a,int b){printf("has two equal roots:%\n",-b/(2*a));}void p2(int a,int b, int disc){float x1,x2;x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);printf("Has distant real roots:% and %\n",x1,x2);}void p3(int a,int b, int disc){float real,image;real=-b/(2*a);image=sqrt(-disc)/(2*a);printf("Has complex roots:\n");printf("%+%8.4fi\n",real,image);printf("%-%8.4fi\n",real,image);}void main(){ int a,b,c,disc;printf("a,b,c:");scanf("%d,%d,%d",&a,&b,&c);disc=b*b-4*a*c;if(fabs(disc)<=1e-6) p1(a,b);else if(disc>1e-6) p2(a,b,disc);else p3(a,b,disc);}8-6#include"stdio.h"#include"stdlib.h"#include"conio.h"#include"time.h"void main(){void printaverage(int score[][5],int m,int n);void printname(int score[][5],int m,int n);int score[10][5];int i,j;srand(time(0));for(i=0;i<10;i++)for(j=0;j<5;j++)score[i][j]=50+rand()%50+1;printf("Output Students' score:\n");printf("Course 1 2 3 4 5\n");printf("-------------------------\n");for(i=0;i<10;i++){printf("No.%2d:",i+1);for(j=0;j<5;j++)printf("%3d ",score[i][j]);printf("\n");}printaverage(score,10,5);printname(score,10,5);} void printaverage(int score[][5],int m,int n){int i,j,sum;printf("\nAverage score:\n");for(i=0;i<m;i++){sum=0;for(j=0;j<n;j++)sum=sum+score[i][j];printf("No.%d:%.1f\n",i+1,sum/5.0);}}void printname(int score[][5],int m,int n){int i,j,max0,row0;for(j=0;j<5;j++){max0=score[0][j];for(i=0;i<10;i++)if(score[i][j]>max0){max0=score[i][j];row0=i;}printf("Course %d,max score=%d,student:No.%d\n",j+1,max0,row0+1);}}大学英语I词汇练习题Choose the best one to complete each sentence.1. She felt like ____C___ frustration, but she was determined not to lose her self-control.A. to cry out ofB. to cry forC. crying out ofD. crying for2. The method he used turned out to be ___A____ in improving the students' English.A. effectiveB. abilityC. responseD. explicit3. Measures had to be taken in face of the housing problem that ____C____ in the city.A. foundedB. raisedC. aroseD. produced4. Without electronic computers, much of today's advanced technology ___D____.A. haven't been achievedB. wouldn't be achievedC. hadn't been achievedD. wouldn't have been achieved5. Jane said to her husband, "Don't worry. There is no cause for __B______ about our daughter'sability to manage herself."A. careB. concernC. attentionD. love6. The tap won't ____A____, and there is water all over the floor.A. turn offB. turn downC. turn onD. turn up7. Wearing the right shoes and clothes ____B____ being fit can make all the difference.A. in additionB. as well asC. alsoD. too8. She didn't try to do anything for her daughter, and ____B____ it's too late now.A. in caseB. in any caseC. at this caseD. in case of9. The girl said she hated _____C___ he smiled at her.A. that wayB. this wayC. the wayD. all the way10. I'll call the hotel. I'll tell them we'll ____A____ tomorrow morning and stay there for two nights.A. check inB. check outC. check offD. check over11. My father didn't go to New York; the doctor suggested that he ___C____ there.A. not to goB. won'tC. not goD. not to go to12. She _____C____ him to find answers to her problems.A. learned fromB. came intoC. leaned onD. subjected to13. The large wings of that bird ______B_____ it to fly high and fast.A. makeB. enableC. forceD. realize14. If he ____D___ the policeman honestly, he would not have been arrested.A. would answerB. answerC. should answerD. had answered15. I intended ___A____ you last Sunday, but I had no time.A. to have called onB. calling onC. to be calling onD. to be called on16. If you try to learn too many things at a time, you may get ____D___.A. alarmedB. scaredC. surprisedD. confused17. I haven't read this book, and my brother hasn't ____A____.A. eitherB. neitherC. alsoD. too18. No sooner ____B_____ than the truck started off.A. his luggage was loadedB. had his luggage been loadedC. loaded his luggageD. his luggage was being loaded19. At the end of the game, the whole crowd ____C____ their feet and cheered wildly.A. emerged fromB. rose fromC. got toD. stood up20. Darren has decided to give ___D_____ football at the end of this season.A. inB. forC. offD. up21. Neither his friends nor his mother ____B____ his marriage to that girl.A. acceptB. acceptsC. agreeD. agrees22. Most people believe that he is quite _____A___ of lying to get out of trouble.A. capableB. enabledC. ableD. skilled23. We were told that most of our luggage would be _____B___ by sea.A. approachedB. transportedC. handledD. communicated24. The Broadcasting Museum also offers Saturday workshops to _____B___ children with the world of radio.A. contriveB. acquaintC. acquireD. admit25. Without a passport, leaving the country is ____D___.A. out of questionB. without questionC. in the questionD. out of the question26. She states her views very ___A______.A. definitelyB. definiteC. infiniteD. infinitely27. You need to ____D____ the employers that you can do the job.A. convictB. confuseC. confirmD. convince28.The little boy was so lucky to have ___B_____ the earthquake.A. surviveB. survived C .surviving D. survival29. It is ___B___ that later in life you will see each other again.A. likeB. likelyC. likedD. seemed30.Women’s social ____B____ has changed much over the years.A. stateB. statusC. statesD. statement31.The novel _____D____ the imagination of thousands of readers.A. caterB. cateredC. captureD. captured32.There are good chances of ______D______ working in this firm.A. prospectB. portionC. positionD. promotion33.If you are ______A_____ a night owl, having the first class start in the afternoon might be thebest thing that could ever happen to you.A. more ofB. more thanC. much moreD. more34. The conference discussed the fair ______A_____of income and wealth.A. distributionB. distributeC. contributionD. contribute35. I was so ___C__ in today's history lesson that I didn't understand anything.A. amazedB. confusingC. confusedD. amazing36. ___B___ in England, Anne Bradstreet both admired and imitated several English poets.A. Having born and educatedB. Born and educatedC. Since born and educatedD. To be born and educated37. You will see this product ___B___ wherever you go.A. to be advertisedB. advertisedC. advertiseD. advertising38. I never forget my __B__ with you for the first time.A. to meetB. meetingC. to have metD. having to be meeting39.She was so angry that she left like __B__ something at him.A. to throwB. throwingC. to have thrownD. having thrown40. We are ___C__ in different kinds of extracurricular activities on campus.A. having involvedB. to involveC. involvedD. involving41.Let me assure you that it was not done ______D_____.A. intensiveB. intensivelyC. intentionalD. intentionally42.This medicine can ____A____ the pain in no time.A. lessenB. lessC. looseD. loosen43.Advertising is often the most ____A____ method of product promotion.A. effectiveB. effectedC. emotionalD. efficient44.It is at 5 a.m. _C___ the train from Beijing to Guangzhou will arrive.A. forB. withC. thatD. when45.Things might have been much worse if the mother _B___ on her right to keep the baby.A. has been insistingB. had insistedC. would insistD. insisted46.Had he worked harder, he __B__ the exams.A. must have got throughB. would have got throughC. permitted are freshmenD. are permitted freshmen47.God knows, how can I ____A____ six exams a week?.A. endureB. tolerateC. bear D .suffer48.All their attempts ______C______ the child from the burning building were in vain.A. rescuedB. to have rescueC. to rescueD. in rescuing49,Could you find someone ______C_______ for me to play tennis with?A. to fitB. fitC. fittingD. suit50. The smooth space flight marks a big ______D______ for Chinese scientists.A. breakB. breakingC. breakupD. breakthrough51 Economic crises destroy the capitalist system, and they grow in size and ____A____.A. durationB. duringC. endureD. enduringpanies will have to do more than this if they are to ___B_____ the earthquake.A. survivedB. surviveC. survivesD. surviving53. The baby felt ____C____ by her parents.A. hateB. loveC. ignoredD. ignore54. There are good chances of ______D______ in this firm.A. moveB. motiveC. motionD. promotion55. She tried to ______D______ our friendship.A. underminedB. keptC. maintainedD. undermine56. The new airport will ______C___ tourism.A. helpedB. useC. facilitateD. benefited57. They were full of ____B____ when they saw my new car.A. jealousB. envyC. pitifulD. loves58. This medicine can ____A____ the pain in no time.A. lessenB. lessC. leastD. little59.He _______A_____ her on her last physics paper.A. complimentedB. complimentC. completeD. completed60. Mrs. Hill is _______B____ on Tom’s marrying Susan.A. awareB. keenC. kindD. keep61. Let me assure you that it was not done ______C_____.A. internationalB. purposeC. intentionallyD. intentional62.The novel _____D_____ the imagination of thousands of readers.A. seizeB. catchC. captureD. captured63. If you yell ___A____ someone, you should apologize.A. atB. outC. ofD. under64. He came over and ____B____ a piece of paper into my hand.A. shoveB. shovedC. shovesD. shoving65. The girl student whose father is a senior engineer used to ___A____ abroad.A. studyB. studyingC. studiesD. studied66.The doctor tried to help ____D____ the function of his limbs.A. storeB. storesC. restoresD. restore67. Women’s social ____B____ has changed much over the years.A. placeB. statusC. landD. work68. It was he ___A___ knew he didn’t need to be afraid to make mistakes at that moment.A. whoB. whomC. whichD. whose69. A dropped cigarette is being ____C____ for the fire.A. firedB. fireC. blamedD. blame70. Advertising is often the most _____D___ method of promotion.A. effectB. affectC. affectedD. effective71.In time you’ll _____A_____ the beauty of this language.A. appreciateB. appreciatesC. appreciatedD. appreciating72. ______B___ is his favorite toy is hard for the kid to decide.A. whoB. whichC. howD. where73. After dinner, I took a ____A____ around the park.A. strollB. divingC. runD. walking74. The boy really enjoyed the useful and ____B____ work in science.A. creationB. creativeC. createdD. creator75. Some professions are _____A_______ with people who have not grown up.A. stuffedB. stuffC. stiffD. stiffed76. Economic crises destroy the capitalist system, and they grow in size and ____A____.A. durationB. duringC. endureD. enduring77. The new airport will ____C_____ tourism.A. helpedB. useC. facilitateD. benifited78. Let me assure you that it was not done _____C______.A. internationalB. purposeC. intentionallyD. intentional79. The girl student whose father is a senior engineer used to ___A____ abroad.A. studyB. studyingC. studiesD. studied80. Advertising is often the most ____D____ method of promotion.A. effectB. affectC. affectedD. effective81. It was he ___A___ knew he didn’t need to be afraid to make mistakes at that moment.A. whoB. whomC. whichD. whosepanies will have to do more than this if they are to _____B___ the earthquake.A. survivedB. surviveC. survivesD. surviving83. The baby felt ____C____ by her parents.A. hateB. loveC. ignoredD. ignore84. If you yell ___A____ someone, you should apologize.A. atB. outC. ofD. under85. Mrs. Hill is ______B_____ on Tom’s marrying Susan.A. awareB. keenC. kindD. keep86. _____A____ college takes hard work!A. SurvivingB. SurviveC. SurvivesD. To surviving87. It is absolutely ____A___ for the human beings to do something to save the earth.A. necessaryB. necessarilyC. unnecessaryD. unnecessarily88. An employer is more ____D____ to choose the candidate with high grades.A. likeB. probablyC. possiblyD. likely89. I don’t like this young woman. She seems a social ___D____.A. flowerB. workerC. negotiatorD. butterfly90. How should this ___B____ girl see the coldness of the letter?A. blindB. innocentC. warmD. pretty91. Be careful not to ___B____ yourself in their local conflicts.A. attendB. involveC. take part inD. participate92. Home prices are holding ____C___ in most regions, and people live with steady income.A. flexibleB. fierceC. stableD. shortage93. He was ______C______ by a newspaper reporter at that time.A. being interviewB. interviewC. being interviewedD. to interview94. They were full of ____C____ when they saw my new car.A. enviousB. envilyC. envyD. envies95. ________C_________, it is a very stable pattern producing results through effort .A. ContraryB. ConverseC. On the contraryD. Diverse96. The teacher doesn’t permit _____B____ in classA. smokeB. smokingC. to smokeD. to have a smoke97. The man in the corner confessed to ___A____ a lie to the manager of the company.A. have toldB. be toldC. being toldD. having told98. I never forget __B____ you for the first time.A. to meetB. meetingC. to have metD. having to be meeting.99. _______A___ a man has, ___________ he wants.A. The more, the moreB. More, moreC. The more, moreD. More, the more100. I see no _____B____ of his success.A. possibleB. possibilityC. possibilitiesD. probable101. He could be ____B____ about everything else in the world, but not about Manet, his loving child.A. visualB. criticalC. favoriteD. essential102. Some see themselves as the provider of ideas, ____C____ others view their role as essentially managerial.A. whenB. thereforeC. whileD. otherwise103. The idea ____A____ to him so much that he took it without hesitation.A. appealedB. interestedC. drewD. attracted104. The teacher evaluated the performance of each of his students who ____B____ evaluated his performance.A. by turnB. in turnC. at turnD. on turn105. Disease _____A____ during the journey and many passengers had to be rushed to hospital for treatment.A. broke outB. broke downC. started offD. started out106. The products have been _____A_____ to strict tests before leaving the factory.A. subjectedB. adjustedC. objectedD. constricted107. Our television set is out of order. Could you come and _____C_____ it for us?A. see throughB. see outC. see toD. see over108. _____C_____ they have taken matters into their hands, the pace of events has quickened.A. As thatB. So thatC. Now thatD. For that109. Our neighbor said if we made more noise he would _____B____ us to the police.A. inform ofB. complain aboutC. report toD. care for110. People working in the government should not _____A______ business affairs that might affect their political decisions.A. engage inB. hope forC. choose betweenD. pick on111. It's not easy to ______B______ to the top in show business.A. make outB. make itC. make upD. make them112. Carter is in charge of the office while I'm ______C______.A. leavingB. on leavingC. on leaveD. on relief113. We must ____B____ our attention on the question of reducing our cost.A. payB. focusC. absorbD. promote 114. Snap judgments, if ___B_____, have usually been considered signs of immaturity or lack of common sense.A. taking seriouslyB. taken seriouslyC. take seriouslyD. to be taken seriously115. Being with his family for a few days, I gained one or two insights ____A____ the reason he behaves the way he does.A. intoB. ofC. onD. off116. ___D___ your age or knowledge of the language, you'll be 100% involved in your studies from the first lesson to the last.A. As a result ofB. In spiteC. Despite ofD. Regardless of117. He said he had caught a bad cold and told me to ____D____ him.A. stay atB. stay upC. stay inD. stay away from118. Good managing of a company ___A_____ great efforts.A. calls forB. calls outC. calls inD. calls at119. I've looked ___C_____ all my papers but I still can't find the contract.A. uponB. outC. throughD. in120. When I'm going out in the evening I use the bike if I can, ___B_____ the car.A. regardless ofB. more thanC. other thanD. rather than121. We can't speak ___A____ our teacher.A. too highly ofB. too high ofC. highly of tooD. to highly of122. No one in our class ____D___ in sports than he.A. are more interestedB. are much interestedC. is much interestedD. is more interested123. As a teacher you have to ____B____ your methods to suit the needs of slower children.A. enlargeB. adjustC. affectD. afford124. ________A___ the hotel before soldiers armed with pistols and clubs charged into the crowd.A. Scarcely had they leftB. Scarcely they had leftC. Scarcely they leftD. Scarcely left they125. In the film, the peaceful life of a monk _____D_____ the violent life of a murderer.A. is compared withB. is compared toC. is contrasted toD. is contrasted with126 I didn't realize the food problem was so _____B___ in this city; with winter coming, many people would starve to death without more help.A. essentialB. criticalC. explicitD. effective127. Female socialization places importance on getting along with others, _____B___ male socialization places importance on becoming independent.。

(word完整版)C语言程序设计课后习题1-8参考答案

(word完整版)C语言程序设计课后习题1-8参考答案

C语言程序设计课后习题1—8参考答案习题1参考答案一、简答题1、冯诺依曼计算机模型有哪几个基本组成部分?各部分的主要功能是什么?答:冯诺依曼计算机模型是由运算器、控制器、存储器、输入设备、输出设备五大功能部件组成的。

运算器又称算术逻辑部件,简称ALU,是计算机用来进行数据运算的部件。

数据运算包括算术运算和逻辑运算。

控制器是计算机的指挥系统,计算机就是在控制器的控制下有条不紊地协调工作的.存储器是计算机中具有记忆能力的部件,用来存放程序和数据.输入设备是用来输入程序和数据的部件。

输出设备正好与输入设备相反,是用来输出结果的部件。

2、简述计算机的工作原理。

答:计算机的工作原理可简单地概括为:各种各样的信息,通过输入设备,进入计算机的存储器,然后送到运算器,运算完毕把结果送到存储器存储,最后通过输出设备显示出来。

整个过程由控制器进行控制。

3、计算机软件系统分为哪几类?答:软件内容丰富,种类繁多,通常根据软件用途将其分为两大类:系统软件和应用软件。

系统软件是指管理、监控、维护计算机正常工作和供用户操作使用计算机的软件。

这类软件一般与具体应用无关,是在系统一级上提供的服务。

系统软件主要包括以下两类:一类是面向计算机本身的软件,如操作系统、诊断程序等。

另一类是面向用户的软件,如各种语言处理程序(像BC、VC等)、实用程序、字处理程序等。

在操作系统的基础上运行。

4、什么叫软件?说明软件与硬件之间的相互关系。

答:软件是指计算机程序及有关程序的技术文档资料。

两者中更为重要的是程序,它是计算机进行数据处理的指令集,也是计算机正常工作最重要的因素。

在不太严格的情况下,认为程序就是软件。

硬件与软件是相互依存的,软件依赖于硬件的物质条件,而硬件则需在软件支配下才能有效地工作.在现代,软件技术变得越来越重要,有了软件,用户面对的将不再是物理计算机,而是一台抽象的逻辑计算机,人们可以不必了解计算机本身,可以采用更加方便、更加有效地手段使用计算机。

大学生C语言课后习题全部答案详解

大学生C语言课后习题全部答案详解
}#include<stdio.h>
\
main()
{
int y,m,d,yt,mt,dt,age;
printf("请按格式2010-12-10输入你的生日");
scanf("%d-%d-%d",&y,&m,&d);
printf("请按格式2010-12-10输入今天的日期");
scanf("%d-%d-%d",&yt,&mt,&dt);printf(源自| 0——退出|\n");
printf("|------------------------|\n");
printf("请输入选项\n");
ch=getch();
putch(ch);
}
#include<stdio.h>
#include<conio.h>
main()
{
//定义
int a=0,b=0,he=0,cha=0,ji=0;
{printf("%4d",i);
n++;
}
}
printf("\n");
printf("%d",n);
}#include<stdio.h>
#include<math.h>
main()
{
int n=0,s=1,i=0;
printf("input:");
scanf("%d",&n);
for(i=1;i<=n;i++)

湘潭大学c语言课后习题答案

湘潭大学c语言课后习题答案

#include<>void main(){int a,b,c,num,n;printf("请输入一个三位数:\n");scanf("%d",&num);c=num%10;b=(num/10)%10;a=num/100;if(a==b&&a!=c)n=1;else if(a==c&&a!=b)n=1;else if(b==c&&a!=b)n=1;else n=0;if(n)printf("%d\n",num);}#include<>#include<>void main(){int x;double y;scanf("%d",&x); /*输入x*/switch(x/10){case 0:y=cos(x+;break;case 1:y=pow(cos(x+,2);break;case 2:y=pow(cos(x+,4);break;}printf("%lf\n",y);}#include<>void main(){int num,i,leap;scanf("%d",&num);if(num%2!=0){printf("是奇数\n");if(num>1){for(i=2;i<=num/2;i++){if(num%i==0){leap=0;break;}else leap=1;}if(leap) printf("是素数\n");else printf("不是素数\n");}}else printf("是偶数\n");}#include <>void main(){int a,b,c,i,n;for(i=0;i<10000;i++){for(n=1;n<10000;n++){a=i;b=a+n;c=b+n;if(a*c==5*b&&b+c==8*a){printf("%d,%d,%d\n",a,b,c);break;}}}}#include<>#include<>void main(){double m=,p=;int k=1,n=1;do{m=n;n=n+2;if(k%2==0)m=*m;p=p+m;k++;}while(fabs(m)>=1e-6);p=p*;printf("%lf\n",p);}#include<>void main(){int k,k1=1,k2=1,k3;float p=0,m;for(k=1;k<=20;k++){k3=k1+k2;m=(float)k3/k2;p=p+m;k1=k2;k2=k3;}printf("%f\n",p);}(1).#include<>void main(){int i,j,k;for(i=0;i<=4;i++){for(j=0;j<=3-i;j++)printf(" ");for(k=0;k<=2*i;k++)printf("*");printf("\n");}for(i=0;i<=3;i++){for(j=0;j<=i;j++)printf(" ");for(k=0;k<=6-2*i;k++)printf("*");printf("\n");}}(2.)#include<>void main(){int i,j,k;char c=65;for(i=0;i<=4;i++){for(j=0;j<=3-i;j++)printf(" ");for(k=0;k<=2*i;k++)printf("%c",c);printf("\n");c=c+1;}for(i=0;i<=3;i++){for(j=0;j<=i;j++)printf(" ");for(k=0;k<=6-2*i;k++)printf("%c",c);printf("\n");c=c+1;}}#include<>void main(){int a,b,i,n;for(a=1;a<3000;a++){for(b=0,i=1;i<=a/2;i++)if(a%i==0) b=b+i;for(n=0,i=1;i<=b/2;i++)if(b%i==0) n=n+i;if(n==a&&a<b)printf("%d和%d\n",a,b);}}#include<>#include<>void main(){int num,a,b,c,p;for(num=100;num<1000;num++){c=num%10;b=num/10%10;a=num/100;p=pow(a,3)+pow(b,3)+pow(c,3);if(num==p)printf("%d\n",num);}}#include<>void main(){int m,i,n;for(i=2;i<1000;i++){m=0;for(n=1;n<=i/2;n++){if(i%n==0)m=m+n;}if(i==m){printf("完数:%d=",i);for(n=1;n<=i/2;n++)if(i%n==0)printf("%d ",n);}}}#include<>#include<>void main(){int i,a,b;for(i=1000;i<=9999;i++){a=i%100;b=i/100;if(i==pow((a+b),2))printf("%d\n",i);}}#include<>void main(){int i,a,b,c;for(i=100;i<1000;i++){a=i%10;b=i/10%10;c=i/100;if(((a==b)&&(b!=c))||((a==c)&&(b!=c))|| ((b==c)&&(a!=b)))printf("%d\n",i);}}#include<>void main(){int i,n=1,m;for(i=1;i<10;i++)n=(n+1)*2;printf("第一天摘了%d个桃子\n",n);}#include <>void main(){int long a;char h[16];int i;scanf("%ld",&a);i=15;while(a){int x=a%16;if(x<10)h[i]=x+'0';elseh[i]=x-10+'A';a=a/16;i--;}printf("十六进制:");i++;while(i<16){printf("%c",h[i]);i++;}printf("\n");}#include<>int main(){int num,a[10],m;int i=0,j,flag=1;scanf("%d",&num);do{m=num%10;a[i]=m;num=num/10;i++;}while (num%10!=0);for (j=0,i--;i>=j;i--,j++)if (a[j]==a[i])continue;else{flag=0;break;}if (flag)printf("是回文\n");elseprintf("不是回文\n");return 0;}#include<>void main(){int i,j,max,row=0,column=0;inta[3][4]={{1,3,5,7},{2,6,8,9},{11,22,33,10}};max=a[0][0];for(i=0;i<=2;i++)for(j=0;j<=3;j++)if(a[i][j]>max){max=a[i][j];row=i;column=j;}printf("max=%d,row=%d,column=%d\n", max,row,column);}#include<>void main(){int f[10]={1,1};int i,pow=0;for(i=2;i<=9;i++)f[i]=3*f[i-1]+2*f[i-2];for(i=0;i<=9;i++)pow=pow+f[i];printf("第十项是:%d,前十项和为:%d\n",f[9],pow);}#include <>void main(){int i,j;int a[8][8];for(i=0;i<8;i++){a[i][0]=1;a[i][i]=1;}for(i=2;i<8;i++)for(j=1;j<i;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];for(i=0;i<8;i++){printf("%*d",18-i*2,a[i][0]);for(j=1;j<=i;j++)printf("%4d",a[i][j]);printf("\n");}}#include<>#include<>void main(){int i,m;char a[100],b[100];gets(a);gets(b);if(strcmp(a,b)!=0){if(strlen(a)>strlen(b)){strcat(a,b);puts(a);}else{strcat(b,a);puts(b);}}}#include <>void Fun(int a){char h[16];int i;i=15;while(a){int x=a%16;if(x<10)h[i]=x+'0';elseh[i]=x-10+'A';a=a/16;i--;}printf("十六进制:");i++;while(i<16){printf("%c",h[i]);i++;}printf("\n");}void main(){int n;while(scanf("%d",&n)){Fun(n);printf("\n");}}符串的复制)符串的连接)符串小写字母改为大写字母)符串大写字母改为小写字母)较两字符串是否相等)//#include<>int strcmp(char *c,char *d){while(*c==*d&&*c!='\0'&&*d!='\0'){c++;d++;}if(*c==*d)return 0;else if(*c>*d)return 1;elsereturn -1;}void main(){char a[100],b[100];printf("分别输入两个字符串a和b:\n");gets(a);gets(b);if(strcmp(a,b)==0)printf("相等\n");else if(strcmp(a,b)==1)printf("不等,且a>b\n");elseprintf("不等,且b>a\n");}#include<>#include<>float A(float a,float b,float c){float s;s=(a+b+c)/;return(s);}float B(float a,float b,float c){float area,s;s=A(a,b,c);area=sqrt(s*(s-a)*(s-b)*(s-c));return(area);}void main(){float a,b,c,area,s,p;printf("输入三边长a,b,c:\n");scanf("%f%f%f",&a,&b,&c);if(a+b<c||a+c<b||b+c<a)printf("不能构成三角形\n");else{p=B(a,b,c);printf("三角形面积是:%f\n",p);}}#include<>#include<>int fun(int x){int i,m=1;for(i=1;i<=x;i++)m=m*i;return(m);}void main(){int n=10,sum=0;for(n=1;n<=10;n++)sum=sum+fun(n);printf("和为:%d\n",sum); }#include<>#include<>void fun(char a[]){char i,m,n;n=i=strlen(a);while(i>3){i=i-3;for(m=n;m>i;m--)a[m]=a[m-1];a[i]=',';n++;}}void main(){static char a[20];printf("输入数字:\n");gets(a);fun(a);puts(a);}#include<>#include<>long fun(char *a){int i=1,x,y;long m=0;x=strlen(a);if(a[0]=='-'){y=-1;x--;a++;}elsey=1;while(*a)m=m*10-48+(*a++);return(m*y);}void main(){char a[100];long n;printf("输入纯数字字符串:\n");gets(a);n=fun(a);printf("%ld\n",n);}。

C语言课后习题答案(第1-8章)

C语言课后习题答案(第1-8章)

第1章C语言概述一、简答题1.(1)语言简洁、紧凑,使用方便、灵活;(2)运算符丰富;(3)具有丰富的数据类型;(4)具有结构化的控制语句;(5)语法限制不太严格,程序设计自由度大;(6)C语言允许直接访问物理地址,能进行位(bit)操作,能实现汇编语言的大部分功能,可以直接访问硬件;(7)生成目标代码质量高,程序执行效率高;(8)用C语言写的程序可移植性好,基本上不作修改就能用于各种型号的计算机和各种操作系统。

2.顺序结构,分支结构,循环结构3.所谓算法就是为解决一个问题而采取的方法和步骤。

算法的特性:有穷性、确定性、输入、输出、可行性。

表示算法的方式:(1)用自然语言表示;(2)用流程图表示;(3)用N—S流程图表示;(4)用伪代码表示;(5)用计算机语言表示。

二、算法1.瓶子A里盛有醋瓶子B里盛有酱油那一个空瓶C将A中的醋倒入C将B中的酱油倒入A将C中的醋倒入B2.输入一个数放在a中max = a以下步骤重复9次:输入一个数放在a中如果a>max,max=b打印max的值3.如果a<btemp=aa=bb=temp如果c>atemp1=atemp2=ba=cb=temp1c=temp2否则如果c>btemp=bb=cc=temp打印a,b,c的值4.i=1sum=0sum=sum+ii=i+1打印sum 的值5.如果(n 除以3的余数为0 并且 n 除以5的余数为0)n 能够同时被3和5整除否则n 不能够同时被3和5整除6.i=101以下语句循环50遍:j=2flag=1当j<(i 除以2的整数部分)时循环以下语句:如果i 除以j 的余数为零flag=0退出该循环如果flag=1打印i 的值i=i+17.如果m<ntemp=mm=nn=tempi=nj=1做以下循环,直到m 能够被i/j 整除:如果i 能够被j 整除并且m 能够被i/j 整除i/j 即是m 和n 的最大公约数跳出循环否则j=j+1打印i/j 的值8.data=b*b —4*a*c如果data >0否则如果data=0x1=x2=—b/2否则无实数解三、编程题1.main (){adata b x 2)(1+-=a data b x 2)(2--=printf(“ How do you do? \n”);printf(“##############################\n”);}2.main(){int a,b,c;scanf(“%d%d%d”,&a,&b,&c);printf(“sum=%d”,a+b+c);}第二章数据类型与表达式一、单项选择题1. B2. C3. A4. A5. D6. B7.B8.A9.C 10. D二、写出下列程序的运行结果1.aabb cc abcAN2.ab97983. 9,11,9,104.a=2,b=3,c=25.s1=2.500000s2=3三、编程题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("%c%c%c%c%c\n",c1,c2,c3,c4,c5);}第三章顺序程序设计选择题1)B 2)D 3)D 4)A 5)B 6)A 7)B 8)A 9)D 10)D 11)C 12)D 13)A填空题1)b 2)261 3)25 21 37 4)abc 5)5.0,4,c=3 6)3编程题#define pi 3.1415926main( ){float h, r, l, sv, sz ;printf(“请输入圆的半径r,圆柱高h:\n”) ;scanf(“%f,%f”,&r, &h) ;l=2*pi*r ;s=pi*r*r ;sv=3.0/4.0*pi*r*r*r ;sz=pi*r*r*h ;printf(“圆周长为:l=%6.2f, 圆面积为:s=%6.2f\n”, l, s) ;printf(“求体积为:sv=%6.2f, 圆柱体积为:sz=%6.2f\n”, sv, sz) ;}2、main( ){float c, f ;printf(“请输入一个华氏温度:\n”) ;scanf(“%f”, &f) ;c=(5.0/9.0)*(f-32) ;printf(“摄氏温度为:%6.2f\n”, c) ;}第四章循环程序设计一、选择题1.D2.D3.D4.C5.D6.A7.B8.B9.A 10.D.11.B 12.A 13.B 14.C 15.C 16.C 17.D 18.B19.D 20.D 21.A二、填空题1.5858582.03.204.89215.20,07.1,3,28.Year%400==0year%100!=0count++三、编程题1.main(){long a;scanf(“%ld”,&a);if (a>=10000&&a<=99999)printf(“这是个五位数”);else if (a>=1000&&a<=9999) printf(“这是个四位数”);else if (a>=100&&a<=999) printf(“这是个三位数”);else if (a>=10&&a<=99) printf(“这是个二位数”);else if (a>=0&&a<=9)printf(“这是个一位数”);elseprintf(“输入错误”);}2.main(){int a;int g,s,b;//个位、十位、百位printf("水仙花数有:\n");for(a=100;i<=999;a++){g=a%10;s=a/10%10;b=a/100;if (a==g*g*g+s*s*s+b*b*b) printf("%d ",a);}printf("\n");}4、main(){int i,n;printf("input n:");scanf("%d",&n);for(i=2;i<=n;i++){while(i!=n)if(n%i==0){printf("%d*",i);n/=i;}else break;}printf("%d\n",n);}5、#include “stdio.h”main(){char c;int w,x,y,z;w=x=y=z=0;while((c=getchar())!=’\n’){if((c>’a’&&c<’z’)||(c>’A’&&c<’Z’)) w++;else if (c==’’)x++;else if (c>’0’&&c<’9’)y++;else z++}printf(“英文字母个数:%d\n”,w);printf(“空格个数:%d\n”,x);printf(“数字个数:%d\n”,y);printf(“其他字母个数:%d\n”,z);}6、main(){float h=100,t;int j;for(j=1;j<10;j++){t=h/2;h+=(t*2);}printf(“共经过:%f 米\n”,h);printf(“第十次反弹 %f 米\n”,t);}第五章数组一、单项选择题1. B2. A3. C4. C5. D6. B7.C8.B9.C 10. B二、写出下列程序的运行结果1.S=3682.*************************3.S1=18S2=104.!margorP5.数字0的出现次数是:3数字1的出现次数是:2数字2的出现次数是:2数字3的出现次数是:2数字4的出现次数是:0数字5的出现次数是:1数字6的出现次数是:1数字7的出现次数是:1数字8的出现次数是:1数字9的出现次数是:1三、编程题1.#include "stdio.h"main(){int a[11];int i,j,t;printf("input 10 number:\n");for (i=1;i<11;i++)scanf("%d",&a[i]);printf("\n");for (i=1;i<=9;i++)for (j=1;j<=10-i;j++)if (a[j]<a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}printf("the sorted number is:\n");for (i=1;i<11;i++)printf("%4d",a[i]);2.#include "stdio.h"main(){int a[11],i,b;printf("imput 10 sorted number:\n");for (i=0;i<10;i++)scanf("%d",&a[i]);printf("input an integer:\n");scanf("%d",&b);for (i=9;i>=0&&a[i]>b;i--)a[i+1]=a[i];a[i+1]=b;printf("sorted numbers:\n");for (i=0;i<11;i++)printf("%5d",a[i]);}3.#include "stdio.h"main(){int a[5],i,j,t;printf("input 5 integer:\n");for (i=0;i<5;i++) /*输入5个整数*/scanf("%d",&a[i]);for (i=0,j=4;i<j;i++,j--) /*逆序存放*/{t=a[i];a[i]=a[j];a[j]=t;}for (i=0;i<5;i++) /*逆序存放后重新输出*/printf("%5d",a[i]);}4.#include "stdio.h"main(){int i,j,a[10][10]={{1},{1,1}};for (i=2;i<10;i++) /*给二维数组每个元素赋值*/ {a[i][0]=1;for (j=1;j<=i;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];}for (i=0;i<10;i++) /*输出二维数组*/{for (j=0;j<=i;j++)printf("%-5d",a[i][j]);printf("\n");}}5.#include "stdio.h"main(){int a[3][4],i,j,max,row,col;printf("input 3*4 matrix:\n");for (i=0;i<3;i++) /*输入3*4矩阵*/for (j=0;j<4;j++)scanf("%d",&a[i][j]);max=a[0][0]; row=0; col=0;for (i=0;i<3;i++) /*寻找矩阵中的最大值及其行列号*/ for (j=0;j<4;j++)if (max<a[i][j]){max=a[i][j]; row=i, col=j;}printf("\n数组中最大的数是%d,其行号是%d,列号是%d\n",max,row,col);}6.#define N 3#define M 4#include "stdio.h"main(){int a[N][M],i,j,k,max,row,col;printf("input %d*%d matrix: \n",N,M);for (i=0;i<N;i++) /*输入二维矩阵*/for (j=0;j<M;j++)scanf("%d",&a[i][j]);for (i=0;i<N;i++) /*求鞍点*/{max=a[i][0]; row=i; col=0;for(j=1;j<M;j++) /*求矩阵中每一行的最大值及其所行列号*/ if (max<a[i][j]){max=a[i][j]; col=j;}for (k=0;k<N;k++) /*判断每一行的最大值在其所在列是否最大*/ if (a[k][col]<max)break;if (k==N) /*得到鞍点*/{printf("the point is %d,row=%d,col=%d\n",max,row,col);break;}}if (i==N) /*没有鞍点*/printf("no point\n");}7.#include "stdio.h"main()/*word=0表示未出现单词,如出现单词就置word为1.num用来统计单词个数*/char c;printf("please input a string :\n");while ((c=getchar())!='\n')if (c==' ')word=0;else if (word==0){word=1; num++;}printf("There are %d words in the line\n",num);}8.#include "stdio.h"main(){int i,j,uppn,lown,dign,span,othn;/*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);}9.#include "stdio.h"main()char str1[20],str2[20];printf("input two strings:\n");gets(str1);gets(str2);j=strlen(str1); /*求字符串1的长度*/for (i=0;str2[i]!='\0';i++,j++) /*字符串合并*/str1[j]=str2[i];str1[j]='\0'; /*加上字符串结束标志*/puts(str1);}10.#include "stdio.h"main(){int i,n;char str1[20],str2[20];printf("input two strings:(no more than 20 characters)\n");gets(str1); gets(str2);n=strlen(str1)<strlen(str2)?strlen(str1):strlen(str2); /*n中存放较短字符串的长度*/for (i=0;i<n&&str1[i]==str2[i];i++);printf("%d\n",str1[i]-str2[i]);}第六章函数与编译预处理一、单项选择题1. C2. B3. B4. B5. A6. B7.A8.C9.D 10. A二、写出下列程序的运行结果1.92.4,B8,B3.-4.0000004.a=6,b=55.48三、编程题1.main(){int n;printf("input n(n>0):\n");scanf("%d",&n);if (prime(n))printf("%d is a sushu\n",n);else printf("%d is not a sushu\n",n);}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);}2.#define N 3convert(int array[3][3]){ int i,j,t;for (i=0;i<N-1;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;int a[N][N];printf("input a:\n");for (i=0;i<N;i++)for (j=0;j<N;j++)scanf("%d",&a[i][j]);printf("Array a:\n");for (i=0;i<N;i++){for (j=0;j<N;j++)printf("%5d",a[i][j]);printf("\n");}convert(a);printf("a de zhuanzhi is:\n"); for (i=0;i<N;i++){for (j=0;j<N;j++)printf("%5d",a[i][j]);printf("\n");}}3. #include <stdio.h>#include <string.h>main(){char str[100];printf("input a string:\n") ;gets(str);inverse(str);printf("the reversed string is:%s\n",str);}inverse(char str[]){char t;int i,j;for (i=0,j=strlen(str)-1;i<j;i++,j--){t=str[i];str[i]=str[j];str[j]=t;}}4. #include <stdio.h>concat(char str1[],char str2[]){ int i=0,j;while (str1[i]!='\0')i++;for (j=0;str2[j]!='\0';i++,j++)str1[i]=str2[j];str1[i]='\0';}main(){char str1[100],str2[100];gets(str1);gets(str2);concat(str1,str2);puts(str1);}5. main(){char str[80];printf("input a string (4 ge shu zi zi fu):\n"); scanf("%s",str);insert(str);printf("result is:\n%s\n",str);}insert(char str[]){int i;for (i=strlen(str);i>0;i--){str[2*i]=str[i];str[2*i-1]=' ';}}6. #include "stdio.h"int i,ndight,nwhite,nletter,nother;count(char str[]){ndight=nwhite=nletter=nother=0;for (i=0;str[i]!='\0';i++)if (str[i]>='0'&&str[i]<='9')ndight++;else if ((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z')) nletter++;else if (str[i]==' ')nwhite++;else nother++;}main(){char text[80];printf("input a string:\n");gets(text);count(text);printf("ndight=%d,nletter=%d,nwhite=%d,nother=%d\n",ndight,nletter,nwhite,nothe r);}7. #define N 10#include <stdio.h>sort(char str[]){int i,j;char t;for (i=1;i<N;i++)for (j=0;j<N-i;j++)if (str[j]>str[j+1]){t=str[j];str[j]=str[j+1] ;str[j+1]=t;}}main(){char str[N];int i ;printf("Input 10 ge zi fu:\n");gets(str);sort(str);printf("The sorted result:\n") ;for(i=0;i<N;i++)printf("%c",str[i]);}8. #include <stdio.h>#include <string.h>#define N 10void input_e(int num[],char name[N][8]){int i;for (i=0;i<N;i++){ printf("input gong hao:");scanf("%d",&num[i]);printf("input name:");getchar();gets(name[i]);}for (i=0;i<N;i++)printf("%5d%10s\n",num[i],name[i]);}void sort(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+1;j<N;j++)if (num[j]<num[min]) min=j;temp1=num[i];strcpy(temp2,name[i]);num[i]=num[min];strcpy(name[i],name[min]);num[min]=temp1;strcpy(name[min],temp2);}printf("the sorted result:\n");for (i=0;i<N;i++)printf("%5d%10s\n",num[i],name[i]);}void search(int n,int num[],char name[N][8]) /*折半查找法*/ {int top,bott,mid,find;find=0;top=0;bott=N-1;if ((n<num[0])||(n>num[N-1]))find=-1;while ((find==0)&&(top<=bott)){mid=(bott+top)/2;if (n==num[mid]){find=1; printf("%d name is:%s\n",n,name[mid]);}else if (n<num[mid])bott=mid-1;else top=mid+1;}if ((find==-1)||(find==0))printf("%d is not found.\n",n);}main(){int num[N],number,c,flag;char name[N][8];input_e(num,name);sort(num,name);for (flag=1;flag;){printf("please input chazhao de gonghao:"); /*输入查找的工号*/ scanf("%d",&number);search(number,num,name);printf("continue Y/N?"); /*是否继续查找*/getchar();c=getchar();if (c=='N'||c=='n')flag=0;}}9. #include "stdio.h"#define MAX 10main(){char str[MAX];char c;int i;i=0;printf("input number(16 jinzhi): "); /*输入一个十六进制的数*/while((c=getchar())!='\n'&&i<MAX){ str[i]=c;i++;}str[i]='\0';printf("result is :%d\n",htod(str));int htod(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);}10. #define SWAP(a,b) t=a;a=b;b=tmain(){int a,b,t;printf("input a,b:") ;scanf("%d,%d",&a,&b);SWAP(a,b);printf("result:a=%d,b=%d\n",a,b);}11. #define SURPLUS(a,b) ((a)%(b)) main(){int a,b;printf("input a,b:");scanf("%d,%d",&a,&b);printf("result is: %d\n",SURPLUS(a,b)); }12. main(){int a,b,c;printf("input a,b,c:");scanf("%d,%d,%d",&a,&b,&c);printf("max=%d\n",max(a,b,c));}int max(int x,int y,int z){int t;t=(x>y ? x : y);return(t>z?t:z);}#define MAX(x,y) ((x)>(y)?(x):(y))main(){int a,b,c;printf("input a,b,c:");scanf("%d,%d,%d",&a,&b,&c);printf("max=%d\n",MAX(MAX(a,b),c));}13. #include "stdio.h"#define CHANGE 1#define MAX 80main(){char str[MAX];int i;printf("input a string:\n");gets(str);#if (CHANGE){ for (i=0;str[i]!='\0';i++)if (str[i]>='a'&&str[i]<'z'||str[i]>='A'&&str[i]<'Z')str[i]=str[i]+1;else if (str[i]=='z'||str[i]=='Z')str[i]=str[i]-25;}#endifprintf("%s\n",str);}第七章指针一、选择题1)A 2)D 3)D 4)C 5)B 6)B 7)B 8)C 9)B 10)C 11)A 12)A 13)A 14)C 15)B 16)A 17)C 18)B 19)D 20)B二、阅读下面程序,写出程序运行结果1)abcdeedcba 2)1113151719 3) 11,97,114)3 6 5)6385三、编程题1、main( ){ int a[10],i,temp,*p=a;printf("Please input array a:\n"); for(i=0;i<10;i++)scanf("%d",&a[i]);printf("array a:\n");for(i=0;i<10;i++)printf("%4d",a[i]);for(i=0;i<5;i++){ temp=p[i];p[i]=p[10-i-1];p[10-i-1]=temp;}printf("\n Now array a:\n");for(i=0;i<10;i++)printf("%4d",a[i]);}2、main( ){ int a[3][3],*p,i,j;printf("please input matrix:\n"); for(i=0;i<3;i++)for(j=0;j<3;j++)scanf("%d",&a[i][j]);p=&a[0][0];move(p);printf("\n Now matrix:\n");for(i=0;i<3;i++){ for(j=0;j<3;j++)printf("%4d",a[i][j]);printf("\n");}}move(int *q){ int i,j,t;for(i=0;i<3;i++)for(j=i;j<3;j++){ t=*(q+3*i+j);*(q+3*i+j)=*(q+3*j+i);*(q+3*j+i)=t;}}3、#include <stdlib.h>#include <alloc.h>#include <string.h>#include <stdio.h>main( ){int binary();void insert();char *temp,*ptr1[6]={"BASIC","DATA","PASCAL","SQL","USE"}; int i;ptr1[5]=malloc(20);printf("\n");printf("original string:\n");for(i=0;i<5;i++)printf("%s\n",ptr1[i]);printf("input search string:\n");temp=malloc(20);gets(temp);i=binary(ptr1,temp,5);printf("i=%d\n",i);insert(ptr1,temp,5,i);printf("output strings:\n");for(i=0;i<6;i++)printf("%s\n",ptr1[i]);}int binary(char *ptr[],char *str,int n){int hig,low,mid;low=0;hig=n-1;if(strcmp(str,ptr[0])<0) return(0);if(strcmp(str,ptr[hig])>0) return(n);while(low<=hig){mid=(low+hig)/2;if(strcmp(str,ptr[mid])<0)hig=mid-1;else if(strcmp(str,ptr[mid])>0)low=mid+1;else return(mid);}return(low);}void insert(char *ptr[],char *str,int n,int i){int j;for(j=n;j>i;j--)strcpy(ptr[j],ptr[j-1]);strcpy(ptr[i],str);}4、main( ){ int i;char *p[6],str[6][20];for(i=0;i<6;i++)p[i]=str[i];printf("Input 6 strings:\n");for(i=0;i<6;i++)scanf("%s",p[i]);sort(p);printf("Now the strings:\n");for(i=0;i<6;i++)printf("%s\n",p[i]);}sort(char *q[ ]){ int i,j;char *t;for(i=0;i<5;i++)for(j=0;j<5-i;j++)if(strcmp(*(q+j),*(q+j+1))>0){ t=*(q+j);*(q+j)=*(q+j+1);*(q+j+1)=t;}}5、main( ){ int m;char str1[20],str2[20],*p1,*p2;printf("please input two strings:\n"); scanf("%s",str1);scanf("%s",str2);p1=str1;p2=str2;m=stringcmp(p1,p2);printf("result is %d:\n",m);}stringcmp(char *p1,char *p2){ int i=0;while(*(p1+i)==*(p2+i))if(*(p1+(i++))=='\0') return(0);return((*p1+i)-*(p2+i));}6、main( ){ char str1[30],str2[30],str3[100];int i=0,j=0,k=0;printf("please input two strings:\n");scanf("%s",str1);scanf("%s",str2);while(str1[i]!='\0' && str2[j]!='\0'){ str3[k++]=str1[i++];str3[k++]=str2[j++];}while(str1[i]!='\0')str3[k++]=str1[i++];while(str2[j]!='\0')str3[k++]=str2[j++];str3[k]='\0';printf("\n%s\n",str3);}第八章结构体一、选择题1、C2、B3、C4、D5、B6、A7、D8、C9、C 10、B二、填空题1、342、03、514、10,x5、364041三、编程题1、 struct complex{float x; /*实部*/float y; /*虚部*/}main(){struct complex c1={5,3},c2={2,6};float t1,t2;t1=c1.x*c2.x - c1.y*c2.y;t2=c1.x*2.y + c1.y*c2.x;printf(“The two complexs product is:”); printf(“%.2f%+.2f i\n”,t1,t2);}2. struct student{char name[10]; /**/int number; /*学号*/float x; /*成绩*/float y; /*成绩*/}main( ){struct student s[45];int i;void max();void average();void sort();for (i=0;i<45;i++)scanf(“%s%d%f%d”,s[i].name,&s[i].number,&s[i].x,&s[i].y); /*输入学生的、学号、两门课程成绩*/max(s,45);average(s,45);sort(s,45);}void max(struct student *a,int n){float temp;char na[10];int i ,num;temp=a[0].x+a[0].y;for (i=1;i<n;i++)if(temp<a[i].x+a[i].y) /*求总分最高的学生、学号*/{temp=a[i].x+a[i].y;strcmp(na,a[i].name);num=a[i].number;}printf(“%s,%d”,na,num);}void average(struct student *b,int n){float aver1,aver2,s1,s2;int i ;s1=s2=0;for(i=0;i<45;i++) /*求每门课程的总成绩*/{s1+=b[i].x;s2+=b[i].y;}aver1=s1/45;aver2=s2/45;for(i=0;i<45;i++)if(b[i].x<aver1&&b[i].y<aver2)/*输出两门课程都低于平均成绩的学生*/printf(“%s,%d\n”,b[i].name,b[i].number);}void sort(struct student *c,int n){float t;int i,j ;struct student temp;for (i=0;i<n-1;i++) /*改进的选择法排序可参考课本P89页*/ {k=i ;for(j=i+1;j<n;j++)if(c[k].x<c[j].x)k=j;if(k!=i){temp=c[i];c[i]=c[k];c[k]=temp;}for(i=0;i<n;i++)printf(“%s,%.2f\n”,c[i].n,c[i].x);}3. typedef struct Ljian{int pname;int wnum;struct Ljian *next;}Part;/*用户自定义零件结构体类型*/# include “stdio.h”main( ){int n=1;Part *head,*p1,*p2;head=NULL;p1=( Part * ) malloc (sizeof(struct Ljian));/*创建第一个结点*/scanf(“%d%d”,&p1->pname,&p1->wnum);p1->next=NULL;while(n<=10) /*将结点加入链表*/{if(n==1)head=p1; /*是第一个结点,作表头*/elsep2->next=p1; /*不是第一个结点,作表尾*/p2=p1;p1=( Part *) malloc(sizeof(struct Ljian));/*开辟下一个结点*/scanf(“%d%d”,&p1->pname,&p1->wnum);p1->next=NULL;n++;}}4.#include<stdlib.h>#include<stdio.h>#include<conio.h>#define LEN sizeof(struct student)typedef struct student{ long num; /*学号*/float score; /*成绩*/struct student *next;} STU; /*用户自定义学生结构体类型*/STU *Create ( ) /*创建链表* /{int n=0;STU *p1,*p2,*head;head=NULL;p1=(STU *)malloc(LEN); /*创建第一个结点*/scanf("%ld,%f",&p1->num,&p1->score);p1->next=NULL;while(p1->num!=0) /*将结点加入链表*/{++n;if(n==1) /*是第一个结点,作表头*/head=p1;else /*不是第一个结点,作表尾*/p2->next=p1;p2=p1;p1=(STU *)malloc(LEN); /*开辟下一个结点*/scanf("%ld,%f",&p1->num,&p1->score);p1->next=NULL;}free(p1); /*释放最后一个结点所占的存*/ return (head); /*返回链表的头指针*/}void print(STU *head) /*输出链表* /{STU *p;p=head;do{printf("%ld\t%5.1f\n",p->num,p->score);p=p->next;}while(p!=NULL);}STU *merge(STU *p1,STU *p2) /*合并链表* /{STU *p,*head , *s1,*s2;s1=p1;s2=p2;while(s1->next!=NULL)s1=s1->next;/*将指针s1指向链表1的最后一个节点*/while(s2->next!=NULL)s2=s2->next;/*将指针s2指向链表2的最后一个节点*/if(s1->num<p2->num){s1->next=p2;head=p1;}/*因为两个链表都是按学号升序排列,如果链表1的最后一个节点中的学号小于链表2的第一个节点中的学号,则直接将链表2插入到链表1的后面*/else if(s2->num<p1->num){s2->next=p1;head=p2;}/*如果链表2的最后一个节点中的学号小于链表1的第一个节点中的学号,则直接将链表1插入到链表2的后面*/else{ if(p1->num<p2->num) /*确定两个链表的顺序* /{head=p=p1;p1=p1->next;}else{head=p=p2;p2=p2->next;}while(p1!=NULL && p2!=NULL)if(p1->num<p2->num){p->next=p1;p=p1;p1=p1->next;}else{p->next=p2;p=p2;p2=p2->next;}if(p1!=NULL)p->next=p1;/*如果链表2的节点插入完,则将链表1直接插到后面*/elsep->next=p2; /*如果链表1的节点插入完,则将链表2直接插到后面*/}return head;}***注:红字部分仅仅是对程序的改进,删掉也不影响程序的功能和运行****void main(){STU *listA,* listB,*head;printf("Please input first link listA information:\n");listA =Create ( );print(listA);printf("\nPlease input second link listB information:\n");listB =Create ( ) ;print(listB);printf("\nThe merged link is:\n");head=merge(listA, listB);print(head);getch( ); /*为便于观察结果,等待键入任意键*/}。

C语言教材课后习题参考答案.docx

C语言教材课后习题参考答案.docx
printf( “\n”);
}
}
}
4、
main()
{int i, a, min, max;
scanf(“%d" , &a);
min=a;max=a;
for(i=2;i<=100;i++)
{ scanf(“,&a);
if (a<min) min=a;
if (a>max) inax=a;
}
printf (<<Max=%d, Min=%d\nv, max, min);
3、存储单元、符号地址、内存地址
4、十、十六、八
5、double(双精度型)
6、8
7、5.500000
8、a二-32768
9、+0017,021,0x11
三、写程序运行结果
3257
32 57
7.8& -345.12,7. 8765,-345.1230
7.87654e+00,-3. 5e+02
a,97, 141,61
main()
{int x, y;
for(x=20;x<=100;x++)
for (y=20;y<=100;y++)
if(x*x+y二二1053&&x+y*y二二873)
printf (i(x=%dy=%d\n” , x, y);
}
7、
main()
{
long int k, g, s=0;
printf ("Please input an integer:,z);
Printf(is not a quadratic);

C语言教材后习题及答案(DOC)

C语言教材后习题及答案(DOC)

第一章1.什么是程序?什么是程序设计语言?2.C语言有什么特点?3.什么是算法?算法的描述有哪些基本方法?第一章主要关注两个概念:一是程序的三种结构(顺序、分支或叫选择、循环);二是函数,主函数main()第二章1.指出下列哪些是C语言合法的用户识别符。

void a3_b3 For 2a define _123 _abc DO2.指出下列哪些是C语言合法的常量。

"3'4"" 0x1g 33"a" E20 '\18' \0xa' 1.E-5 3.F '\0' .12'\a'"3'4"" 双引号内表示字符串,但要在一个串内含有单引号、双引号要用转义字符正确的是:"3\’4\""0x是十六进制的表示形式之一,但十六进制里没有g这个数值\0xa' 带一个单引号、0开头,无意义.这里要注意\ddd,1-3位八进制数,\xhh,1-2位十进制数,他们都表示对应的ASCII值E20,E表示指数形式,但两边必须都有数字。

将此题全部弄懂,所有关于常量的分基本跑不掉了。

3.指出下列定义变量语句中的错误。

int a;b=5; //同类型的两个变量,中间不能用分号。

doubel h; //doubleint x,float y; //中间要用分号float f(x); //非法符号定义变量要注意,数据类型的关键字,还有就是变量名称必须是合法标识符。

4.写出能表示数学式子1╱(2a+1╱2a)的C语言表达式。

1.0/(2*a+1/(2*a))5.⑴#include<stdio.h>int main(){double x=1,y;y=x+3/2;printf("y=%f\n",y);return 0;}结果是:2.000000,注意3/2=1(2)#include<stdio.h>int main(){float x=2.9,y=3.2,z;int a=8;z=x+a%3*(int)(x+y)/2;printf("z=%f\n",z);return 0;}结果是:8.900000,注意(int)(x+y)=6。

湘大C程序设计题库答案(3套)

湘大C程序设计题库答案(3套)
请勿改动主函数main和其它函数中的任何内容仅在函数fun的花括号中填入你编写的若干语句
题+答案
书梦空间 2017.5
不足和错误之处,希望给出宝贵的意见.谢谢!

/*------------------------------------------------------【程序填空】 ---------------------------------------------------------
double sum = 0.0; int c = 0, i = 0; /***********FOUND***********/ while(x[i]==0) {
if(x[i]<0) {
sum=sum+x[i]; c++; } i++; } /***********FOUND***********/ sum=sum\c; return sum; } int main() { int x[1000]; int i=0;
【?】 int main() {
int x; double y,z; /***********SPACE***********/ scanf("【?】",&x,&y); z=2*x*sqrt(y); /***********SPACE***********/ printf("z=【?】",z); return 0; }
注意:不要改动其他行,不得增行或删行,也不得更改程序的结构。
-------------------------------------------------------*/ #include <stdlib.h> #include <conio.h> #include <stdio.h> double fun(int x[ ]) {

C语言课后习题答案

C语言课后习题答案

C语言课后习题答案第1章 C语言概述一、选择题答案: 1.A 6.B 二、填空题答案: 1.函数一、选择题答案: 1.B 6.C 11.B 16.C2.D 7.C 12.B 17.B3.D 4.B 8.B 9.A 13.D 14.D 18.D 19.A5.D 10.D 15.A 20.C2.目标3.编译程序4.分号2.B 7.D3.C 8.B4.C5.D第2章数据类型、运算符和表达式二、填空题答案: 1.2 度6.307.4,2一、选择题答案: 1.C 6.A 二、填空题答案:1.scanf printf 2.顺序、选择、循环 3.1 4.3 5.9,10 6.10,20 7.213 8.空格、Tab键、回车键2.C 7.D3.C 4.A 8.D 9.C5.A 10.D8.42.6,4,23.184.5 5.double或双精第3章顺序结构第4章选择结构一、选择题答案:1.B 6.C2.D 7.C3.D 8.B4.B 9.B5.B二、程序改错题1.输入两个实数,在fun()函数中按数值由小到大输出它们。

(保留2位小数)答案:=======(答案1)======= float t;=======(答案2)======= if(a>b)=======(答案3)=======printf(\2.编写一个程序计算某年某月有几天。

(注意要区分闰年)答案:=======(答案1)======= switch(mm)=======(答案2)======= break;=======(答案3)======= default : 三、程序填空题1.功能:输出a、b、c三个变量中的最小值。

答案:=======(答案1)======= &c=======(答案2)======= c:t1四、程序设计题1.功能:对任意输入的x,用下式计算并输出 y 的值。

说明:因为关于函数调用的知识在第7章讲解,所以在现阶段本程序的设计可以改用一个主函数来完成。

c语言第五版课本课后习题答案

c语言第五版课本课后习题答案

c语言第五版课本课后习题答案C语言是一门广泛应用的编程语言,其第五版课本通常包含了大量的理论知识和实践练习。

课后习题是帮助学生巩固和检验学习成果的重要部分。

以下是一些C语言第五版课本课后习题的答案示例,请注意,这些答案仅供参考,具体答案可能因课本版本和习题内容的不同而有所变化。

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

答案: C语言是一种结构化编程语言,具有高效性、可移植性、丰富的运算符和数据类型等特点。

它广泛应用于系统软件、嵌入式系统等领域。

习题2:解释编译型语言和解释型语言的区别。

答案:编译型语言在执行前需要通过编译器将源代码转换为机器语言,生成可执行文件;而解释型语言在执行时由解释器逐行解释执行,不需要编译过程。

# 第二章:数据类型与变量习题1:列举C语言中的基本数据类型。

答案: C语言的基本数据类型包括整型(int)、浮点型(float和double)、字符型(char)等。

习题2:什么是变量?变量声明的一般形式是什么?答案:变量是存储数据值的容器,其名称可以代表内存中的一个存储位置。

变量声明的一般形式为:`数据类型变量名;`。

# 第三章:控制结构习题1:什么是条件语句?列举C语言中的条件语句类型。

答案:条件语句用于根据条件的真假来控制程序的执行流程。

C语言中的条件语句包括if语句、if...else语句和switch语句。

习题2:循环语句的作用是什么?C语言中有哪些循环语句?答案:循环语句用于重复执行一段代码直到满足特定条件。

C语言中的循环语句包括for循环、while循环和do...while循环。

# 第四章:函数习题1:函数的定义是什么?函数的作用是什么?答案:函数是一段具有特定功能的代码块,可以被重复调用。

函数的作用是实现代码的模块化,提高代码的复用性。

习题2:函数声明和函数定义的区别是什么?答案:函数声明提供了函数的名称、返回类型和参数列表,但不包含函数的实现细节;函数定义则包含了函数的实现代码。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include<stdio.h>
#include<math.h>
void main()
{
int i,a,b;
for(i=1000;i<=9999;i++)
{
a=i%100;
b=i/100;
if(i==pow((a+b),2))
printf("%d\n",i);
}
}
6.8
#include<stdio.h>
for(i=0;i<8;i++)
{
printf("%*d",18-i*2,a[i][0]);
for(j=1;j<=i;j++)
printf("%4d",a[i][j]);
printf("\n");
}
}
7.6
#include<stdio.h>
#include<string.h>
void main()
{
a[i]=a[i]-32;
}
void main()
{
char a[100];
printf("输入字符串:\n");
gets(a);
strupr(a);
printf("转化后的字符串:\n");
puts(a);
}
//(4.字符串大写字母改为小写字母)//
#include<stdio.h>
void strlwr(char a[100])
void main()
{
int f[10]={1,1};
int i,pow=0;
for(i=2;i<=9;i++)
f[i]=3*f[i-1]+2*f[i-2];
for(i=0;i<=9;i++)
pow=pow+f[i];
printf("第十项是:%d,前十项和为:%d\n",f[9],pow);
}
7.5
int main()
{
int num,a[10],m;
int i=0,j,flag=1;
scanf("%d",&num);
do
{
m=num%10;
a[i]=m;
num=num/10;
i++;
}
while (num%10!=0);
for (j=0,i--;i>=j;i--,j++)
if (a[j]==a[i])
void Fun(int a)
{
char h[16];
int i;
i=15;
while(a)
{
int x=a%16;
if(x<10)
h[i]=x+'0';
else
h[i]=x-10+'A';
a=a/16;
i--;
}
printf("十六进制:");
i++;
while(i<16)
{
printf("%c",h[i]);
void main()
{
int i,a,b,c;
for(i=100;i<1000;i++)
{
a=i%10;
b=i/10%10;
c=i/100;
if(((a==b)&&(b!=c))||((a==c)&&(b!=c))||((b==c)&&(a!=b)))
printf("%d\n",i);
}
}
6.9
#include<stdio.h>
gets(a);gets(b);
strcat(a,b);
printf("连接后的字符串:\n");
puts(a);
}
//(3.字符串小写字母改为大写字母)//
#include<stdio.h>
void strupr(char a[100])
{
int i;
for(i=0;i<=100;i++)
if(a[i]>=97&&a[i]<=122)
scanf("%d",&num);
if(num%2!=0)
{
printf("是奇数\n");
if(num>1)
{
for(i=2;i<=num/2;i++)
{if(num%i==0){leap=0;break;}
else leap=1;}
if(leap) printf("是素数\n");
else printf("不是素数\n");
int i,m;
char a[100],b[100];
gets(a);gets(b);
if(strcmp(a,b)!=0)
{
if(strlen(a)>strlen(b))
{
strcat(a,b);
puts(a);
}
else
{
strcat(b,a);
puts(b);
}
}
}
8.1
#include <stdio.h>
void main()
{
int i,n=1,m;
for(i=1;i<10;i++)
n=(n+1)*2;
printf("第一天摘了%d个桃子\n",n);
}
7.1
#include <stdio.h>
void main()
{
int long a;
char h[16];
int i;
scanf("%ld",&a);
{
case 0:y=cos(x+3.0);break;
case 1:y=pow(cos(x+7.5),2);break;
case 2:y=pow(cos(x+4.0),4);break;
}
printf("%lf\n",y);
}
5.4
#include<stdio.h>
void main()
{
int num,i,leap;
{
int i;
for(i=0;i<=100;i++)
if(a[i]>=65&&a[i]<=90)
a[i]=a[i]+32;
}
void main()
{
char a[100];
printf("输入字符串:\n");
if(a%i==0) b=b+i;
for(n=0,i=1;i<=b/2;i++)
if(b%i==0) n=n+i;
if(n==a&&a<b)
printf("%d和%d\n",a,b);
}
}
6.5
#include<stdio.h>
#include<math.h>
void main()
{
int num,a,b,c,p;
#include<stdio.h>
void strcat(char *c,char *d)
{
while(*c!=0)
c++;
while(*d!=0)
{
*c=*d;
c++;
d++;
}
*c=0;
}
void main()
{
char a[100],b[100];
printf("分别输入字符串a和b:\n");
}
}
else printf("是偶数\n");
}
5.5
#include <stdio.h>
void main()
{
int a,b,c,i,n;
for(i=0;i<10000;i++)
{
for(n=1;n<10000;n++)
{
a=i;b=a+n;c=b+n;
if(a*c==5*b&&b+c==8*a)
while(*(d+i)!='\0')
{
*(c+i)=*(d+i);
i++;
}
*(c+i)='\0';
}
void main()
{
char a[100],b[100];
printf("输入一个字符串:");
gets(b);
strcpy(a,b);
printf("复制后的字符串:%s\n",a);
}
//(2.字符串的连接)//
i++;
}
printf("\n");
}
void main()
{
int n;
相关文档
最新文档