中国大学C语言程序题_第1页
中国大学C语言程序题_第2页
中国大学C语言程序题_第3页
中国大学C语言程序题_第4页
中国大学C语言程序题_第5页
已阅读5页,还剩57页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、.第3周编程题在线测试1 计算两个数的平方和(3分)题目内容:从键盘读入两个实数,编程计算并输出它们的平方和,要求使用数学函数pow(x,y)计算平方值,输出结果保留2位小数。 程序中所有浮点数的数据类型均为float。提示:使用数学函数需要在程序中加入编译预处理命令 #include 以下为程序的运行结果示例:please input x and y:1.2,3.4Result=13.00输入格式: %f,%f输出格式:输入提示信息:Please input x and y:n输出格式:Result=%.2fn为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。#i

2、nclude #include int main()float x,y;printf(Please input x and y:n);scanf(%f,%f,&x,&y);printf(Result=%.2fn,pow(x, 2)+pow(y,2);return 0;2逆序数的拆分计算(3分)题目内容:从键盘输入一个4位数的整数,编程计算并输出它的逆序数(忽略整数前的正负号)。例如,输入-1234,忽略负号,由1234分离出其千位1、百位2、十位3、个位4,然后计算4*1000+3*100+2*10+1 = 4321,并输出4321。再将得到的逆序数4321拆分为两个2位数的正整数43和21,

3、计算并输出拆分后的两个数的平方和的结果。以下是程序的运行结果示例:Input x:-1234y=4321a=43,b=21result=2290输入格式: %d输出格式:输入提示信息:Input x:n逆序数输出格式:y=%dn逆序数拆分后的输出格式:a=%d,b=%dn平方和的输出格式:result=%dn为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。#include?int main()printf(Input x:n);int x;scanf(%d,&x);if (x = 0)x = -x;int a, b, c, d;a = x / 1000;b = x

4、 / 100 % 10;c = x / 10 % 10;d = x % 10;printf(y=%dn, d * 1000 + c * 100 + b * 10 + a);printf(a=%d,b=%dn, d * 10 + c, b * 10 + a);printf(result=%dn, (b * 10 + a)*(b * 10 + a) + (d * 10 + c)*(d * 10 + c);return 0;3拆分英文名(3分)题目内容:从键盘输入某同学的英文名(小写输入,假设学生的英文名只包含3个字母。如: tom),编写程序在屏幕上输出该同学的英文名,且首字母大写(如: Tom)

5、。同时输出组成该英文名的所有英文字符在26个英文字母中的序号。以下为程序的运行结果示例:Input your English name:tomTomt:20o:15m:13输入格式: %c%c%c输出格式:输入提示信息:Input your English name:n首字母大写的英文姓名的输出格式:%c%c%cn姓名中每个字母在26个英文字母中的序号的输出格式:%c:%dn为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。#include int main()char a, b, c;printf(Input your English name:n);scanf(

6、%c%c%c,&a, &b, &c);printf(%c%c%cn,a-32,b,c);printf(%c:%dn,a,a-96);printf(%c:%dn,b,b-96);printf(%c:%dn,c,c-96);return 0;4计算体指数(3分)题目内容:从键盘输入某人的身高(以厘米为单位,如174cm)和体重(以公斤为单位,如70公斤),将身高(以米为单位,如1.74m)和体重(以斤为单位,如140斤)输出在屏幕上,并按照以下公式计算并输出体指数,要求结果保留到小数点后2位。程序中所有浮点数的数据类型均为float。假设体重为w公斤,身高为h米,则体指数的计算公式为: 以下是程序

7、的运行结果示例:Input weight, height:70,174weight=140height=1.74t=23.12输入格式: %d,%d输出格式:输入提示信息:Input weight, height:n (注意:在height和逗号之间有一个空格)体重输出格式:weight=%dn身高输出格式:height=%.2fn体指数输出格式:t=%.2fn为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。#include #include int main()float weight, height,t;printf(Input weight, height:

8、n);scanf(%f,%f, &weight,&height);t = weight / pow(height / 100), 2);printf(weight=%dn, (int)weight*2);printf(height=%.2fn, height/100);printf(t=%.2fn, t);return 0;第4周无处不在的抉择1数位拆分v2.0(4分)题目内容:从键盘上输入一个4位数的整数n,编写程序将其拆分为两个2位数的整数a和b,计算并输出拆分后的两个数的加、减、乘、除和求余运算的结果。例如n=-4321,设拆分后的两个整数为a,b,则a=-43,b=-21。除法运算结果

