首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
其他好文
> 详细
UVA 400 Unix ls by sixleaves
时间:
2015-06-05 00:39:17
阅读:
116
评论:
0
收藏:
0
[点我收藏+]
标签:
题目其实很简单,答题意思就是从管道读取一组文件名,并且按照字典序排列,但是输入的时候按列先输出,再输出行。而且每一行最多60个字符。而每个文件名所占的宽度为最大文件名的长度加2,除了输出在最右边的文件名的宽度是最大文件名长度之外。编程实现这个功能,这就是Unix系统下ls命令的一部分而已。具体实现如下。
主要学习的技能要点就是如何用偏移位移法来按列输出。如果一个矩阵n行m列,要按照列输出。但是我们知道,编程时候,只能是for(行)在列。所以必须要有一个偏移量来计算这种偏移关系。x = rows * c + r;
1
#include <iostream>
2
#include <
string
>
3
#include <algorithm>
4
#include <vector>
5
#include <cmath>
6
7
using
namespace
std;
8
9
vector<
string
> words;
10
void
print(
const
string
&s,
int
len,
char
extra);
11
int
main() {
12
13
14
int
n;
15
string
w;
16
17
while
(cin >> n) {
18
19
int
max = 0;
20
words.clear();
21
for
(
int
i = 0; i < n; i++) {
22
cin >> w;
23
string
::size_type str_size = w.size();
24
if
(str_size > max) max = str_size;
25
words.push_back(w);
26
}
27
int
cols = (60 - max) / (max + 2) + 1;
28
int
rows = ceil(
double
(n) / cols);
29
sort(words.begin(), words.end());
30
print("", 60, ‘-‘);
31
cout << endl;
32
for
(
int
r = 0; r < rows; r++) {
33
34
for
(
int
c = 0; c < cols; c++) {
35
36
int
idx = c * rows + r;
37
if
(idx < n) print(words[idx],(c == cols - 1? max: max + 2), ‘ ‘);
38
39
}
40
41
cout << endl;
42
43
}
44
45
46
}
47
48
return
0;
49
50
}
51
52
void
print(
const
string
&s,
int
len,
char
extra) {
53
54
cout << s;
55
string
::size_type str_size = s.size();
56
for
(
int
i = 0; i < len - str_size; i++) {
57
cout << extra;
58
}
59
}
UVA 400 Unix ls by sixleaves
标签:
原文地址:http://www.cnblogs.com/objectc/p/4553412.html
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
分布式事务
2021-07-29
OpenStack云平台命令行登录账户
2021-07-29
getLastRowNum()与getLastCellNum()/getPhysicalNumberOfRows()与getPhysicalNumberOfCells()
2021-07-29
【K8s概念】CSI 卷克隆
2021-07-29
vue3.0使用ant-design-vue进行按需加载原来这么简单
2021-07-29
stack栈
2021-07-29
抽奖动画 - 大转盘抽奖
2021-07-29
PPT写作技巧
2021-07-29
003-核心技术-IO模型-NIO-基于NIO群聊示例
2021-07-29
Bootstrap组件2
2021-07-29
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!