1 Star 0 Fork 0

xzshen / 算法入门经典第五章习题

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
5-1 Alignment of Code.cpp 1.33 KB
一键复制 编辑 原始数据 按行查看 历史
xzshen 提交于 2018-10-22 20:35 . Upload 5-1 Alignment of Code.cpp
#include<iostream>
#include<sstream>
#include<vector>
#include<string>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1000 + 5;
const int maxl = 180 + 5;
vector<string> code[maxn]; //存代码行
int lens[maxl]; //存每列最长的字符长度
void print(const string& s,int len,char extra);
int main(){
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
memset(lens,0,sizeof(lens)); //lens数组清零
string line;
int rows=1,cols=1;
while( getline(cin, line)){
stringstream ss(line);
string s;
int k=1;
while(ss >> s){
int l = s.length();
//cout << l << endl;
lens[k] = max(lens[k], l);
k++;
code[rows].push_back(s);
}
rows++;
cols = max(k,cols); //获取最大列数
//cout << cols << endl;
}
/*
for(int i = 1; i < cols ; i++){
cout << lens[i] << " ";
}
cout << endl;
*/
for(int i = 1; i < rows; i++){
for(int j = 1; j <= code[i].size(); j++){
if( j == code[i].size()) cout << code[i][j-1]; //每行最后一列,输出末尾不能有空格
else print(code[i][j-1], lens[j]+1, ' '); //lens[j]+1保证字符串之间至少有一个' ';
}
cout << endl;
}
return 0;
}
void print(const string& s,int len,char extra){
cout << s;
for(int i=0;i<len-s.length();i++){
cout << extra;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/xzshen/fifth_test.git
git@gitee.com:xzshen/fifth_test.git
xzshen
fifth_test
算法入门经典第五章习题
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891