9、要求精确到小数点后2位,数据类型为float。求余和除法运算需要考虑除数为0的情况,即如果拆分后b=0,则输出提示信息The second operater is zero!程序的运行结果示例1:Please input n:120012,0sum=12,sub=12,multi=0The second operater is zero!程序的运行结果示例2:Please input n:-2304-23,-4sum=-27,sub=-19,multi=92dev=5.75,mod=-3输入提示信息:Please input n:n输入格式: %d输出格式:拆分后的两个整数的输出格式:%d,%

10、dn加法、减法、乘法的输出格式:sum=%d,sub=%d,multi=%dn除法和求余的输出格式:dev=%.2f,mod=%dn除数为0的提示信息:The second operater is zero!n为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。时间限制:500ms内存限制:32000kb#include int main() int m, x, y;printf(Please input n:n);scanf(%d, &m);x = m / 100;y = m % 100;printf(%d,%dn, x, y);printf(sum=%d,sub=

11、%d,multi=%dn, x + y, x - y, x*y);if (y != 0)printf(dev=%.2f,mod=%dn, (float)x / y, x%y); else printf(The second operater is zero!n); return 0;2出租车计价(4分)题目内容:已知某城市普通出租车收费标准为:起步里程为3公里,起步费为8元,10公里以内超过起步里程的部分,每公里加收2元,超过10公里以上的部分加收50%的回空补贴费,即每公里3元。出租车营运过程中,因堵车和乘客要求临时停车等客的,按每5分钟加收2元计算,不足5分钟的不计费。从键盘任意输入行驶里

12、程(精确到0.1公里)和等待时间(精确到分钟),请编程计算并输出乘客应支付的车费,对结果进行四舍五入,精确到元。程序运行结果示例1:Input distance and time:2,2fee = 8程序运行结果示例2:Input distance and time:5,5fee = 14程序运行结果示例3:Input distance and time:12,15fee = 34程序运行结果示例4:Input distance and time:20,0fee = 52输入提示信息:Input distance and time:输入格式:用逗号分隔的两个数字,第一个表示距离、第二个表示时间

13、:%f,%d输出格式:价格的输出格式:fee = %.0fn (注意:等号的两边各有一个空格)为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。#include #include ?int main()float f=0, fee=0;int d=0,dp=0;printf(Input distance and time:);scanf(%f,%d, &f,&d);if (f0)&(f10) if (f = 3 & d 5) fee = 8; printf(fee = %.0fn, fee); if (f=5) dp=d/5; fee=(f-3)*2+8+2 * d

14、p; printf(fee = %.0fn, fee); if (f = 10 & d = 5)dp = d / 5;fee = (f - 10) * 3 + 22 + 2 * dp;printf(fee = %.0fn, fee);if (f = 10 & d 5)fee = (f - 10) * 3 + 22;printf(fee = %.0fn, fee);return 0;3数据区间判断(6分)题目内容:从键盘输入一个int型的正整数n(已知:0n10000),编写程序判断n落在哪个区间。如果用户输入的数据不在指定的范围里,程序输出 error!。例如,输入265,则该数属于区间 10

15、0-999。程序运行结果示例1:Please enter the number:25632563: 1000-9999程序运行结果示例2:Please enter the number:156156: 100-999程序运行结果示例3:Please enter the number:3636: 10-99程序运行结果示例4:Please enter the number:33: 0-9程序运行结果示例5:Please enter the number:10923error!输入提示信息:Please enter the number:n输入格式: %d输出格式:输出的区间判断:%d: 1000

16、-9999n%d: 100-999n%d: 10-99n%d: 0-9n输入错误提示信息:error!n为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。#include #include ?int main()int x;printf(Please enter the number:n);scanf(%d,&x); if (x=0&x=1000&x=10&x=100&x=999)printf(%d: 100-999n,x);else printf(error!n);return 0;4计算一元二次方程的根v2.0(4分)题目内容:根据下面给出的求根公式,计算并输出

17、一元二次方程的两个实根,要求精确到小数点后4位。其中a,b,c的值由用户从键盘输入。如果用户输入的系数不满足求实根的要求,输出错误提示error!。程序中所有的数据类型均为float。程序运行结果示例1:Please enter the coefficients a,b,c:1,2,1x1=-1.0000, x2=-1.0000程序运行结果示例2:Please enter the coefficients a,b,c:2,6,1x1=-0.1771, x2=-2.8229程序运行结果示例3:Please enter the coefficients a,b,c:2,1,6error!输入提示信

