第5章数组-习题答案.doc
精品文档,仅供学习与交流,如有侵权请联系网站删除1: /*编写程序,用筛选法求 100 之内的素数。*/#include <stdio.h>void main ()int i,j;for (i=2; i<100; i+) for (j=2; j<i; j+) if (i%j= 0)break;if (j = i)printf("%dt", i);2/*输入一整数,并能逐位正序或反序输出。*/#include <stdio.h>void main()int num,a,i=0;printf("输入一个数字n");scanf("%d",&num);printf("正序输出则输入 1 反序输出输入 0n");scanf("%d",&a);switch(a)case 1:printf("%d",num); break;case 0:while(0!=num)i=i*10+num%10;num=num/10;printf("%d",i);break;default:printf("请输入 0 和 1!n");break;3/*对于一个 4X4 矩阵逆时针旋转 90 度*/#include <stdio.h>void main()int a44,i,j,b44;for(i=0;i<4;i+)for(j=0;j<4;j+)scanf("%d",&aij);for(i=0;i<4;i+)for(j=0;j<4;j+)b3-ji=aij;printf("逆时针旋转 90 度后:n");for(i=0;i<4;i+)for(j=0;j<4;j+)printf("%d ",bij);printf("n");4: /*将一个数组中的数据奇数放到前面,偶数放到后面。*/#include <stdio.h>void main ()int a9,b9,i,j=0,m=0;for(i=0;i<9;i+)scanf("%d",&ai);for(i=0;i<9;i+)if(ai%2!=0)bj=ai;j+;elseb8-m=ai;m+;for(i=0;i<9;i+)printf("%d",bi); 5: /*输入一个 4X4 的矩阵,求四个边上元素的和。*/#include <stdio.h>void main ()int i,j,a44,sum;printf("输入一个 4 阶方阵:n");for (i=0;i<4;i+)for (j=0;j<4;j+)scanf("%d",&aij);sum=a00+a03+a30+a33;printf("四个边上元素的和为%dn",sum);6:/*输入 9 个10 以内的自然数,将它们组成两个整数,求如何组合得到两个数的成绩最大或最小。*/#include <stdio.h>#include <math.h>void main ()int j,k;int i;int t;int a=0,b=0;int flag=1;int num9;int max,max_a,max_b;int min,min_a,min_b;printf("输入 9 个 10 以内的自然数:");for(j=0;j<9;j+)scanf("%d",&numj);for(j=0;j<8;j+)for(k=0;k<8-j;k+)if(numk>numk+1)t=numk+1;numk+1=numk;numk=t;for(i=1;i<511;i+)for(j=8;j>=0;j-)if(i&(int)pow(2.0,j)=0)a=a*10+numj;elseb=b*10+numj;if(flag) max=a*b;max_a=a;max_b=b;flag=0;if(max<a*b)max=a*b;max_a=a;max_b=b;a=0;b=0;flag=1;for(j=0;j<8;j+)for(k=0;k<8-j;k+)if(numk<numk+1)t=numk+1;numk+1=numk;numk=t;for(i=1;i<511;i+)for(j=8;j>=0;j-)if(i&(int)pow(2.0,j)=0)a=a*10+numj;elseb=b*10+numj;if(flag)min=a*b;min_a=a;min_b=b;flag=0;if(min>a*b)min=a*b;min_a=a;min_b=b;a=0;b=0; printf("最小的数为%d x %d = %dn",min_a,min_b,min);printf("最大的数为%d x %d = %dn",max_a,max_b,max);7: /*输入一个数组,输出它的区间范围,例如输入 1,2,3,4,5,6,7,8,20,21,22,23,输出1-8,20-23。*/#include <stdio.h>void main ()#define M 5int aM,i,j,t,m,k;for(i=0;i<M;i+)scanf("%d",&ai);/* 把数组从小到大排列 */for(i=0;i<M-1;i+)for(j=0;j<M-1-i;j+)if(aj>aj+1)t=aj+1;aj+1=aj;aj=t;j=0;for(i=0;i<M;i+)while(aj+1=aj+1)j+;if(j-i!=0)printf("%d-%dn",ai,aj);i=j;j+;if(j-i=0)printf("%dn",ai);i=j;j+;8:/*输入一个区间范围,输出数组,例如输入 1-8, 20-23,输出 1, 2,3,4,5,6,7, 8, 20, 21,22,23。*/#include<stdio.h>void main()int a, c; char b, d;int i;printf("输入区间以'.'结束以','分隔,例如 1-8,22-30:");doscanf("%d%c%d%c", &a, &b, &c, &d);for (i = a; i <= c; i+)printf("%d ", i);while (d != '.');10: /*输入一篇文章,该文章小于 1000 字符,统计单词的个数,单词是连续的大小写字母组成*/#include <stdio.h>void main()char text1000;int count=0,i=0;printf("输入一段文章:");gets(text);while(texti!=0)if( (texti>='a'&&texti<='z') | (texti>='A'&&texti<='Z') )i+;continue;elsei+;count+;printf ("有%d 个单词",count+1)11:/*输入一片文章,该文章小于 1000 字符,对文中内容进行加密,加密方法是 A->Z,B->ZZ->A,a->zz->a。*/#include <stdio.h>void main ()char i,sum=0,text1000;printf("请输入一段文字:n");gets(text);for(i=0;texti!=0;i+)if(texti>=65&&texti<=90)texti=155-texti; if(texti>=97&&texti<=122)texti=219-texti;puts(text);12: /* 编写一个 程序,判断输入的数字是 否在指定范围内,范围 也 由用户输入,例 如“100-100000,220000,250000,300000-600000”。*/#include <stdio.h>void main ()int a,b,c;printf("请输入:下限-上限,需要判断的数n");scanf("%d-%d,%d",&a,&b,&c);if (c<=b&&c>=a)printf("%d 在范围内",c);elseprintf("%d 不在范围内",c);13:/*编写一个程序,输入一篇文章,该文章小于 1000 字符,从中将指定字符串换成另外一个。*/#include <stdio.h>void main ()char i,a,b,text1000;printf("请输入需要被替换的字符-替换后的字符n");scanf("%c-%c",&a,&b);printf("请输入一段文字n");getchar();gets(text);for(i=0;texti!=0;i+)if(texti='a')texti='b'puts(text);14:/*读入一个字符串,判断它是否是回文,所谓回文即正序和逆序内容相同的字符串。*/#include <stdio.h>#include <string.h>void main ()char i,b,a1000;int sum=0;printf("输入一个字符串n");gets(a);b=strlen(a); if(b%2=0)for(i=0;i<(b/2);i+)if(ai=ab-1-i)sum+;elsebreak;if(sum=(b/2)printf("是回文");elseprintf("不是回文");elsefor(i=0;i<(b-1)/2);i+)if(ai=ab-1-i)sum+;elsebreak;if(sum=(b-1)/2)printf("是回文");elseprintf("不是回文");15:/*编写一个程序,输入一篇文章,该文章小于 1000 字符,从中找出出现频率最高的单词。*/#include "stdafx.h"int check(char *p)if (*p >= 'a'&&*p <= 'z') | (*p >= 'A'&&*p <= 'Z')return 1;elsereturn 0;int wordcount(char *text)char *p, *q;int count = 0;p = text;q = p + 1;while (*q)if (check(p) && !(check(q)p+;q+;count+;elsep+;q+;return count;void main()char text1000, *word1000, *p, *q, *first;p = text;q = p + 1;int i=0, j = 1, t = 0, sum = 0, a1000, max = 0, wsum;printf("请输入一段文字:n");gets_s(text);/ 提醒正确输入格式while (texti+1)i+;if (check(&texti)printf("句子结尾漏了标点符号n");exit(1);i = 0;/ 将大写字母转换为小写while (texti)if (texti >= 'A'&&texti <= 'Z')texti += 32;i+;i = 0;/* sum 来保存单词数 */sum = wordcount(text);/* 把每个单词以字符串形式保存到*word 中 */first = text;while (*q)if (check(p) && !(check(q)*q = 0; wordi = first;p+;q+;i+;elsep+;q+;if (!(check(p) && check(q)first = q;/* 记各个单词出现次数存在 a 中 */for (i = 0; i<sum; i+)ai = 0;for (j = 0; j<sum; j+)if (strcmp(wordi, wordj) = 0)ai = ai + 1;for (i = 0; i<sum; i+)for (j = i; j<sum; j+)if (i != j&&strcmp(wordi, wordj) = 0)aj = 0;for (i = 0; i<sum; i+)if (amax<ai)max = i;printf("出现最多次的单词:%s ", wordmax);for (i = 0; i<sum; i+)if (amax = ai && i != max)printf("%s ", wordi);printf(",出现了%d 次n", amax);【精品文档】第 8 页