九度OJ 1195:最长&最短文本 (搜索)
2015-10-26 11:33
344 查看
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:3144
解决:1156
题目描述:
输入多行字符串,请按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输出。
输入:
输入包括多行字符串,字符串的长度len,(1<=len<=1000)。
输出:
按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输出。
样例输入:
样例输出:
来源:2008年华中科技大学计算机研究生机试真题
思路:
先搜索一遍,知道最短长度和最长长度,然后遍历输出。
也可排序后输出。
代码:
内存限制:32 兆
特殊判题:否
提交:3144
解决:1156
题目描述:
输入多行字符串,请按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输出。
输入:
输入包括多行字符串,字符串的长度len,(1<=len<=1000)。
输出:
按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输出。
样例输入:
hello she sorry he
样例输出:
he hello sorry
来源:2008年华中科技大学计算机研究生机试真题
思路:
先搜索一遍,知道最短长度和最长长度,然后遍历输出。
也可排序后输出。
代码:
#include <stdio.h> #include <string.h> #include <stdlib.h> #define N 1000 int cmp(const void *a, const void *b) { return strlen((char *)a) - strlen((char *)b); } int main(void) { int n; int i, j; char s [N+1]; i = 0; while (scanf("%s", s[i]) != EOF) i++; n = i; qsort(s, n, sizeof(s[0]), cmp); for (i=1; i<n && strlen(s[i])==strlen(s[0]); i++) ; for (j=0; j<i; j++) printf("%s\n", s[j]); if (strlen(s[0]) != strlen(s[n-1])) { for (i=n-2; i>=0 && strlen(s[i])==strlen(s[n-1]); i--) ; for (j=i+1; j<n; j++) printf("%s\n", s[j]); } return 0; } /************************************************************** Problem: 1195 User: liangrx06 Language: C Result: Accepted Time:10 ms Memory:1820 kb ****************************************************************/
相关文章推荐
- 你不知道的JavaScript--Item1 严格模式
- IOS 开发,调用打电话,发短信,打开网址
- 机器学习笔记_ch4_分类算法
- C++ static_cast和dynamic_cast的区别
- NS_OPTIONS枚举的用法
- Android 表格布局的RadioButton
- Uber从单体架构转向微服务架构
- msm8974 camera driver添加新摄像头kernel hal修改
- 杂七杂八的
- 九度OJ 1194:八进制 (进制转换)
- 【NOIP2006】金明的预算方案 背包DP
- 互联网专家资源分享(四)
- Python基础(十)正则表达式
- PHP Cookie的用法
- JAVA面试逻辑题1
- 问题:oracle字符串函数;结果:Oracle字符串函数
- 九度OJ 1194:八进制 (进制转换)
- Python 深入理解yield
- ajaxfileupload上传文件成功,但是无法获取到值
- 字符串