当前位置:网站首页>Unix ls
Unix ls
2022-07-17 22:20:00 【zjsru_Beginner】
作者:lht
题目描述:
The computer company you work for is introducing a brand new computer line and is developing a new Unix-like operating system to be introduced along with the new computer. Your assignment is to write the formatter for the ls function. Your program will eventually read input from a pipe (although for now your program will read from the input file). Input to your program will consist of a list of (F) filenames that you will sort (ascending based on the ASCII character values) and format into (C) columns based on the length (L) of the longest filename. Filenames will be between 1 and 60 (inclusive) characters in length and will be formatted into left-justified columns. The rightmost column will be the width of the longest filename and all other columns will be the width of the longest filename plus 2. There will be as many columns as will fit in 60 characters. Your program should use as few rows (R) as possible with rows being filled to capacity from left to right.
Input
The input file will contain an indefinite number of lists of filenames. Each list will begin with a line containing a single integer (1 ≤ N ≤ 100). There will then be N lines each containing one left-justified filename and the entire line’s contents (between 1 and 60 characters) are considered to be part of the filename. Allowable characters are alphanumeric (a to z, A to Z, and 0 to 9) and from the following set {._-} (not including the curly braces). There will be no illegal characters in any of the filenames and no line will be completely empty. Immediately following the last filename will be the N for the next set or the end of file. You should read and format all sets in the input file.
Output
For each set of filenames you should print a line of exactly 60 dashes (-) followed by the formatted columns of filenames. The sorted filenames 1 to R will be listed down column 1; filenames R + 1 to 2R listed down column 2; etc.
思路:
运用c++知识,首先得出最长单词,并将所有单词存入数组并并排序,再运用双循环依次带空格输出。
注意点:
- 计算最长单词时的计算
- 输出时所带空格的计算
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;
const int maxlengh = 60;//最大限制
string s[2000];
int main()
{
int n;
while(~scanf("%d",&n))
{
printf("------------------------------------------------------------\n");
int max_len = 0;
for(int i=0; i<n; i++)//求最长
{
cin >> s[i];
int len = s[i].length();
if(len > max_len) max_len = len;
}
sort(s,s+n);//排序
int line = (maxlengh-max_len)/(max_len+2) + 1;//行
int row = (n-1)/line+1;//列
for(int i=0; i<row; i++)
{
for(int j=0; j<line; j++)
{
int k = i+j*row;
if(k>=n)
continue;
cout << s[k];
int lf = max_len-s[k].length();//多余空格
if(j<line-1)lf+=2;
for(int m=0; m<lf; m++)
putchar(' ');
}
putchar('\n');
}
}
return 0;
}边栏推荐
- 5-21 拦截器 Interceptor
- 6U VPX high speed signal processing board based on ku115+mpsoc (xcku115 + zu9eg +dsp)
- ORA-08103
- End repeated development and personalize the login system in twoorthree times
- SBOM(Software Bill of Materials,软件物料清单)
- JVM常用调优配置参数
- Characteristics of DMA mode
- Principle and simple implementation of custom MVC
- MySQL index (I)
- Li Kou 455 distribute cookie notes
猜你喜欢

Comparison of two virtual machines

国内顶尖专家集聚广州,探讨健康医疗数据安全应用

ICML2022 | 几何多模态对比表示学习

国科大.深度学习.期末复习知识点总结笔记

【萌新解题】四数之和

Database SQL Server

Comparaison de deux types de machines virtuelles
![[flask introduction series] request hook and context](/img/a9/96e330525ee1337daab275b87db892.png)
[flask introduction series] request hook and context

Deployment principle

PCIe Cameralink signal generator (Cameralink image analog source)
随机推荐
MySQL storage functions and triggers
Classification of blocks
Database SQL Server
[mqtt from getting started to improving series | 06] subscribe subscription workflow of mqtt3.1.1
2021.07.13【B站】是这样崩的
SQL related time date type
数据填报、报表展示哪家强?亿信ABI给你答案
MongoDB分片集群搭建
Preview of authtalk phase I | comprehensive dismantling of multi tenant solutions
Chang'an chain learning research - storage analysis wal mechanism
dba
SBOM (software bill of materials)
Zabbix实现对Redis的监控
TDesign compositionapi refactoring path
ICML2022 | 几何多模态对比表示学习
2. MySQL introduction
中断的分类
Sliding window maximum problem
Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言
Is it safe to open a fund account online now?