UVa——1593Alignment of Code(string重定向+vector数组)

本文介绍了一种解决UVA-1593 Alignment of Code问题的方法,该问题要求根据每列中最长字符串的长度来格式化多行包含短字符串的数据。文章提供了一个C++实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UVA - 1593

Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

 

Status

Description

Download as PDF
 

题意:输入数行数据,一行数据多个短字符串,输出要按照每列最长的占位进行输出。

下面是具体显示效果(为了看清楚具体输出我把空格换成了"|")

输入:

--------------------------------------------

  start:  integer;    // begins here
stop: integer; //  ends here
 s:  string;
c:   char; // temp

输出:

--------------------------------------------

start:|integer;|//|begins|here
stop: |integer;|//|ends  |here
s:    |string;
c:    |char;   |//|temp

代码:

#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstdio>
#include<set>
#include<sstream>
#include<map>
#include<vector>
#include<iomanip>
using namespace std;
static int len[50];//记录每列字符串的最大长度
int main(void)
{
	string s,one;
	vector<string>list[1001];//每一个vector都是一个数组,类似于可变长的二维数组
	vector<string>::iterator it;
	int hang=0,lie,maxlen=0;
	while (getline(cin,s))
	{
		lie=0;
		istringstream sin(s);//重定向
		while (sin>>one)
		{
			list[hang].push_back(one);//加入到自己所在行
			len[lie]=max(len[lie],(int)one.size());//每列的最大长度
			lie++;//列数更新
		}
		hang++;//行数更新
	}	
	for (int i=0; i<hang; i++)
	{
		for (int j=0; j<list[i].size(); j++)
		{
			if(j!=list[i].size()-1)
				cout<<left<<setw(len[j])<<list[i][j]<<" ";//输出占位+一个空格
			else
				cout<<left<<list[i][j]<<endl;//输出占位不加空格				
		}
	}
	return 0;
}

转载于:https://www.cnblogs.com/Blackops/p/5356415.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值