18、息:Please enter the coefficients a,b,c:n输入格式:%f,%f,%f输出格式:输出格式:x1=%7.4f, x2=%7.4fn如果输入的系数不满足求实根的要求,输出错误提示信息:error!n为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。#include #includeint main()float a, b, c, x1, x2, m;printf(Please enter the coefficients a,b,c:n); scanf(%f,%f,%f, &a, &b, &c);m=b*b-4*a*c;if (m0)p

19、rintf(error!n); else x1 = (-b + sqrt(m)/(2 * a); x2 = (-b - sqrt(m)/(2 * a); printf(x1=%7.4f, x2=%7.4fn,x1,x2); return 0;第5周周而复始的循环之道1 6位密码输入检测(4分)题目内容:从键盘输入6位仅由数字09组成的密码。用户每输入一个密码并按回车键后,程序给出判断:如果是数字,则原样输出该数字,并提示用户目前已经输入了几位密码,同时继续输入下一位密码;否则,程序提示error,并让用户继续输入下一位密码。直到用户输入的密码全部是数字为止。以下为程序的运行结果示例:Input

20、 your password:11, you have enter 1-bits number66, you have enter 2-bits numberaerrorderror44, you have enter 3-bits number66, you have enter 4-bits number88, you have enter 5-bits number22, you have enter 6-bits number输入提示信息:Input your password:n输入格式:%c输出格式:如果输入的是数字,输出格式为:%c, you have enter %d-bits

21、 numbern如果输入的不是数字,输出提示信息:errorn为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。#include int main() char a; int i=0; printf(Input your password:n); while(i=48&a=57) printf(%c, you have enter %d-bits numbern,a,+i); else printf(errorn); getchar(); return 0;2判断一个整型数据有几位v1.0(4分)题目内容:从键盘输入一个整型数据(int型),编写程序判断该整数共有几

22、位。例如,从键盘输入整数16644,该整数共有5位。程序运行结果示例1:Please enter the number:2112521125: 5 bits程序运行结果示例2:Please enter the number:-12234-12234: 5 bits输入提示信息:Please enter the number:n输入格式:%d输出格式:%d: %d bitsn为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。#include int main() int x,y,n; printf(Please enter the number:n); scanf(%

23、d,&x); n=x; for (y=1;x/=10;y+); printf(%d: %d bitsn,n,y); return 0;3检测输入数据中奇数和偶数的个数(4分)题目内容:从键盘输入一系列正整数,输入-1表示输入结束(-1本身不是输入的数据)。编写程序判断输入数据中奇数和偶数的个数。如果用户输入的第一个数据就是-1,则程序输出over!。否则。用户每输入一个数据,输出该数据是奇数还是偶数,直到用户输入-1为止,分别统计用户输入数据中奇数和偶数的个数。程序运行结果示例1:Please enter the number:11:odd55:odd88:even99:odd1212:eve

24、n1717:odd-1The total number of odd is 4The total number of even is 2程序运行结果示例2:Please enter the number:-1over!The total number of odd is 0The total number of even is 0输入提示信息:Please enter the number:n输入格式:%d输出格式:用户输入的第一个数据就是-1,输出格式:over!n奇数的输出格式:%d:oddn偶数的输出格式:%d:evenn输入数据中奇数的个数统计:The total number of

25、odd is %dn输入数据中偶数的个数统计:The total number of even is %dn为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。#include int main() int s,odd=0,even=0; printf(Please enter the number:n); do scanf(%d,&s); if (s=-1&odd=0&even=0) printf(over!n); else if( s%2!=0 &s!=-1) printf(%d:oddn,s); odd+; else if (s%2=0) printf(%d:e

26、venn,s); even+; else even+=0; while (s!=-1); printf(The total number of odd is %dn,odd); printf(The total number of even is %dn,even); return 0;4计算球的反弹高度(4分)题目内容:一个球从100米高度自由落下,每次落地后反跳回原高度的一半,再落下并反弹.,求它在第5次和第10次落地时,分别共经过了多少米?第5次和第10次反弹分别是多高?要求计算结果保留到小数点后3位。用户从键盘输入想要计算的第n次(n=15)。程序中所有浮点数的数据类型均为float。

