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


    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;
    }
  • 相关阅读:
    移动端的爬坑路
    判断设备ios或android以及判断是否是微信内置浏览器
    使用vue directive 写好的滑动删除功能
    不用ajax,使用json数据渲染商品的方法
    vue中使用swiper的一些坑
    vue的自定义指令的坑
    better-score获取滑动距离的坑
    linux命令
    关于打印
    数据可视化
  • 原文地址:https://www.cnblogs.com/Blackops/p/5356415.html
Copyright © 2020-2023  润新知