简单数据结构-Uva400 Unix Is命令(题解、翻译及分析,参考紫书)

题目分析

本题乍看来比较复杂,其实只是一个简单的排序问题,但是输出有坑(Uva常态)。此处为了理解题意,我们做一个翻译练习,权当练一下英语(为竞赛的英语环境做准备)。

自己写的翻译

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.
你所工作的电脑公司正在推行一个全新的电脑系列(brand:系列,品牌;introduce这里是推行/推出的意思)并且正在研发一种新的Unix式的操作系统(operating system:操作系统),这些操作系统将会随着电脑一起被推出。你的任务(assignment除了有分配的意思之外,还可以理解为被安排的任务)是为Is function写一个格式化程序(formatter:格式化程序)。
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 © 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 ® as possible with rows being filled to capacity from left to right.
你的程序的最终目的是从一个管中(尽管现在你的程序将从输入文件中进行读取)进行读取【这个管很清奇啊】。你输入进去的东西将包含一系列的文件名,这些文件名需要你进行排序【这句话前面的都是废话,这里才到重点,看题时着重注意需要你做什么】(根据ASCII码进行升序排序,ascending:升序)【注意这个ascend单词很重要】,并根据最长文件名来进行列方向的安排。(format:安排,设计;格式化 column:列 就是线性代数里面常用的 r和c里面的那个c)。文件名将包括1到最多60个字母(inclusive:包容的),并进行列方向的左对齐排列(left-justified:左对齐;justify:证明;对。。。解释;调整使全行排满;使每行排齐;使齐行)。最右端列的按照最右端列上最长文件名的长度对齐就行,其它列上的则按照其它所有列的所有文件名的最长的那个来确定带宽,带宽为最长文件名长度加2(即无论其它文件名多长,都要占带宽那么大的长度,且占带宽是左对齐的)。【之前看紫书上那些只言片语一片懵逼,现在看来还是直接看英文原文更容易理解,此处为意译,事实上我们竞赛不一定非要逐字逐句地将英文转译为中文,而是看到英文能理解题目的要求就行。】列是尽可能多的,且每行最多可以包含60个字符(最多60个字符列)。你的程序需要用尽量少的行从左到右进行填充(capacity:容量)
Input
The input file will contain an indefinite number of lists of filenames. Each list will begin with a line containing a single integer ( ). 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.

注:题目英文部分引自https://blog.csdn.net/frankiller/article/details/7655733

自己的代码及注释(有借鉴紫书的部分和对紫书部分代码的讲解)

首先是代码的思想方面,该代码将本题较特殊的地方——输出,单独确定了一个函数,这样就使得除输出之外的地方变得简单了许多,这也是从紫书借鉴来的方法。还有就是,本文代码利用了一个mp数组,以此来先对存放数据的一维数组的各元素序号在二维上进行排列,再通过序号找到各个字符串的位置,是一种间接思路,可以使代码更好打,也更好想。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
const int maxn=150;
void print(string st,int d){          //构建一个可以根据给定带宽左对齐输出的输出函数(注意这里最好写一个函数出来) 
  cout<<st;
  int len=st.length();
  for(int i=0;i<d-len;i++) cout<<' ';
}
int main(){
	
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	string a[maxn];                     //考虑到排序,这里的数据类型采用string
	int mp[maxn][maxn];                 //可以引入一个名为mp的二维数组,用于确定填充(排列)顺序,mp里面存对应的a的序号 
	int n;
	while(cin>>n){                      //Uva常见的多组数据输入情况 
		memset(mp,0,sizeof(mp));
		int maxx=0;
		for(int i=0;i<n;i++){
			cin>>a[i];
			if(a[i].length()>maxx) maxx=a[i].length();//求每组数据的最长单词 
		}
		int maxr,maxc;
		maxc=(60-maxx)/(maxx+2)+1;        //求行和列上各多少个文件名,maxc的求法是先将整体的60个字符,减掉最后一列上的maxx个字符位置。因为无论如何,每行总会存在最后一列,且其必然是maxx个字符,除去最后一列的之外,再进行整除,求取有多少列,然后再把最后一列算上(即后面的+1)。 
		maxr=(n-1)/maxc+1;
		for(int i=0;i<60;i++) cout<<'-';
		cout<<endl;
    sort(a,a+n);                      //根据题意进行排序 
    int k=0;
    
		for(int j=0;j<maxc;j++)           //确定mp数组的内容 
      for(int i=0;i<maxr;i++){
      	mp[i][j]=k;
      	k++;
			}
			
		for(int i=0;i<maxr;i++){          //根据mp数组确定出的顺序,进行输出 
			for(int j=0;j<maxc;j++){
				if(mp[i][j]<n){
			    if(j==maxc-1) print(a[mp[i][j]],maxx);
					else print(a[mp[i][j]],maxx+2);
				}
			}
		  cout<<endl;
		}
		
	} 
	return 0;
} 

作者

Bowen
本文借鉴了紫书部分内容,其余如翻译、注释及大部分代码等均为作者本人原创,转载请注明出处。本文作者水平有限,如有纰漏,敬请斧正。欢迎大家一起交流学习。

发布了50 篇原创文章 · 获赞 7 · 访问量 1157

猜你喜欢

转载自blog.csdn.net/numb_ac/article/details/103038697