27、程序运行结果示例1:Input:55 times:287.5003.125程序运行结果示例2:Input:1010 times:299.6090.098输入提示信息:Input:n输入格式:%d输出格式:反弹次数:%d times:n第n次反弹共经过多少米:%.3fn第n次的反弹高度:%.3fn为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。#include int main() int time,i; float each=0,sum=0,h=100; printf(Input:n); scanf(%d,&time); for (i=0;itime;i+) su

28、m+=h; h/=2; each=h; sum+=each; printf(%d times:n,time); printf(%.3fn,sum-each); printf(%.3fn,each); return 0;第6周分工与合作:领导的艺术1程序改错v2.0(5分)下面代码的功能是将百分制成绩转换为5分制成绩,具体功能是:如果用户输入的是非法字符或者不在合理区间内的数据(例如输入的是a,或者102,或-45等),则程序输出Input error!,并允许用户重新输入,直到输入合法数据为止,并将其转换为5分制输出。目前程序存在错误,请将其修改正确。并按照下面给出的运行示例检查程序。1. #

29、include2. intmain()3. 4. intscore;5. chargrade;6. printf(Pleaseinputscore:);7. scanf(%d,&score);8. if(score100)9. printf(Inputerror!n);10. elseif(score=90)11. grade=A;12. elseif(score=80)13. grade=B;14. elseif(score=70)15. grade=C;16. elseif(score=60)17. grade=D;18. else19. grade=E;20. printf(grade:

30、%cn,grade);21. return0;22. 程序运行结果示例1:Please input score:aInput error!Please input score:-12Input error!Please input score:230Input error!Please input score:92grade: A程序运行结果示例2:Please input score:88grade: B程序运行结果示例3:Please input score:73grade: C程序运行结果示例4:Please input score:65grade: D程序运行结果示例5:Please

31、input score:27grade: E输入提示信息:Please input score:n输入格式:%d输出格式:输入错误时的提示信息:Input error!n输出格式:grade: %cn (注意:%c前面有一个空格)为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。#include #include int main() int score,m=0; char grade; printf(Please input score:n); do m=scanf(%d,&score); getchar(); if (m!=1|score100) printf(

32、Input error!nPlease input score:n); m=0; while (m=0); if (score=90) grade = A; else if (score=80) grade = B; else if (score=70) grade = C; else if (score=60) grade = D; else grade = E; printf(grade: %cn , grade); return 0;2编程计算a+aa+aaa+aaa(n个a)的值(4分)题目内容:编程计算 a+aa+aaa+aaa(n个a)的值,n和a的值由键盘输入。例如,当n=4,a

33、=2,表示计算2+22+222+2222的值。程序运行结果示例:Input a,n:2,4sum=2468输入提示信息:Input a,n:n输入格式: %d,%d(先输入a,后输入n)输出格式: sum=%ldn为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。#include int main() int a,n,sum=0,tem=1; printf(Input a,n:n); scanf(%d,%d,&a,&n); int i; for (i=1;i=n;i+) sum += (a*tem); tem=tem*10+1; printf(sum=%ldn,su

34、m); return 0;3搬砖问题(4分)题目内容:n块砖(27n=77),36人搬,男搬4,女搬3,两个小孩抬一块砖,要求一次搬完,问男人、女人和小孩各需多少人?请用穷举法编程求解,n的值要求从键盘输入。输出结果按照男人数量升序给出(见下面示例3)。程序的运行结果示例1:Input n(27n=77):28men=0,women=4,children=32程序的运行结果示例2:Input n(27n=77):36men=3,women=3,children=30程序的运行结果示例3:Inputn(27n=77):60men=2,women=14,children=20men=7,women

35、=7,children=22men=12,women=0,children=24输入提示:Input n(27n=77):n输入格式: %d 输出格式:men=%d,women=%d,children=%dn为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。#include int main() int n,woman,man,kid; printf(Input n(27n=77):n); scanf(%d,&n); for(man=0;man=19;man+) for (woman=0;woman=22;woman+) kid=36-man-woman; if(4

36、*man+3*woman+0.5*kid=n) printf(men=%d,women=%d,children=%dn,man,woman,kid); return 0;4编程输出某年某月有多少天(考虑到闰年)。(5分)题目内容:从键盘输入一个年份和月份,输出该月有多少天(考虑闰年),用switch语句编程。程序运行结果示例1:Input year,month:2015,331 days程序运行结果示例2:Input year,month:2015,430 days程序运行结果示例3:Input year,month:2016,229 days程序运行结果示例4:Input year,mont

