作业10-数组答案

上传人:飞****9 文档编号:127830666 上传时间:2020-04-06 格式:DOC 页数:6 大小:20KB
返回 下载 相关 举报
作业10-数组答案_第1页
第1页 / 共6页
作业10-数组答案_第2页
第2页 / 共6页
作业10-数组答案_第3页
第3页 / 共6页
作业10-数组答案_第4页
第4页 / 共6页
作业10-数组答案_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《作业10-数组答案》由会员分享,可在线阅读,更多相关《作业10-数组答案(6页珍藏版)》请在金锄头文库上搜索。

1、1. 选择法排序1.1. 题目要求:输入一个正整数 n (1n=10),再输入 n 个整数,将它们从大到小排序后输出。1.2. 测试结果(输入结束回车符用表示,括号内为说明)(1) 输入:4 (n=4) 5 1 7 6 输出:After sorted: 7 6 5 1(2) 输入:5 (n=5) 5 4 3 2 1 输出:After sorted: 5 4 3 2 1#include int main(void) int i, index, k, n, temp; int a10; scanf(%d, &n); for(i = 0; i n; i+) scanf(%d, &ai); /*-*/

2、for(i = 0; i n-1; i+)index=i; for(k = i+1; k n; k+) if (aindexak) index=k; if (index!=i) temp=ai; ai=aindex; aindex=temp; printf(After sorted: ); for(i = 0; i n; i+) printf(%d , ai); printf(n); 2. 在数组中查找指定的元素2.1. 题目要求输入一个正整数 n (1n=10),再输入 n 个整数,将它们存入数组 a 中,再输入一个整数 x,然后在数组 a 中查找与 x 相同的元素,如果找到,输出 x 在数

3、组 a 中对应元素的最小下标,如果没有找到,输出相应信息。2.2. 测试结果(输入结束回车符用表示,括号内为说明)(1) 输入:6(n=6) 1 3 5 7 9 55(x=5) 输出:5: a2(2) 输入:4 (n=4)1 3 5 72(x=2) 输出:2: not found#include int main(void) int flag, i, n, x; int a10; scanf(%d, &n); for(i = 0; i n; i+) scanf(%d, &ai); scanf(%d, &x); flag=0; for(i = 0; i n; i+) if (ai=x) flag

4、=1; break;/*-*/ if(flag != 0) printf( %d: a%dn, x, i); else printf( %d: not foundn, x);3. 字符串逆序输出3.1. 题目要求输入一个以回车结束的字符串(少于80个字符),将它的内容逆序输出。如ABCD 的逆序为DCBA。3.2. 测试结果(输入结束回车符用表示)(1)输入:Welcome to you! 输出:!uoy ot emocleW(2)输入:Happy New Year! 输出:!raeY weN yppaH#include int main(void) int i, j, temp; char

5、str80; i = 0; while(stri = getchar( ) != n) i+; stri = 0;i-;for(j=0;ji/2;j+)temp=stri-j;stri-j=strj;strj=temp;/*-*/ for(i = 0; stri != 0; i+) putchar(stri);4. 输出字符在字符串中所对应的最大下标4.1. 题目要求输入一个字符,再输入一个以回车结束的字符串(少于80个字符),在字符串中查找该字符,如果找到,输出该字符在字符串中所对应的最大下标 (下标从0开始);否则输出Not Found。4.2. 测试结果(输入结束回车符用表示)(1)输入

6、:m (字符m)programming(字符串programming) 输出:index = 7 (m在programming中对应的最大下标是7)(2)输入: a(字符a)1234(字符串1234) 输出: Not Found (1234中没有a) #include stdio.h#define MAXLEN 80int main(void) char cc, ch; char strMAXLEN; int count, flag, i, index; cc = getchar(); getchar(); i = 0; while(ch = getchar() != n) stri+ = c

7、h; stri = 0;i-;flag=0;for( ;i=0;i-)if(stri=cc) flag=1;index=i;break;/*-*/ if(flag != 0) printf(index = %dn, index); else printf(Not Foundn); 5. 统计大写辅音字母的个数5.1. 题目要求输入一个以回车结束的字符串(少于80个字符),统计并输出其中大写辅音字母的个数。大写辅音字母:除A, E, I, O, U以外的大写字母。5.2. 测试结果(输入结束回车符用表示)(1)输入:HELLO 输出:count = 3 (HELLO中有3个大写辅音字母)(2)输

8、入:group (字符a) 输出:count = 0 (group中没有大写辅音字母)#include stdio.h#define MAXLEN 80int main(void) char ch; char strMAXLEN; int count, i; i = 0; while(ch = getchar() != n) stri+=ch; stri = 0;count=0;for(i=0;stri!=0;i+)if (striA&stri=Z&stri!=E&stri!=I&stri!=O&stri!=U)count+; printf(count = %dn, count); 6. 字符

9、串替换6.1. 题目要求输入一个以回车结束的字符串(少于80个字符),将其中的大写字母用下面列出的对应大写字母替换,其余字符不变,输出替换后的字符串。原字母 对应字母 A Z B Y C X D W X C Y B Z A6.2. 测试结果(输入结束回车符用表示)(1)输入:A flag of USA 输出:After replaced: Z flag of FHZ(2)输入:1+2=3 (字符a) 输出:After replaced: 1+2=3#include stdio.h#include string.h#define MAXLEN 80int main(void) char ch; char str80; int i; i = 0; while(ch = getchar() != n) stri = ch; i+; stri = 0;for(i=0;stri!=0;i+)if (stri=A&stri=Z)stri=Z-stri+A; printf(After replaced: ); for(i = 0; stri != 0; i+) putchar(stri); putchar(n);

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 中学教育 > 其它中学文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号