




已阅读5页,还剩17页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1 电大 C+语言程序设计第 1、 2、 3、 4 次作业及答案 第一次作业 一、写出下列每个程序运行后的输出结果 1. #include void main() int x=5; switch(2*x-3) case 4:printf(%d ,x); case 7:printf(%d ,2*x+1); case 10:printf(%d ,3*x-1);break; default:printf(%s ,defaultn); printf(%sn,switch end.); 2. #include void main() int i,s=0; for(i=1;i=6;i+) s+=i*i; printf(s=%dn,s); 3. #include void main() int i,s1=0,s2=0; for(i=0;i10;i+) if(i%2)s1+=i; else s2+=i; printf(%d %dn,s1,s2); 2 4. #include void main() int n=10,y=1; while(n-)y+;y+; printf(y=%dn,y); 5. #include void main() int f,f1,f2,i; f1=f2=1; printf(%d %d ,f1,f2); for(i=3;i=10;i+) f=f1+f2; printf(%d ,f); if(i%5=0)printf(n); f1=f2; f2=f; printf(n); 6. #include #include void main() int i,n; for(n=2;n=20;n+) int temp=(int)sqrt(n);/sqrt(n)求出 n 的平方根并取整 for(i=2;itemp)printf(%d ,n); printf(n); 3 7. #include #include const int M=20; void main() int i,c2,c3,c5; c2=c3=c5=0; for(i=1;i=M;i+) if(i%2=0)c2+; if(i%3=0)c3+; if(i%5=0)c5+; printf(%d %d %dn,c2,c3,c5); 8. #include #include const int M=20; void main() int i,s; for(i=1,s=0;i5 的最小 n 值。 5编写一个主函数,求满足不等式 22+42+ +n21000 的最大 n 值,假定分别用 i 和 s 为取偶数值和累加值的变量,并限定使用 do 循环编程。 6 6编写一个主函数,计算并输出 n!的值,其中 n 值由键盘输入。 参考答案: 1、 答案: #include void fun4(char* a,int b) do if(*a=0 & *a=9)b*a-48+; while(*a+); /*void main() char * a=122333444499888; int b10=0; fun4(a,b); for(int i=0;i10;i+) printf(%d ,bi); */ 2、 答案: #include 7 const int M=2,N=3; double Mean(double aMN, int m,int n ) double v=0; for(int i=0;im;i+) for(int j=0;jn;j+) v+=aij; return v/(m*n); /*void main() double a23=1,2,3,4,5,6; printf(%lfn,Mean(a,2,3); */ 3、 答案: #include int FF(int a , int n) int mul=1; if(n=1)mul*=a0; else mul=an-1*FF(a,n-1); return mul; /*void main() int a6=1,2,3,4,5,6; printf(%dn,FF(a,6); */ 4、 答案: 8 #include void main() double sum=0; int n=1; while(true) if(sum + 1/(double)n 5)break; else sum += 1/(double)n; n+; printf(%d, %lfn,n,sum); 5、 答案: #include void main() int s=0,i=2; do s+=i*i; if(s+(i+2)*(i+2)=1000)break; else i+=2; while(true); printf(i=%d,s=%d,i,s); 6、 答案: #include void main() 9 int s=0,n; printf(请输入 n 的值: ); scanf(%d,&n); for(int i=1;i=n;i+) s=s*i; printf(n=%d,s=%d,n,s); 第三次作业 一、写出下列每个程序运行后的输出结果 1 程序代码: #include void SB(char ch) switch(ch) case A: case a: printf(WW ); break; case B: case b: printf(GG ); break; case C: case c: printf(PP ); break; default: printf(BB ); break; void main() char a1 = b, a2 = C, a3 = f; SB(a1); SB(a2); SB(a3); SB(A); printf(n); 10 2 程序代码: #include #include double SD(int a, int b, char op) double x; switch(op) case +: x = a + b; break; case -: x = a - b; break; case *: x = a * b; break; case /: if(b) x = (double)a/b; else exit(1); break; default: printf(运算符错! n); exit(1); return x; void main() int x = 20, y = 8; printf(%3.2lf , SD(x, y, -); printf(%3.2lf , SD(x, y, *); printf(%3.2lfn, SD(x + y, y, /); 3 程序代码: #include void WF(int x, int y) x = x + y; y = x + y; printf(subs: x, y = %d, %dn, x, y); void main() int x = 18, y = 23; printf(main: x, y = %d, %dn, x, y); 11 WF(x, y); x = 2 * x; printf(main: x, y = %d, %dn, x, y); 4 程序代码: #include #include void fun(char ss); void main() char s15 = 567891234; fun(s); printf(%sn, s); void fun(char ss) int i, n = strlen(ss); for(i = 0; i n / 2; i +) char c = ssi; ssi = ssn - 1 - i; ssn - 1 - i = c; 5 程序代码: #include void InsertSort(int a, int n) int i, j, x; for(i = 1; i = 0; j -) / 为 x 顺序向前寻找合适的插入位置 12 if(x aj) aj + 1 = aj; else break; aj + 1 = x; void main() int i; int a6 = 20, 15, 32, 47, 36, 28 ; InsertSort(a, 6); for(i = 0; i 6; i +) printf(%d , ai); printf(n); 6 程序代码: #include void main() int a8 = 3, 5, 7, 9, 11, 13, 15, 17 ; int i, * p = a; for(i = 0; i 8; i +) printf(%5d, * p +); if(i + 1) % 4 = 0) printf(n); 7 程序代码: #include int LA(int * a, int n) int i, s = 0; for(i = 0; i n; i +) s += ai; return s; 13 void main() int a = 5, 10, 15, 20, 25, 30 ; int b = LA(a, 4); int c = LA(a + 2, 3); printf(%d %dn, b, c); 8 程序代码: #include int LB(int * a, int n) int i, s = 1; for(i = 0; i n; i +) s *= * a +; return s; void main() int a = 1, 2, 3, 4, 2, 4, 5, 2 ; int b = LB(a, 4) + LB(&a3, 4); printf(b=%dn, b); 二、写出下列每个函数的功能 1 程序代码: int WB(int a, int n, int x) int i; for(i = 0; i n; i +) if(ai = x) return 1; return 0; 2 程序代码: 14 int WC(int a, int n, int k) int c = 0, i; for(i = 0; i = k) c +; return c; 3 程序代码: #include #include #include const int N = 10; int ff(int x, int y) int z; printf(%d + %d = , x, y); scanf(%d, &z); if(x + y = z) return 1; else return 0; void main() int i, a, b, c = 0; srand(time(0); / 初始化随机数序列 for(i = 0; i N; i +) a = rand() % 20 + 1; / rand()函数产生 032767 之间的一个随机数 b = rand() % 20 + 1; c += ff(a, b); printf(得分: %dn, c * 10); 4 * 程序代码: int fun6(int m, int n, int b) 15 if(m b & n b) return m * n; else if(m % b = 0 & n % b = 0) return b * fun6(m / b, n / b, b); else return fun6(m, n, + b); 5 程序代码: #include #include void LI(int n) int * a = malloc(n * sizeof(int); int i; for(i = 0; i = 0; i -) printf(%d , * (a + i); printf(n); free(a); 6 程序代码: int LK(double a, int n) double s = 0; int i, m = 0; for(i = 0; i n; i +) s += ai; s /= n; for(i = 0; i = s) m +; return m; 16 参考答案: 一、 1、答案:运行结果: GG PP BB WW 2、答案:运行结果: 12.00 160.00 3.50 3、答案:运行结果: main: x, y = 18, 23 subs: x, y = 41, 64 main: x, y = 36, 23 4、答案:运行结果: 432198765 5、运行结果: 47 36 32 28 20 15 6、答案:运行结果: 3 5 7 9 11 13 15 17 7、答案:运行结果: 50 60 8、答案:运行结果: b=184 二、 1、答案:在整型数组 a 的前 n 个元素中查找值为 x 的元素,找到返回 1,找不到返回 0。 2、答案:统计整型数组 a 的前 n 个元素中不小于 k 的元素个数并返回 3、答案:程序随机产生 10 道 20 以内整数加法题,请用户回答。并统计得分, 4、答案:调用 fun6(m, n, 2)求 m 和 n 的最小公倍数 5、答案:读入 n 个整数,然后逆序输出 17 6、答案:返回双精度数数组 a 的前 n 个元素中不小于平均值的元素个数。 第四次作业 一、写出下列每个程序运行 后的输出结果 1 程序代码: #include struct Worker char name15; / 姓名 int age; / 年龄 float pay; / 工资 ; void main() struct Worker x = wanghua, 52, 2350 ; struct Worker y, * p; y = x; p = &x; printf(%s %d %6.2fn, , y.age, y.pay); printf(%s %d %6.2fn, p-name, p-age + 1, p-pay + 20); 2 程序代码: #include #include struct Worker char name15; / 姓名 int age; / 年龄 float pay; / 工资 ; void main() struct Worker x; char * t = liouting; 18 int d = 38; float f = 493; strcpy(, t); x.age = d; x.pay = f; x.age +; x.pay *= 2; printf(%s %d %6.2fn, , x.age, x.pay); 3 程序代码: #include struct Worker char name15; / 姓名 int age; / 年龄 float pay; / 工资 ; int Less(struct Worker r1, struct Worker r2) if(r1.age r2.age) return 1; else return 0; void main() struct Worker a4 = abc, 25, 420 , def, 58, 638 , ghi, 49, 560 , jkl, 36, 375 ; struct Worker x = a0; int i; for(i = 1; i 4; i +) if(Less(x, ai) x = ai; printf(%s %d %6.2fn, , x.age, x.pay); 二、写出下列每个函数的功能 1 程序代码: struct Worker 19 char name15; / 姓名 int age; / 年龄 float pay; / 工资 ; void QA(struct Worker a, int n) int i; for(i = 1; i name); p = f; while(- n) p = p-next = malloc(sizeof(struct StrNode); scanf(%s, p-name); p-next = NULL; return f; 3 程序代码: struct IntNode 20 int data; / 结点值域 struct IntNode * next; / 结点指针域 ; struct IntNode * FindMax(struct IntNode * f) struct IntNode * p = f; if(! f) return NULL; f = f-next; while(f) if(f-data data) p = f; f = f-next; return p; 4 * 程序代码: struct IntNode int data; / 结点值域 struct IntNode * next; / 结点指针域 ; int Count(struct IntNode * f) int c = 0; while(f) c +; f = f-next; return c; 21 5 程序代码: struct IntNode int data; / 结点值域 struct IntNode * next; / 结点指针域 ; struct IntNode * Input(int n) struct IntNode * f, * p; f = malloc(sizeof(struct IntNode); if(n = 0) return NULL; f-next = NULL; printf(从键盘输入 %d 个整数: , n); while(n -) scanf(%d, &(f-data); p = f;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年VB考试大赛练习试题及答案
- 重要知识点2025年软考试题及答案
- 资源配置效率与市场竞争研究试题及答案
- 交通事故救治的经验总结与反思计划
- 软考网络管理员技能提升策略试题及答案
- 2025项目管理聘用合同范本
- 2025上海城市租房合同范本
- 2025年VB考试逻辑与试题及答案
- 细化临床实践指南的工作方案计划
- 2025年公司战略的执行力评估试题及答案
- 铁路防胀知识培训
- 2025年浙江湖州市城市投资发展集团有限公司招聘笔试参考题库附带答案详解
- 2025年高空车作业考试题及答案
- 非遗文化产业发展-深度研究
- (一模)2025年深圳市高三年级第一次调研考试 英语试卷(含标准答案)
- 丙酸铬、淀粉酶对黄羽肉鸡生长性能、抗氧化和肠道健康的影响
- 光伏发电新能源课件
- 2025年贵州遵义路桥工程限公司招聘10人高频重点提升(共500题)附带答案详解
- 上海市居住房屋租赁合同范本
- 老旧小区改造给排水施工方案
- 2025届江苏省南京市南京师大附中高考数学一模试卷含解析
评论
0/150
提交评论