UVA 1593 Alignment of Code
题意:
输入后对齐输出。
思路:
①用一个vector数组装每行的字符。
②用整型数组len存每列最大的长度加一(多算一个空格),比如在样例输入中,第一列最大的是start:。
1 #include<iostream> 2 #include<algorithm> 3 #include<string> 4 #include<vector> 5 #include<sstream> 6 #include<cstdio> 7 #include<set> 8 #include<cstring> 9 using namespace std; 10 vector<string> vtr[1005]; 11 string str; 12 int len[185];//记录每列string最大的长度 13 int main() 14 { 15 int cnt = 0;//数行数 16 memset(len, 0, 185); 17 while (getline(cin, str)) 18 { 19 stringstream ss(str); 20 string word; 21 int cnt_word = 0;//数单词数 22 while (ss >> word) 23 { 24 vtr[cnt].push_back(word); 25 //每次比较,存入最大的长度,length+1是因为最少都要一个空格 26 len[cnt_word] = len[cnt_word] > (word.length() + 1) ? len[cnt_word] : word.length() + 1; 27 cnt_word++; 28 } 29 cnt++; 30 } 31 32 for (int i = 0; i < cnt; i++)//行 33 { 34 for (int j = 0; j < vtr[i].size(); j++)//列 35 { 36 if (len[j]> vtr[i][j].length())//不够长要补空格 37 { 38 cout << vtr [i][j]; 39 for (int k = 0;j!= vtr[i].size()-1&& k < len[j] - vtr[i][j].length(); k++) 40 { 41 cout << " "; 42 } 43 } 44 else 45 { 46 cout << vtr[i][j]; 47 } 48 } 49 50 cout << "\n"; 51 } 52 53 return 0; 54 }
分类:
OJ
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步