37、h:2014,228 days程序运行结果示例5:Input year,month:2015,13Input error!输入提示信息:Input year,month:n输入格式: %d,%d输出格式:输入错误时的提示信息:Input error!n输出格式: 31 daysn 30 daysn 29 daysn 28 daysn为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。 #include int isleap(int n);int main() int year,month; printf(Input year,month:n); scanf(%d,%d

38、,&year,&month); switch(month) case 1:case 3:case 5:case 7:case 8:case 10:case 12: printf(31 daysn); break; case 4:case 6:case 9:case 11: printf(30 daysn); break; case 2: if (isleap(year) printf(29 daysn); break; else printf(28 daysn); break; default:printf(Input error!n); return 0;int isleap(int n)

39、if(n%100!=0&n%4=0|n%400=0) return 1; else return 0;第7周盗梦空间的逻辑:探寻递归的奥秘1递归法计算游戏人员的年龄(4分)题目内容:有n个人围坐在一起,问第n个人多大年纪,他说比第n-1个人大2岁;问第n-1个人,他说比第n-2个人大2岁,.,问第3个人,他说比第2个人大2岁;问第2个人,他说比第1个人大2岁。第1个人说自己10岁,问第n个人多大年纪。递归函数原型:unsigned int ComputeAge(unsigned int n);提示:计算年龄的递归公式为:输入格式:%u输出格式:The persons age is %un输入样

40、例1:5输出样例1:The_persons_age_is_18输入样例2:10输出样例2:The_persons_age_is_28注意:为避免出现格式错误,请直接拷贝粘贴上面给出的输入、输出提示信息和格式控制字符串!(注意:在输出中,“_”代表空格,如果直接将上段示例粘贴到代码中,应将其替换为空格。)#include unsigned int Age(unsigned int age);int main() unsigned int n=0; scanf(%ud,&n); printf(The persons age is %un,Age(n); return 0;unsigned int

41、Age(unsigned int n) if (n=1) return 10; else return Age(n-1)+2;2递归法计算两个数的最大公约数(4分)题目内容:利用最大公约数的性质计算。对正整数a和b,当ab时,若a中含有与b相同的公约数,则a中去掉b后剩余的部分a-b中也应含有与b相同的公约数,对a-b和b计算公约数就相当于对a和b计算公约数。反复使用最大公约数的上述性质,直到a和b相等为止,这时,a或b就是它们的最大公约数。这三条性质,也可以表示为:性质1 如果ab,则a和b与a-b和b的最大公约数相同,即Gcd(a, b) = Gcd(a-b, b)性质2 如果ba,则a和

42、b与a和b-a的最大公约数相同,即Gcd(a, b) = Gcd(a, b-a)性质3 如果a=b,则a和b的最大公约数与a值和b值相同,即Gcd(a, b) = a = b程序运行结果示例1:Input a,b:16,248程序运行结果示例2:Input a,b:-2,-8Input error!输入提示信息:Input a,b:输入格式:%d,%d输出格式:输出最大公约数:%dn输入错误提示信息:Input error!n注意:为避免出现格式错误,请直接拷贝粘贴上面给出的输入、输出提示信息和格式控制字符串!#include int Gcd(int a, int b);int main()

43、int a, b, c; printf(Input a,b:); scanf(%d,%d, &a, &b); c = Gcd(a, b); if ( c!= -1) printf(%dn, c); else printf(Input error!n); return 0;int Gcd(int a, int b) if (a = 0 | b b) return Gcd(a - b, b); else return Gcd(a, b - a); 3寻找中位数v1.0(4分)题目内容:编写一个函数返回三个整数中的中间数。函数原型为:int mid(int a, int b, int c);函数功能

44、是返回a,b,c三数中大小位于中间的那个数。输入格式:%d%d%d输出格式:The result is %dn输入样例1:12 6 18输出样例1:The_result_is_12输入样例2:-9 7 -2输出样例2:The_result_is_-2注意:为避免出现格式错误,请直接拷贝粘贴上面给出的输入、输出提示信息和格式控制字符串!(注意:在输出中,“_”代表空格,如果直接将上段示例粘贴到代码中,应将其替换为空格。)#include int mid(int a, int b, int c);int main() int a,b,c; scanf(%d %d %d,&a,&b,&c); printf(The

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论