c语言1.23e 001,C语言复习资料网上编程作业(23题)

本文提供了多个C语言编程题目,涉及数据类型的内存占用、输入输出、条件判断、循环控制、函数应用等基本概念。通过编程实现计算字节数、分段函数、质数判断、数列求和、平均值计算等功能,旨在巩固C语言基础知识和提高编程能力。
摘要由CSDN通过智能技术生成

《C语言复习资料网上编程作业(23题)》由会员分享,可在线阅读,更多相关《C语言复习资料网上编程作业(23题)(33页珍藏版)》请在人人文库网上搜索。

1、2011-2012-1高级程序设计复习习题(初稿)第一部分:网上编程作业(23题)编程题1:sizeof测试字节数要求:编写程序,测试以下类型在内存中所占空间大小: char、int 、short 、long、unsigned int 、float 、double,输出时给出较清晰的提示信息。要求输出格式(GCC):sizeof(char)=1sizeof(int)=4sizeof(short)=2sizeof(long)=4sizeof(unsigned int)=4sizeof(float)=4sizeof(double)=8参考代码:#includeint main()printf(si。

2、zeof(char)=%dn,sizeof(char);printf(sizeof(int)=%dn,sizeof(int);printf(sizeof(short)=%dn,sizeof(short);printf(sizeof(long)=%dn,sizeof(long);printf(sizeof(unsigned int)=%dn,sizeof(unsigned int);printf(sizeof(float)=%dn,sizeof(float);printf(sizeof(double)=%dn,sizeof(double);return 0;编程题2:输入输出练习要求:编写程序,。

3、定义一个 int 型变量 a ,一个 float 型变量 b ,一个 double 型变量 c 。a 变量初始化,值为20; b 变量赋值,值为3.1415f, c 变量的值从键盘输入,调用 printf 函数输出三个变量的值,输出时给出较清晰的提示信息 。如输入:123.4则输出:value of a is: 20value of b is: 3.141500value of c is: 123.400000参考代码:#include int main() int a=20; float b; double c; b=3.1415f; scanf(%lf,&c); printf(value 。

4、of a is: %dn,a); printf(value of b is: %fn,b); printf(value of c is: %fn,c); return 0; 编程题3:用if语句求解分段函数要求:分段函数求解:输入 x ,计算并输出 y 的值: y=x+100 ( 当 x 20) y= x ( 当 2 0 x 100) y=x-100 ( 当 x 100) 测试用例的输入及输出结果如下: 测试用例:输入 测试用例:输出 -9 x=-9.000000, y=91.000000 78.97 x=78.970000, y=78.970000 235.98 x=235.980000, 。

5、y=135.980000 参考代码:#include int main() double x,y; scanf(%lf,&x); if(x 2009-10-31 2009 2 2009-2-28 2008 2 2008-2-29 参考代码:#include void main() int i,j; scanf(%d%d,&i,&j); switch(j) case 1: case 3: case 5: case 7: case 8: case 10: case 12:printf(%d-%d-31n,i,j);break; case 2:if(i%400=0|(i%4=0&i%100!=0) 。

6、printf(%d-%d-29n,i,j); else printf(%d-%d-28n,i,j);break; case 4: case 6: case 9: case 11:printf(%d-%d-30n,i,j);break; default:printf(您的输入有误,请重新1-12之间的月份:n);break; 编程题5:输出所有的3位数字的质数要求:以每行 5 个的形式输出所有的 3 位数字的质数。 说明:( 1 )每个质数按 ”%6d” 格式输出。 ( 2 )最后一个质数后面要输出一个换行符。 参考代码:#include #include int main() int x,y,。

7、n=0; double k; for(x=100; xk) n+; printf(%6d,x); if(n%5=0) printf(n); printf(n); return 0; 编程题6:数列求和要求:输入一个双精度实数x,计算并输出下式的值,直到最后一项的绝对值小于10-5(保留两位小数),s=x-x2/2!+x3/3!-x4/4!+ 说明:(1)scanf之前无需用printf给提示信息。 (2)求和结果用%.2f控制输出格式。 测试用例的输入及输出结果如下: 测试用例:输入 测试用例:输出 1 sum=0.63 3.5 sum=0.97 5.0 sum=0.99 参考代码:#incl。

8、ude #include int main() double x,t=1,i=1,sum=0; int sign=1; scanf(%lf,&x); do t=t*x/i; sum+=sign*t; sign=-sign; i+; while(fabs(t=1e-5); printf(sum=%.2fn,sum); return 0; 编程题7:求平均值要求:编程从键盘上输入 20 个整数,求去掉最大值和最小值以后那些元素的平均值。 说明:( 1 ) scanf 之前无需用 printf 给提示信息。 测试用例的输入及输出结果如下 : 测试用例:输入 测试用例:输出 1 2 3 4 5 6 7。

9、 8 9 10 11 12 13 14 15 16 17 18 19 20 count=18,average=10.500000 90 80 70 100 50 60 70 100 75 85 85 90 80 70 65 50 60 70 80 90 count=16,average=76.250000 参考代码:#include int main() int i,a20,max,min,sum=0,count=0; for(i=0;imax)max=ai; else if(ai 1 1 2 3 5 10 1 1 2 3 5 8 13 21 34 55 参考代码:#include int m。

10、ain() int i,n,f39,count=0; scanf(%d,&n); f0=1; f1=1; for(i=2;i4Before delete,elements are:2 3 4 5 3 2 0After delete,elements are:2 3 5 3 2 02 3 4 2 05Before delete,elements are:2 3 4 2 0does not exist,no operating!2 3 3 4 3 5 3 7 03Before delete,elements are:2 3 3 4 3 5 3 7 0After delete,elements ar。

11、e:2 4 5 7 0参考代码:int main( ) int a20,b20; int i,k=0,x,j,m=0,h=0; for(i=0;i 1,2,3,4,5, 8 1,2,3,4,5,6,7,8, 参考代码:#include #include #include void create (int a,int n); void print (int a,int n); int main() int *array,n; scanf(%d,&n); array=(int*)malloc(n*4); create (array,n); print (array,n); printf(n); r。

12、eturn 0; void create (int a,int n) int i,j=1; for(i=0;i #include #include void create (int a,int n); void print (int a,int n); int main() int *array,n; scanf(%d,&n); array=(int*)malloc(n*4); create (array,n); print (array,n); printf(n); return 0; void create (int a,int n) int i,j=1; for(i=0;i#includ。

13、evoid Find(int *a,int *max,int *maxPos,int *min,int *minPos);main()int array10=23,45,90,-9,43,90,4,2,-9;int *a=array;int max,min,maxPos,minPos;Find(array,&max,&maxPos,&min,&minPos);printf(max=%d, maxPos=%d, min=%d, minPos=%dn,max,maxPos,min,minPos);return 0;void Find(int *a,int *max,int *maxPos,int 。

14、*min,int *minPos) int i;*max=*min=a0;*maxPos=*minPos=0;for(i=1;i*max)*max=ai; *maxPos=i; else if(ai100101;测试用例:输入 测试用例:输出 045 100101 145 110010110171000001111参考代码:#includeint octtobin(int n,int*p)int i,r,k=0;while(n)r=n%8;for(i=1;i=0;i-)printf(%d,arrayi);printf(n);return 0;编程题13:最大公约数。要求:实现求两个正整数的最大。

15、公约数。说明:(1)从键盘输入两个正整数; (2)输出这两个正整数的最大公约数;测试用例:输入 测试用例:输出 345 258 gcd=333 33 gcd=3324 72gcd=24参考代码:#includeint main()int a,b,c;scanf(%d%d,&a,&b);if (a Area=6.000 1 4 5 Error!Please input again. 5.5 6.4 9 Area=17.429 -1 0 2 Error!Please input again. 参考代码:#include#includeint main()double a, b, c, p, s;s。

16、canf(%lf%lf%lf, &a, &b, &c);if(a+bc&a-b 2005 is not a leap year!2008 2008 is a leap year!158Input error!11159Input error!1900 1900 is not a leap year!参考代码:#include#define LEAP_YEAR(y)(y)%400=0|(y)%4=0&(y)%100!=0int main()int year;scanf(%d,&year);if(year=1000&year hangsan 86 2 lisi 84 3 wanger 93The 。

17、average score=87.67The student who has the highest score is:3 wanger 93参考代码:#includetypedef struct studentint num;char name10;int score;st;void input(st*t,int n)int i;for(i=0;ibest-score)best=t+i;*av=sum*1.0/n;return best;int main()st team3,*best=NULL;double av;input(team,3);best=average(team,3,&av)。

18、;printf(The average score=%.2fn,av);printf(The student who has the highest score is:n);printf(%dt%st%dn,best-num,best-name,best-score);return 0;编程题17:根据输入日期输出对应季节要求:定义一个表示一年四季的枚举类型,然后从键盘上输入一年中的日期(包括年、月、日信息),输出对应的季节。 说明:1 季节枚举类型enum Seasons Spring,Summer,Autumn,Winter;2 日期结构体struct Dateint year,month。

19、,day;3 根据月份信息判断相应季节。测试用例:输入 测试用例:输出 2009 10 28Now it is Autumn参考代码:#includeenum season Spring,Summer,Autumn,Winter;struct Dateint year,month,day;int main()struct Date d;enum season s;scanf(%d%d%d,&d.year,&d.month,&d.day);if(d.month=3&d.month=6&d.month=9&d.monthint input(char*s)int i=0;char c;doc=get。

20、char();if(c=0&c=A&c=a&c=0&si=A&si=a&si=q时表明交换结束。测试用例:输入 测试用例:输出 abcdefggfedcba参考代码:#include#includevoid reverse(char*s)char*p=s,*q,t;int len=strlen(s);q=p+len-1;while(pint change(char*source,char*des)int i=0;while(*source)if(*source=0&*sourcevoid main()int n;scanf(%d,&n);int s1010 = 0; /数组定义时最好赋初值in。

21、t i,j;for(i=0;iint sy(int(*p)4)int i,j;for(i=0;i#include #define max 10000void Input(char StudName6max)for (int i=0;i6;i+)gets(StudNamei);void MZ(char ch6max)for (int i=0;i6;i+)for(int j=0;j6;j+)if (strcmp(chi,chj)0)char temp1314;strcpy(temp,chi);strcpy(chi,chj);strcpy(chj,temp);void Output(char ch6max)for (int i=0;i6;i+)printf(%s ,chi);printf(n);void main()char ch6max;Input(ch);MZ(ch);Output(ch。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值