C语言第五六次作业
- 格式:ppt
- 大小:101.50 KB
- 文档页数:40
3. 字母频率统计【问题描述】编写程序从标准输入中读入一段英文,统计其中小写字母出现次数,并以柱状图的形式显示其出现次数。
【输入形式】在标准输入上输入一段英文文章(可能有一行,也可能有多行),在新的一行的开头输入ctrl+z键表示结束。
【输出形式】在屏幕上依次输出表示每个小写字母出现次数的柱状图(以“*”字符表示柱状图,空白处用空格字符表示,某个小写字母出现多少次,就显示多少“*”字符;柱状图的高度以出现最多的字母次数为准),在最后一行依次输出26个小写字母。
【样例输入】The computing world has undergone arevolution since the publication ofThe C Programming Language in 1978.【样例输出】【样例说明】在输入的英文短文中,小写字母a出现了6次,所以其上输出了6个字符"*"。
出现次数最多的是字母n,所以柱状图的高度为9个字符。
字母j没有出现,所以其上都为空格字符。
【评分标准】该题要求输出柱状图表示的字母出现次数,共有5个测试点。
上传C语言文件名为bar.c。
【输入形式】从标准输入读取顶点坐标。
格式为:第一行是点的个数N(3≤N≤15),后面紧接着N 行,每行两个数字(由空格隔开),分别表示该点的X、Y坐标(0≤X,Y≤32767)。
所有点的坐标互不相同,且按顺时针次序给出。
输入数据确保该多边形是一个凸多边形。
【输出形式】向标准输出打印一个浮点数,是该多边形的面积。
该浮点数保留两位小数。
【输入样例】 43 330101 2【输出样例】5.00【样例说明】输入数据表示了如图所示的四边形。
其面积为5.00。
提示:求三角形面积可用海伦公式,求平方根可用<math.h>头文件中定义的sqrt函数。
【评分标准】结果完全正确得20分,每个测试点4分。
提交程序名为:points.c。
C语言作业题【输出形式】用户在下一行显示输出结果【样例输入】3.47【样例输出】3【样例说明】与输入值3.47接近的整数为3和4,3.47比3.5小,舍掉小数部分,结果为3。
【评分标准】结果完全正确得20分,每个测试点4分。
提交程序名为:c0104.c。
当前编程题:第一次作业--- 计算空格换行个数5. 【问题描述】编写一程序,分别计算所输入的字符串中空格,换行符的个数。
【输入形式】输入可以是键盘上的任意字符。
【输出形式】分别输出空格,换行符的个数,输出的结果一行显示,数字之间以空格格开。
【输入样例】bb ss pp=fz【输出样例】2 1【样例说明】字符b与s之间有一个空格,s与p之间有一个空格,p与f之间有一个换行。
【评分标准】结果完全正确得20分,每个测试点4分。
提交程序名为:c0105.c。
当前编程题:第一次作业--- 整数组合(选做题)6. 【问题描述】假设unsigned short类型占16位,x、y、z都是short类型。
编写一程序,从键盘输入x,y 的值(十进制形式),然后将x的高8位作为z的高8位,y的高8位作为z的低8位,最后输出z的值(十进制形式)。
【输入形式】以十进制形式输入x和y的值,以空格隔开。
【输出形式】以十进制形式在新的一行上输出z值。
【输入样例】840 2177【输出样例】776【样例说明】840的二进制形式为:0000 0011 0100 10002177的二进制形式为:0000 1000 1000 0001将840的高8位作为结果的高8位,2177的高8位作为结果的低8位,所以结果为:0000 0011 0000 1000,即十进制形式为:776【评分标准】结果完全正确得20分,每个测试点4分。
提交程序名为:c0106.c。
当前编程题:第二次作业--- 数字排序1. 【问题描述】编写一个程序,将输入的三个数从小到大排列出来。
【输入形式】用户在第一行输入三个数字,每个数字用空格分开。
第五次上机作业:(请用指针实现)1.数据移位有n(n<=10)个整数,使前面各数顺序向后移m(m<=n)个位置,最后m 个数变成最前面的m个数,如图所示。
编写函数shift(a,n,m)实现上述功能,在主函数中输入n个整数和输出调整后的n个数。
SAMPLE OUTPUTnInput n(n<=10):10Input 10 integers:Output the original:Output the shifted:#include <>#define N 10void shift(int a[ ],int n,int m);void main( ){ int i,n,m;int a[N];printf("Input n(n<=10):");scanf("%d",&n);printf("Input %d integers:\n",n);for(i=0;i<n;i++)scanf("%d",a+i);printf("Output the original:\n");for(i=0;i<n;i++)printf("%d ",*(a+i));printf("\n");printf("Input m(m<=%d):",n);scanf("%d",&m);shift(a,n,m);printf("Output the shifted:\n");for(i=0;i<n;i++)printf("%d ",*(a+i));printf("\n");}void shift(int a[ ],int n,int m){ int i,j,t;for(i=0;i<m;i++){ t=a[n-1];for(j=n-2;j>=0;j--)a[j+1]=a[j];a[0]=t;}}2.字符串排序在主函数中输入10个不等长的字符串放入二维字符数组中,编写函数sort()利用指针数组对其排序,在主函数中输出排好序的字符串。
1.输入一个不超过五位的正整数,输出其逆数。
例如输入12345,输出应为54321。
#include<stdio.h>main(){int s[5];int n,j;printf("请输入一个正的五位数");scanf("%d",&n);s[0]=n%10;printf("%d",s[0]);s[1]=(n%100-s[0])/10;printf("%d",s[1]);s[2]=(n%1000-s[0]-s[1])/100;printf("%d",s[2]);s[3]=(n%10000-s[0]-s[1]-s[2])/1000;printf("%d",s[3]);s[4]=(n%100000-s[0]-s[1]-s[2]-s[3])/10000;printf("%d",s[4]);printf("输出逆转后的5位数");for(j=0;j<=4;j++){printf("%d",s[j]);}}2.计算1+2+3…+n的值,n是从键盘输入的自然数。
#include <stdio.h>main(){int n;int i;printf("请输入一个自然数:");scanf("%d",&n);i=n*(n+1)/2;printf("输出1-n的自然数之和:%d\n",i);}3.从终端(键盘)读入20个数据到数组中,统计其中正数的个数,并计算这些正数之和。
#include <stdio.h>#define naxx 10main(){int s[naxx];int a[naxx];int i=0;int j=0;int l=0;int sum=0;int k;printf("请输入:");for(i=0;i<naxx;i++){scanf("%d",&s[i]);}for(j=0;j<naxx;j++){ if(s[j]>0){a[l]=s[j];sum+=a[l];l++;}}for(i=0;i<l;i++){ printf("%d\n",a[i]);}printf("输入数之和%d\n",sum);}4.从终端(键盘)将5个整数输入到数组a中,然后将a逆序复制到数组b中,并输出b中各元素的值。
练习5-1答案一、选择题1.合法的数组说明语句是( B )。
A.int a[]="string"; B.int a[]={0,1,2,3,4,5};C.char a="string"; D.char a[5]={'0', '1', '2', '3', '4', '5'}; 2.以下对一维整型数组a的说明正确的是( D )。
A.int a(10); B.int n=10, a[n]; C.int n; D.#define SIZE 10scanf("%d", &n); int a[SIZE];int a[n];3.已知:int a[10];,则对a数组元素的正确引用是( D )。
A.a[10] B.a[3.5] C.a(5) D.a[10-10]4.以下对一维数组a进行正确初始化的语句是( C )。
A.int a[10]=(0, 0, 0, 0, 0); B.int a[10]={};C.int a[]={0}; D.int a[2]={10, 9, 8};5.对以下说明语句的正确理解是( B )。
int a[10]={6, 7, 8, 9, 10};A.将5个初值依次赋给a[1]至a[5]B.将5个初值依次赋给a[0]至a[4]C.将5个初值依次赋给a[6]至a[10]D.因为数组长度与初值的个数不相同,所以此语句不正确二、填空题6.求所有不超过200的N值,N的平方是具有对称性质的回文数。
所谓回文数就是将一个数从左到右与从右到左读都是一样的,例如:34543和1234321都是回文数。
例如:满足题意要求的数有:N=1,11*11=121;N=111,111*111=12321。
#include <stdio.h>main(){int m[16], n, i, t, count=0;long a, k;printf("Result is:\n");for (n=10; n<200; n++){ k=0;t=1;a=n*n;for (i=1; a!=0; i++){ ①;a/=10;}for (; i>1; i--){ k+=m[i-1]*t;②;}if ( ③) printf("%2d:%10d %10d\n", ++count, n, n*n);}}则①为( m[i]=a%10; )、②为( t*=10; )、③为( k==n*n )。
C语言及实验作业参考做法分支程序设计1.输入4个整数,按从小到大的顺序输出2.根据月份判断季节。
输入月份,输出春/夏/秋/冬。
要求用switch语言实现3.学生学习两门课:必修课成绩为course1,选修课成绩为course2,若两门都及格,打印pass,必修课不及格,打印fail4.从键盘输入一个年份,判该年是否是闰年,是闰年就输出yes,否则输出no 5.从键盘输入一个字符,判断该字符是数字字符、大写字母、小写字母、空格还是其他字符。
6.商店卖西瓜,10斤以上每斤0.8元, 8斤以上每斤0.6元, 6斤以上每斤0.4元,4斤以上每斤0.3元, 4斤以下每斤0.15元,输入西瓜重量和所付的钱数,输出应付的货款和应找的钱数循环程序设计1.从键盘上输入10个整数,计算这些整数的和2.水仙花数。
一个3位数,若其各位数字之和等于它本身,这个数就称为水仙花数.输出所有的水仙花数。
(如153)#include<stdio.h>int main()//方法一{ int num,bai,shi,ge;for(num=100; num<1000; num++){bai=num/100;shi=num/10%10;ge=num%10;if(num==bai*bai*bai+shi*shi*shi+ge*ge*ge)printf("%d\n",num);}return 0;}//方法二int main(){int i,j,k;for(i=1; i<10; i++)for(j=0; j<10; j++)for(k=0; k<10; k++)if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)printf("%d\n",100*i+j*10+k);return 0;}3.计算分数序列2/1,3/2,5/3,8/5,13/8,21/13,.....前20项之和。
练习55.1 计算循环体的执行次数并上机验证。
1. int i = 0;2. short i = 1;while(1) while(i > 0){ {++i; ++i;printf("%d\n", i); printf("%d\n", i);} }3. short i = 1;4. char c = 'a';while(i * i >= 0) while(c >= 0){ {++i; --c;printf("d\n", i); printf("%c\n", c)} }答:1.表达式1恒真,因此循环体会执行无数次。
2.在数学上,变量i的初值为1,每次自增1后会一直大于0,也就是说表达式i > 0恒真,但是,计算机中整型构成一个环,short型变量的取值范围为-32768~32767,当变量i的值自增到32767后再加1就变成了-32768,表达式i 〉0为假,循环退出,因此循环体执行了32767次。
3.表达式i * i >= 0恒真,因此循环体会执行无数次。
4.字符型变量c的值为97号字符’a’,因此从97至0,循环体共执行了98次。
5.2 画出例5-2的程序流程图。
5.3 整数n为几时1.0/n小于10-5。
答:5.4 编程输出整数n的阶乘。
(n! = n * (n-1) * … * 2 * 1)答:与例5-2相同的:0的阶乘等于1,此程序能正确输出吗?for结构的程序。
还可以用:与前面两个程序相比,少用了一个变量。
5.5 编程输出一个正整数各位上数字中零的个数,正整数由用户输入。
5.6 编程输出正整数各位上数字中的最大数。
或者(注意体会两者的区别)5.7 以小僧的人数为循环变量求解例5-4。
答:注意:在用穷举法解决此类问题时要防止因四舍五入而出错的情况。
如此题中设大僧每人2个馍,小僧4人一个馍,则问题应该无解,但用例5-4的算法求解时程序却会给出错误的结果。
第五章习题答案一、选择题1) 有以下程序#include <stdio.h>main(){ int n=2,k=0;while(k++&&n++>2);printf("%d %d\n",k,n);}程序运行后的输出结果是A)0 2 B)1 3 C)5 7 D)1 22)有以下程序:main(){ int k=5;while(--k) printf("%d",k-=3);printf("\n");}执行后的输出结果是()。
A)1 B)2 C)4 D)死循环3)有以下程序#include<stdio.h>main(){int y=10;while(y--);printf(”y=%d\n”,y);}程序执行后的输出结果是A)y=0 B)y= -1 C)y=1 D)while构成无限循环4) 若变量已正确定义,有以下程序段i=0;do printf("%d,",i);while(i++);printf("%d\n",i)其输出结果是A)0,0 B)0,1 C)1,1 D)程序进入无限循环5)有以下程序:main(){ int i;for(i=1;i<=40;i++){ if(i++%5==0)if(++i%8==0) printf("%d",i); }printf("\n");}执行后的输出结果是()。
A)5 B)24 C)32 D)406) 有以下程序#includes <stdio.h>main(){ int a=1,b=2;for(;a<8;a++) {b+=a; a+=2;}printf ("%d,%d\n",a,b);}程序运行后的输出结果是(A)9,18 (B)8,11 (C)7,11 (D)10,147) 有以下程序#include<stdio.h>main(){int y=9;for( ; y>0;y--)if(y%3= =0) printf("%d",--y);}程序的运行结果是A)741 B)963 C)852 D)8) 一下程序段中的变量已正确定义for(i=0;i<4;i++,j++)for(k=1;k<3;k++); printf("*");程序段的输出结果是A)******** B)**** C)** D)*9) 有以下程序#iinclude <stdio.h>main(){ int i,j;for(i=3; i>=1; i--){ for (j=1;j<=2;j++) printf(“%d”,i+j);printf(“\n”);}}程序的运行结果是()A) 2 3 4 B) 4 3 2 C) 2 3 D) 4 53 4 5 5 4 3 3 4 3 44 5 2 3 答案:D10)有以下程序main(){int i,j;for(i=1;i<4;i++){for(j=i;j<4;j++) printf(“%d*%d=%d “,i,j,i*j);printf(“\n”);}}程序运行后的输出结果是A) 1*1=1 1*2=2 1*3=3 B) 1*1=1 1*2=2 1*3=32*1=2 2*2=4 2*2=4 2*3=63*1=3 3*3=9C) 1*1=1 D) 1*1=11*2=2 2*2=4 2*1=2 2*2=41*3=3 2*3=6 3*3=9 3*1=3 3*2=6 3*3=9 11) 有以下程序#include<stdio.h>main(){int i,j,m=55;for(i=1;i<=3;i++)for(j=3;j<=i;j++) m=m%j;printf("%d\n",m);}程序的运行结果是A)0 B)1 C)2 D)312) 有以下程序#include <stdio.h>main(){ int i=5;do{ if (i%3= =1)if (i%5= =2){printf(“*%d”,i); break; }i++;}while(i!=0);printf(“\n”);}程序的运行结果是A)*7 B)*3*5 C) *5 D)*2*6答案:A13) 有以下程序#include <stdio.h>main(){ int c=0,k;for(k=1;k<3;k++)switch(k){ default:c+=k;case 2:c++;break;case 4:c+=2;break;}printf("%d\n",c);}程序运行后的输出结果是A)3 B)5 C)7 D)914) 设变量已正确定义,以下不能统计出一行中输入字符个数(不包含回车符)的程序段是A)n=0;while((ch=getchar())!='\n')n++;B)n=0;while(getchar()!='\n')n++;C)for(n=0;getchar()!='\n';n++);D)n=0;for(ch=getchar();ch!='\n';n++);15)有以下程序#includemain(){int a=1,b=2;while(a<6){b+=a;a+=2;b%二10;}printf(”%d,%d\n”,a,b);}程序运行后的输出结果是A)5,11 B)7,1 C)7,11 D)6,116) 要求通过while循环不断读入字符,当读入字母N时结束循环。
2020年C语言程序设计苏小红版第五六单元答案精编版第五章5.1#include<stdio.h>void main(){double x;printf("请输入一个实数:\n");scanf("%lf",&x);if(x>0){printf("%f\n",x);}else{printf("%f\n",-x);}}5.2#include <stdio.h>main(){int a,b;printf("请输入一个整数:");scanf("%d",&a);b=a%2;if(b==0){printf("%d为偶数\n",a);}else{printf("%d为奇数\n",a);}}5.3#include<stdio.h>#include<math.h>main(){float a,b,c,s,area;printf("请输入三边长:");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("这个三角形的面积为%f\n",area);}else{printf("这三条边不能组成三角形\n");}}5.4#include<stdio.h>#include<math.h>void main(){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=%f\n",x1,x2);}else if(b*b-4*a*c==0){x1=(-b)/(2*a);printf("x1=x2=%f\n",x1);}else if(b*b-4*a*c<0){printf("该方程没有实数解.\n");}}5.5#include<stdio.h>main(){int year,flag;printf("输入年份:");scanf("%d",&year);if(((year%4==0)&&(year%100!==0))||(year%400==0)){flag=1;}else{flag=0;}if(flag==1){printf("%d是闰年\n",year);else{printf("%d不是闰年\n",year);}}5.6#include<stdio.h>void main(){int year,flag;printf("输入年份:");scanf("%d",&year);flag=(((year%4==0)&&(year%100!=0))||(year%400==0))?1:2; if(flag==1){printf("%d年是闰年\n",year);}else{printf("%d年不是闰年\n",year);}}5.7#include<stdio.h>main(){char ch;printf("请输入一个字母:");ch=getchar();if((ch>=65)&&(ch<=90))ch=ch+32;printf("%c,%d\n",ch,ch);}else if((ch>=97)&&(ch<=122)){ch=ch-32;printf("%c,%d\n",ch,ch);}else{printf("%c\n",ch);}}5.8#include<stdio.h>main(){char ch;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<stdio.h>main(){int score,mark;printf("Please enter score:");scanf("%d",&score);if(score<0||score>100){printf("Input error!\n");}else if(score>=90&&score<=100){printf("%d--A\n",score);}else if(score>=80&&score<90){printf("%d--B\n",score);}else if(score>=70&&score<80){printf("%d--C\n",score);}else if(score>=60&&score<70){printf("%d--D\n",score);}else{printf("%d--E\n",score);}}5.10#include <stdio.h>#include <stdlib.h>main(){int flag, year,month,day,day1=31,day2=30,day3=29,day4=28;printf("请以2012,03这种形式输入某年某月 :");scanf("%d,%d",&year,&month);if (month>12||month<1){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<stdio.h>main(){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<stdio.h>main(){double t,w,h;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 <stdio.h>main(){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");}}6.1(2)#include <stdio.h>main(){int k=4,n;for (n=0;n<k;n++){if (n%2==0) continue;k--;}printf("k=%d\n,n=%d\n",k,n); }6.1(3)#include <stdio.h>main(){int k=4,n;for (n = 0;n < k;n++){if (n%2 ==0) break;k--;}printf("k=%d,n=%d\n",k,n); }6.2(1)#include <stdio.h>main(){int i,sum = 0;for (i=1;i<=101;i=i+2){sum=sum+i;}printf("sum = %d\n",sum);}6.2(2)#include <stdio.h>main(){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\n",sum);}6.2(3)#include <stdio.h>main(){long term=0,sum=0;int a,i,n;printf("Input a,n:");scanf("%d,%d",&a,&n);for (i = 1;i<=n;i++){term = term * 10 + a;sum=sum+term;}printf("sum = %ld\n",sum);}6.2(4)#include<stdio.h>#include<math.h>main(){int n = 1;float term = 1.0,sign = 1,sum = 0;while (fabs(term) >= 1e-4){term = sign / n;sum = sum + term;sign = -sign;n++;}printf("sum = %f\n");}6.2(5)#include<stdio.h>#include<math.h>main(){int 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<stdio.h>main(){int x=1,find=0;while (!find){if (x%2==1 && x%3==2 && x%5==4 && x%6==5 && x%7==0){find=1;}x++;}printf("x=%d\n",x-1);}6.4#include<stdio.h>main(){int i,n;long p,j;printf("Please enter n:");scanf("%d",&n);for (i=1;i<=n;i++){p=i*i;j=i*i*i;printf("%d * %d = %ld\n",i,i,p);printf("%d * %d * %d= %ld\n",i,i,i,j);}}6.5#include<stdio.h>main(){double C,F,i;for (F = -40;F <= 110;F = F + 10){C = (F-32)*5/9;printf("F=%f C=%f\n",F,C);}}6.6#include<stdio.h>main(){double p = 1 + 0.01875*12,x4,x3,x2,benjin;x4 = 1000/p;x3 = (x4 + 1000)/p;x2 = (x3 + 1000)/p;benjin = (x2 + 1000)/p;printf("本金为:%f",benjin);}6.7#include<stdio.h>main(){double count = 0,c ,d , sum = 100;printf("Input c :");scanf("%lf",&c);d = c / 100.0;do{sum = sum * (1+d);count++;}while (sum <= 200);printf("count = %f\n",count);}6.8#include<stdio.h>#include<math.h>main(){int count = 1;double sum = 1.0 ,term,n = 3.0,i = 1.0;do {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 <stdio.h>#include <math.h>main(){int count = 1;double sum = 1.0 , term = 1.0, n = 1.0;while (fabs(term) >= 1e-5){term = term / n;sum = sum + term;n++;count++;}printf("e = %lf,count = %d.\n",sum,count); }6.10#include <stdio.h>main(){int i , a , b , c ,d;for (i = 100 ; i <= 999 ; i++){a = i /100;c = i % 10;b = (i - a * 100 - c) / 10;d = a * a * a + b * b * b + c * c * c;if (i == d){printf("i = %d\n",i);}}}6.11#include <stdio.h>main(){long m = 2 , n ,term = 1 ,sum = 1;printf("Input n:");scanf("%ld",&n);do {term = term * m;sum = sum + term ;m++;}while(sum <= n);printf("m = %d.\n",m - 2);6.12#include <stdio.h>main(){int num = 1,sum = 0 , count = 0;while (num > 0){printf("Input num :");scanf("%d",&num);sum = sum + num ;count++;}printf ("sum = %d,count = %d.\n",sum, count - 1); }6.13#include <stdio.h>main(){int term , sum = 0 , count = 0;do {begin:printf("Input term : ");scanf ("%d",&term);if (term > 0){sum = sum + term ;count++;}else if (term < 0)goto begin;elsegoto end ;}while (term != 0);end : printf ("sum = %d. count = %d.\n",sum,count );}6.14#include <stdio.h>main()int a,b,c;for (a = 0;a < 17;a++)for (b = 0;b <= 25 ;b++){c = 30 - a - b ;if (a * 3 + b * 2 + c == 50)printf ("男 = %d,女 = %d,孩子 = %d.\n",a,b,c);}}6.15#include<stdio.h>main(){int a,b;for (a = 0; a <=98 ; a++){b = 98 - a;if (a * 2 + b * 4 == 386){printf ("鸡有%d只,免有%d只.\n",a,b);}}}6.16#include <stdio.h>main(){int x,y,z;for (x = 0;x <= 20;x++){for (y = 0;y <= 33;y++){z = 100 - x - y;if (5 * x + 3 *y + z / 3.0 == 100){printf ("x = %d,y = %d,z = %d\n",x, y, z);}}}}6.17不确定#include<stdio.h>main(){int x, y, z;for (x = 1; x < 10; x++){for (y = 1; y < 20; y++){z = 50 - x - y;if (x * 10 + y * 5 + z == 100)printf("x = %d, y = %d, z = %d\n",x ,y ,z);}}}6.18上:#include<stdio.h>main(){int i, j;for (i = 1; i <= 9; i++){for (j = 1; j <= 9; j++){printf("%-5d",i * j);}printf("\n");}}左:#include<stdio.h>main(){int i, j;for (i = 1; i <= 9; i++){for (j = 1; j <= i; j++){printf("%-5d",i * j);}printf("\n");}}右:#include <stdio.h>main(){int i, j, a = 1, k;char b = ' ';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<stdio.h>main(){long i,summo;double sumfu = 0,term = 0.01;summo = 100000 * 30;for (i = 1;i <= 30;i++){term = term * 2.0;sumfu = sumfu + term;}printf("陌生人付%ld,富翁付%lf.\n",summo,sumfu ); }本章实验题2.1#include<stdio.h>#include<stdlib.h>#include<time.h>main(){int x1, x2, c, answer;begin:srand(time(NULL));x1 = rand() % 10 + 1;x2 = rand() % 10 + 1;c = x1 * x2;do {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<stdio.h>#include<stdlib.h>#include<time.h>main(){int x1, x2, c, answer, count = 0;begin:srand(time(NULL));x1 = rand() % 10 + 1;x2 = rand() % 10 + 1;c = x1 * x2;do {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<stdio.h>#include<stdlib.h>#include<time.h>main(){int count1 = 0, count2 = 0;int x1, x2, c, answer ,i;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); }。
C语言程序设计苏小红版第五六单元答案第五章5.1#include<stdio.h>void main(){double x;printf("请输入一个实数:\n");scanf("%lf",&x);if(x>0){printf("%f\n",x);}else{printf("%f\n",-x);}}5.2#include <stdio.h>main(){int a,b;printf("请输入一个整数:");scanf("%d",&a);b=a%2;if(b==0){printf("%d为偶数\n",a);}else{printf("%d为奇数\n",a);}}5.3#include<stdio.h>#include<math.h>main(){float a,b,c,s,area;printf("请输入三边长:");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("这个三角形的面积为%f\n",area);}else{printf("这三条边不能组成三角形\n");}}5.4#include<stdio.h>#include<math.h>void main(){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=%f\n",x1,x2);}else if(b*b-4*a*c==0){x1=(-b)/(2*a);printf("x1=x2=%f\n",x1);}else if(b*b-4*a*c<0){printf("该方程没有实数解.\n");}}5.5#include<stdio.h>main(){int year,flag;printf("输入年份:");scanf("%d",&year);if(((year%4==0)&&(year%100!==0))||(year%400==0)){flag=1;}else{flag=0;}if(flag==1){printf("%d是闰年\n",year);else{printf("%d不是闰年\n",year);}}5.6#include<stdio.h>void main(){int year,flag;printf("输入年份:");scanf("%d",&year);flag=(((year%4==0)&&(year%100!=0))||(year%400==0))?1:2; if(flag==1){printf("%d年是闰年\n",year);}else{printf("%d年不是闰年\n",year);}}5.7#include<stdio.h>main(){char ch;printf("请输入一个字母:");ch=getchar();if((ch>=65)&&(ch<=90))ch=ch+32;printf("%c,%d\n",ch,ch);}else if((ch>=97)&&(ch<=122)){ch=ch-32;printf("%c,%d\n",ch,ch);}else{printf("%c\n",ch);}}5.8#include<stdio.h>main(){char ch;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<stdio.h>main(){int score,mark;printf("Please enter score:");scanf("%d",&score);if(score<0||score>100){printf("Input error!\n");}else if(score>=90&&score<=100){printf("%d--A\n",score);}else if(score>=80&&score<90){printf("%d--B\n",score);}else if(score>=70&&score<80){printf("%d--C\n",score);}else if(score>=60&&score<70){printf("%d--D\n",score);}else{printf("%d--E\n",score);}}5.10#include <stdio.h>#include <stdlib.h>main(){int flag, year,month,day,day1=31,day2=30,day3=29,day4=28;printf("请以2012,03这种形式输入某年某月 :");scanf("%d,%d",&year,&month);if (month>12||month<1){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<stdio.h>main(){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<stdio.h>main(){double t,w,h;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 <stdio.h>main(){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");}}6.1(2)#include <stdio.h>main(){int k=4,n;for (n=0;n<k;n++){if (n%2==0) continue;k--;}printf("k=%d\n,n=%d\n",k,n); }6.1(3)#include <stdio.h>main(){int k=4,n;for (n = 0;n < k;n++){if (n%2 ==0) break;k--;}printf("k=%d,n=%d\n",k,n); }6.2(1)#include <stdio.h>main(){int i,sum = 0;for (i=1;i<=101;i=i+2){sum=sum+i;}printf("sum = %d\n",sum);}6.2(2)#include <stdio.h>main(){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\n",sum);}6.2(3)#include <stdio.h>main(){long term=0,sum=0;int a,i,n;printf("Input a,n:");scanf("%d,%d",&a,&n);for (i = 1;i<=n;i++){term = term * 10 + a;sum=sum+term;}printf("sum = %ld\n",sum);}6.2(4)#include<stdio.h>#include<math.h>main(){int n = 1;float term = 1.0,sign = 1,sum = 0;while (fabs(term) >= 1e-4){term = sign / n;sum = sum + term;sign = -sign;n++;}printf("sum = %f\n");}6.2(5)#include<stdio.h>#include<math.h>main(){int 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<stdio.h>main(){int x=1,find=0;while (!find){if (x%2==1 && x%3==2 && x%5==4 && x%6==5 && x%7==0){find=1;}x++;}printf("x=%d\n",x-1);}6.4#include<stdio.h>main(){int i,n;long p,j;printf("Please enter n:");scanf("%d",&n);for (i=1;i<=n;i++){p=i*i;j=i*i*i;printf("%d * %d = %ld\n",i,i,p);printf("%d * %d * %d= %ld\n",i,i,i,j);}}6.5#include<stdio.h>main(){double C,F,i;for (F = -40;F <= 110;F = F + 10){C = (F-32)*5/9;printf("F=%f C=%f\n",F,C);}}6.6#include<stdio.h>main(){double p = 1 + 0.01875*12,x4,x3,x2,benjin;x4 = 1000/p;x3 = (x4 + 1000)/p;x2 = (x3 + 1000)/p;benjin = (x2 + 1000)/p;printf("本金为:%f",benjin);}6.7#include<stdio.h>main(){double count = 0,c ,d , sum = 100;printf("Input c :");scanf("%lf",&c);d = c / 100.0;do{sum = sum * (1+d);count++;}while (sum <= 200);printf("count = %f\n",count);}6.8#include<stdio.h>#include<math.h>main(){int count = 1;double sum = 1.0 ,term,n = 3.0,i = 1.0;do {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 <stdio.h>#include <math.h>main(){int count = 1;double sum = 1.0 , term = 1.0, n = 1.0;while (fabs(term) >= 1e-5){term = term / n;sum = sum + term;n++;count++;}printf("e = %lf,count = %d.\n",sum,count); }6.10#include <stdio.h>main(){int i , a , b , c ,d;for (i = 100 ; i <= 999 ; i++){a = i /100;c = i % 10;b = (i - a * 100 - c) / 10;d = a * a * a + b * b * b + c * c * c;if (i == d){printf("i = %d\n",i);}}}6.11#include <stdio.h>main(){long m = 2 , n ,term = 1 ,sum = 1;printf("Input n:");scanf("%ld",&n);do {term = term * m;sum = sum + term ;m++;}while(sum <= n);printf("m = %d.\n",m - 2);6.12#include <stdio.h>main(){int num = 1,sum = 0 , count = 0;while (num > 0){printf("Input num :");scanf("%d",&num);sum = sum + num ;count++;}printf ("sum = %d,count = %d.\n",sum, count - 1); }6.13#include <stdio.h>main(){int term , sum = 0 , count = 0;do {begin:printf("Input term : ");scanf ("%d",&term);if (term > 0){sum = sum + term ;count++;}else if (term < 0)goto begin;elsegoto end ;}while (term != 0);end : printf ("sum = %d. count = %d.\n",sum,count );}6.14#include <stdio.h>main()int a,b,c;for (a = 0;a < 17;a++)for (b = 0;b <= 25 ;b++){c = 30 - a - b ;if (a * 3 + b * 2 + c == 50)printf ("男 = %d,女 = %d,孩子 = %d.\n",a,b,c);}}6.15#include<stdio.h>main(){int a,b;for (a = 0; a <=98 ; a++){b = 98 - a;if (a * 2 + b * 4 == 386){printf ("鸡有%d只,免有%d只.\n",a,b);}}}6.16#include <stdio.h>main(){int x,y,z;for (x = 0;x <= 20;x++){for (y = 0;y <= 33;y++){z = 100 - x - y;if (5 * x + 3 *y + z / 3.0 == 100){printf ("x = %d,y = %d,z = %d\n",x, y, z);}}}}6.17不确定#include<stdio.h>main(){int x, y, z;for (x = 1; x < 10; x++){for (y = 1; y < 20; y++){z = 50 - x - y;if (x * 10 + y * 5 + z == 100)printf("x = %d, y = %d, z = %d\n",x ,y ,z);}}}6.18上:#include<stdio.h>main(){int i, j;for (i = 1; i <= 9; i++){for (j = 1; j <= 9; j++){printf("%-5d",i * j);}printf("\n");}}左:#include<stdio.h>main(){int i, j;for (i = 1; i <= 9; i++){for (j = 1; j <= i; j++){printf("%-5d",i * j);}printf("\n");}}右:#include <stdio.h>main(){int i, j, a = 1, k;char b = ' ';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<stdio.h>main(){long i,summo;double sumfu = 0,term = 0.01;summo = 100000 * 30;for (i = 1;i <= 30;i++){term = term * 2.0;sumfu = sumfu + term;}printf("陌生人付%ld,富翁付%lf.\n",summo,sumfu ); }本章实验题2.1#include<stdio.h>#include<stdlib.h>#include<time.h>main(){int x1, x2, c, answer;begin:srand(time(NULL));x1 = rand() % 10 + 1;x2 = rand() % 10 + 1;c = x1 * x2;do {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<stdio.h>#include<stdlib.h>#include<time.h>main(){int x1, x2, c, answer, count = 0;begin:srand(time(NULL));x1 = rand() % 10 + 1;x2 = rand() % 10 + 1;c = x1 * x2;do {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<stdio.h>#include<stdlib.h>#include<time.h>main(){int count1 = 0, count2 = 0;int x1, x2, c, answer ,i;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); }。
C语⾔作业作业1 数制与编码1.将下列⼗进制实数分别转换为⼆进制、⼋进制和⼗六进制实数:45 195 10.3125 51.8 2.将下列⼋进制和⼗六进制实数转换为⼆进制实数:(42.14)8(10.01)8(3C.24)16(7F.0E)16 3.请写出下列整数的8位⼆进制原码、反码和补码。
0 102 -15 -127 4.请写出下列整数的16位⼆进制补码(以⼗六进制形式书写)。
0 6207 -7225 -32768 5.下列16位⼆进制补码分别表⽰什么整数?00000010 10011010 11101100 010001106.⽤16位⼆进制补码形式完成下列计算:14 + (-9) = 5 (-23) + 15 = -87.下列实数的单精度存储形式是怎样的(根据Intel格式计算)?0.3125 -34.28.请写出空格、数字0、⼤写字母A和⼩写字母a等字符的ASCII码。
9.仔细观察ASCII码表, 怎样将⼩写字母转换为⼤写字母? 怎样将⼤写字母转换为⼩写字母?10.汉字“软”的区位码是4077, “件”区位码是2894, 它们的内码分别是什么?11.字符串"Welcome!"的长度是多少? 需占⽤内存空间多少字节? 最后⼀个字节是什么?12.请将下列字符串按从⼩到⼤的顺序排列:"ABC" "123" "abc""Rate1""Rate2""pen""pencil""1+2"作业2 简单的C 程序1. 物体的位移:输⼊物体的初速度v 0, 加速度a 和时间t , 计算并输出其位移。
2021at t v s += 2. 公汽车票:若乘客⾝⾼低于1.2⽶则免费, 否则收取车费2元。
3. 购物优惠活动(⼀)若顾客购物不⾜100元, 则不优惠; 购物达100元, 按九折优惠。
C语言程序设计教程第五章课后习题参考答案一、选择题1. B2. A3. C4. B5. D二、填空题1. while2. binary3. 164. 35. continue6. global三、判断题1. 错误2. 正确3. 错误4. 错误5. 正确四、编程题1.```c#include<stdio.h>int main() {int num;printf("请输入一个整数:"); scanf("%d", &num);if (num % 2 == 0) {printf("%d是偶数\n", num); } else {printf("%d是奇数\n", num); }return 0;}```2.```c#include<stdio.h>int main() {int num1, num2;printf("请输入两个整数:");scanf("%d %d", &num1, &num2);printf("%d与%d的和为%d\n", num1, num2, num1 + num2); return 0;}```3.```c#include<stdio.h>int isPrime(int num) {int i;if (num <= 1)return 0;for (i = 2; i <= num / 2; 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;}```4.```c#include<stdio.h>int factorial(int num) {int i, result = 1;for (i = 1; i <= num; i++) {result *= i;}return result;}int main() {int num;printf("请输入一个整数:");scanf("%d", &num);printf("%d的阶乘为%d\n", num, factorial(num)); return 0;}```五、简答题1. C语言逻辑与运算符(&&)短路特性是什么?答:C语言逻辑与运算符(&&)具有短路特性,即在进行逻辑与运算时,如果前一个表达式的值为假(0),则后面的表达式将不会被计算,整个逻辑与表达式的值直接为假(0)。
第五章作业:5-1、N-S流程图输入xx > 0 ?Ture False输出x 输出- x#include <stdio.h>main(){const double eps=1e-8; /*定义eps,用于输入的实数和0比大小*/ float a;printf("input a number:");scanf("%f",&a);if(a>0 || fabs(a)<=eps) /*该实数大于等于0*/{ printf("|%f|=%f\n",a,a); }else /*该实数小于0*/{ printf("|%f|=%f\n",a,-a);}}结果:5-2、传统流程图#include <stdio.h>main(){int x,m;printf("input a intanger:");scanf("%d",&x);输入整数xx > 0 ?m = x % 2m = x % 2m= = 0?输出x为偶数输出x为奇数输入错误结束开始if(x>=0){m=x%2; /*取余运算,判断余数是否为0*/if(0==m){ printf("%d是偶数\n",x);}else{ printf("%d是奇数\n",x);}}else /*提示输入为负数*/{ printf("输入错误,%d为负数\n",x);}}结果:5-3、传统流程图#include <stdio.h>#include <math.h>main(){float a,b,c,l,s;printf("input three sides:");scanf("%f,%f,%f",&a,&b,&c);if(a+b>c && a+c>b && b+c>a) /*构成三角形的条件*/{l=(a+b+c)/2;开 始输入3条边a ,b ,ca+b>c && b+c>a&& a+c>b?l=(a+b+c)/2S=sqrt(l*(l-a)*(l-b)*(l-c))输出不是三角形输出面积s 结 束s=sqrt(l*(l-a)*(l-b)*(l-c));printf("三角形的面积是%.3f\n",s);}else{ printf("不能构成三角形\n"); }}结果:5-4、#include <stdio.h>#include <math.h>main(){float m,s; /*定义本金,利息和*/int n; /*定义期限*/printf("输入本金、期限:");scanf("%f,%d",&m,&n);switch (n){case 1: /*存储年限为1年*/ s=m*pow(1+0.0225,n);printf("本金和=%.3f\n",s);break;case 2: /*存储年限为2年*/ s=m*pow(1+0.0243,n);printf("本金和=%.3f\n",s);break;case 3: /*存储年限为3年*/ s=m*pow(1+0.0270,n);printf("本金和=%.3f\n",s);break;case 5: /*存储年限为5年*/s=m*pow(1+0.0288,n);printf("本金和=%.3f\n",s);break;case 8: /*存储年限为8年*/ s=m*pow(1+0.03,n);printf("本金和=%.3f\n",s);break;default: /*提示输入错误*/printf("输入的期限有误");break;}}结果:5-5、#include <stdio.h>main(){int year,flag;printf("Enter the year:");scanf("%d",&year);if(year%100==0 && year%400==0 || year%4==0)flag=1;elseflag=0;if(flag==1)printf("%d is a leap year!\n",year);elseprintf("%d is not a leap year!\n",year);}5-6、#include <stdio.h>main(){int year,flag;printf("Enter the year:");scanf("%d",&year);(year%100==0 && year%400==0 || year%4==0)? (flag=1):(flag=0);if(flag==1)printf("%d is a leap year!\n",year);elseprintf("%d is not a leap year!\n",year);}5-7、#include <stdio.h>main(){char ch; /*定义输入的字符*/int m; /*定义该字符的ASCII值*/printf("input a string and press Enter :");ch=getchar(); /*获取该字符*/if(ch>='A' && ch<='Z') /*若为大写字符,则转化为小写*/{m=ch+32;printf("%c 的小写字母:%c\n",ch,m);}else if(ch>='a' && ch<='z') /*若为小写字符,则转化为大写并算出ASCII值*/ {m=ch-32;printf("%c 的大写字母为:%c,其ASCII值为:%d\n",ch,m,m);}else /*若输入的不是字母,则只输出ASCII值*/{printf("%c的ASCII值为:%d\n",ch,ch);}}结果:5-8、#include <stdio.h>main(){char ch;printf("input a string and press Enter :");ch=getchar(); /*获取该字符*/if(ch>='A' && ch<='Z') /*判断为大写*/{ printf("%c为大写字母\n",ch); }else if(ch>='a' && ch<='z') /*判断为小写*/{ printf("%c为小写字母\n",ch); }else if(ch==' ') /*判断为空格*/{ printf("%c为空格\n",ch); }else if(ch>='0' && ch<='9') /*判断为数字*/{ printf("%c是数字字符\n",ch);}else /*判断为其他字符*/ { printf("%c是其他字符\n",ch);}}结果:5-9#include <stdio.h>main(){int score;printf("input the score:");scanf("%d",&score);if(score>100 || score<0) /*判断师傅输入大于100的数或负数*/ { printf("成绩输入错误\n");}else{if(score>=90){ printf("score:%d-------grade:%c\n",score,'A');}else if(score>=80){ printf("score:%d-------grade:%c\n",score,'B');}else if(score>=70){ printf("score:%d-------grade:%c\n",score,'C');}else if(score>=60){ printf("score:%d-------grade:%c\n",score,'D');}else{ printf("score:%d-------grade:%c\n",score,'E');}}}结果:5-10、#include <stdio.h>main(){int year,month,flag=1; /*定义年份、月份、标识变量flag*/printf("输入年份:");scanf("%d",&year);if(year%400==0 || year%100!=0 && year%4==0) /*若为润年,flag=0,平年,flag=1*/ { flag=0;}printf("输入月份:");scanf("%d",&month);switch (month){case 1: /*1,3,5,7,8,10,12月为31天*/case 3:case 5:case 7:case 8:case 10:case 12:printf("%d年的%d月有31天\n",year,month);break;case 2: /*2月需要判断润年和平年的情况*/if(0==flag){ printf("%d年的%d月有29天\n",year,month); }else{ printf("%d年的%d月有28天\n",year,month);}break;case 4: /*4,6,9,11月为30天*/case 6:case 9:case 11:printf("%d年的%d月有31天\n",year,month);break;default: /*若输入非1~12的月份,提示输入错误*/ printf("月份输入错误\n");}}结果:第六章作业6-1(1)分析:这是一个二重嵌套循环,I=1,j=1,打出1个空格和6个*I=2,j=1和2,做2次循环打出2个空格和6个* I=3,j=1,2,3,做3次循环打出3个空格和6个* I=4,j=1,2,3,4,做4次循环打出4个空格和6个*结果6-1(2)分析:开始:k=4,n=0第一次循环n<4,n%2==0,n=n+1=1;第二次循环n<k,1%2==1,k=k-1=3,n=n+1=2; 第三次循环n<k,2%2==0,n=n+1=3;第四次循环n<k不成立结束:输出n=3,k=3结果:6-1(3)分析:开始: k=4,n=0;第一次循环:0<4,0%2==0,break,结束:输出k=4,n=0;结果:6-2(1)计算1+3+5+7+.......+99+101的值#include <stdio.h>main(){int i,sum = 0;for (i=1;i<=101;i=i+2){sum=sum+i;}printf("sum=%d\n",sum);}结果:(2)计算1*2*3+3*4*5+……+99*100*101 #include <stdio.h>main(){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\n",sum);}结果:(3)计算a+aa+aaa+…+aa…a(n个a)的值,n和a的值由键盘输入#include <stdio.h>main(){long term=0,sum=0;int a,i,n;printf("input a,n:");scanf("%d,%d",&a,&n);for(i=1;i<=n;i++){term=10*term+a; /*将相邻的数乘以10,再加上之前的数*/sum=sum+term;}printf("sum=%ld\n",sum);}结果:(4)计算1-1/2+1/3-1/4+…+1/99-1/100+…,直到最后一项的绝对值小于1e-4为止#include <stdio.h>#include <math.h>main(){int n=1;float term=1.0,sign=1,sum=0;while (sign>=1e-4){term=pow(-1,n-1)*sign; /*计算每项因子*/sum=sum+term;sign=(float)1/(n+1); /*强制类型转换*/n++;}printf("sum=%f\n",sum);}结果:(5)利用sinx=x-x^3/3!+x^5/5!-x^7/7!+….,计算最后一项的绝对值小于10(-5)时为止,输出e的值并统计加的项数#include <stdio.h>#include <math.h>main(){int n=1,count=1;float x;double sum,term;printf("input x:");scanf("%f",&x);sum=x;term=x;do{term=(-1)*x*x*term/((n+1)*(n+2));sum=sum+term;n=n+2;count=count+1;}while(fabs(term)>1e-5);printf("sin(x)=%f,count=%d\n",sum,count);}结果:6-3#include <stdio.h>main(){int x=1,find=0;while(!find){if(x%2==1 && x%3==2 && x%5==4 && x%6==5 && x%7==0){printf("x=%d\n",x);find=1;}x++;}}错误分析:1、while后不能加分号,否则进入死循环;2、判等运算用的是双等号,不是单等号;3、不管是否找,x都要自增运算,所以x++放在if语句的外面。
精心整理1.输入一个不超过五位的正整数,输出其逆数。
例如输入12345,输出应为54321。
#include<stdio.h>main(){int s[5];int n,j;printf("请输入一个正的五位数");scanf("%d",&n);s[0]=n%10;printf("%d",s[0]);s[1]=(n%100-s[0])/10;printf("%d",s[1]);s[2]=(n%1000-s[0]-s[1])/100;printf("%d",s[2]);s[3]=(n%10000-s[0]-s[1]-s[2])/1000;printf("%d",s[3]);s[4]=(n%100000-s[0]-s[1]-s[2]-s[3])/10000;printf("%d",s[4]);printf("输出逆转后的5位数");for(j=0;j<=4;j++){printf("%d",s[j]);}}2.计算1+2+3…+n的值,n是从键盘输入的自然数。
#include <stdio.h>main(){int n;int i;printf("请输入一个自然数:");scanf("%d",&n);i=n*(n+1)/2;printf("输出1-n的自然数之和:%d\n",i);}3.从终端(键盘)读入20个数据到数组中,统计其中正数的个数,并计算这些正数之和。
#include <stdio.h>#define naxx 10main(){int s[naxx];int a[naxx];int i=0;int j=0;int l=0;int sum=0;int k;printf("请输入:");for(i=0;i<naxx;i++){scanf("%d",&s[i]);}for(j=0;j<naxx;j++){ if(s[j]>0){a[l]=s[j];sum+=a[l];l++;}}for(i=0;i<l;i++){ printf("%d\n",a[i]);}printf("输入数之和%d\n",sum);}4.从终端(键盘)将5个整数输入到数组a中,然后将a逆序复制到数组b中,并输出b中各元素的值。
二级C语言-605(总分:60.00,做题时间:90分钟)一、程序填空题(总题数:1,分数:20.00)1.给定程序中已建立一个带有头结点的单向链表,在main函数中将多次调用fun函数,每调用一次fun函数,输出链表尾部结点中的数据,并释放该结点,使链表缩短。
请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。
#include<stdio.h>#include<stdlib.h>#define N 8typedef struct list{int data;struct list*next;}SLIS T;void fun(SLIST*P){SLIST*t,*s;t=P->next;s=p;while(t->next!=NULL){s=t;/**********found**********/t=t-> 1;}/**********found**********/printf("%d", 2);s->next=NULL;/**********found**********/free( 3);}SLIST*creatlist(int*a){SLIST*h*p,*q;int i;h=p=(SLIST*)malloc(sizeof(SLIST));for(i=0;i<N;i++){q=(SLIST*)malloc(sizeof(SLIST));q->data=a[i];p->next=q;p=q;}p->next=0;return h;}void outlist(SLIST*h){SLIST*p;p=h->next;if(p==NULL)printf("/nThe list is NULL!/n");else{printf("/nHead");do {printf("->%df",P->data);p=p->next;)while(p!=NULL);printf("->End/n");}}main(){SLIST*head;int a[y]={11,12,15,18,19,22,25,29};head=creatlist(a);printf("/nOutput from head:/n");outlist(head);printf("/nOutput from tail:/n");while(head->next!=NULL){fun(head);printf("/n/n");printf("/nOutput from head again :/n");outlist(head);}}(分数:20.00)解析:next t->data t [解析] fun函数的功能是输出链表尾部结点中的数据,并释放该结点,使链表缩短。
练习5-1答案一、选择题1.合法的数组说明语句是( B )。
A.int a[]="string"; B.int a[]={0,1,2,3,4,5};C.char a="string"; D.char a[5]={'0', '1', '2', '3', '4', '5'}; 2.以下对一维整型数组a的说明正确的是( D )。
A.int a(10); B.int n=10, a[n]; C.int n; D.#define SIZE 10scanf("%d", &n); int a[SIZE];int a[n];3.已知:int a[10];,则对a数组元素的正确引用是( D )。
A.a[10] B.a[3.5] C.a(5) D.a[10-10]4.以下对一维数组a进行正确初始化的语句是( C )。
A.int a[10]=(0, 0, 0, 0, 0); B.int a[10]={};C.int a[]={0}; D.int a[2]={10, 9, 8};5.对以下说明语句的正确理解是( B )。
int a[10]={6, 7, 8, 9, 10};A.将5个初值依次赋给a[1]至a[5]B.将5个初值依次赋给a[0]至a[4]C.将5个初值依次赋给a[6]至a[10]D.因为数组长度与初值的个数不相同,所以此语句不正确二、填空题6.求所有不超过200的N值,N的平方是具有对称性质的回文数。
所谓回文数就是将一个数从左到右与从右到左读都是一样的,例如:34543和都是回文数。
例如:满足题意要求的数有:N=1,11*11=121;N=111,111*111=12321。
#include <stdio.h>main(){int m[16], n, i, t, count=0;long a, k;printf("Result is:\n");for (n=10; n<200; n++){ k=0;t=1;a=n*n;for (i=1; a!=0; i++){ ①m[i]=a%10;a/=10;}for (; i>1; i--){ k+=m[i-1]*t;②t*=10;}if ( ③k==n*n ) printf("%2d:%10d %10d\n", ++count, n, n*n);}}则①为( m[i]=a%10; )、②为( t*=10; )、③为( k==n*n )。