核心:将字符存进string数组中,输出即可
getline(cin, str)的复习
满分
核心: vector的erase的使用, 传参是vector.begin()
/**
题意: 矩阵存储字符,通过words,一行打印字符
难点: 存储数据 vector<string> a;
先将字母抽离到vector<string> b中, 再循环b输出每一个字符
*/
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
vector<string> alp[30], word;
int main()
{
string str, s;
for(int i = 0; i < 26; i++){
for(int j = 0; j < 7; j++)
{
getline(cin, str);
alp[i].push_back(str);
}
}
getline(cin, s);
// erase是索引,必须使用s.begin(), 而不是s[0]
while (!isupper(s[0])) s.erase(s.begin());
int pos = 0, t = 0;
while (t < s.size())
{
while (isupper(s[t])) t++;
word.push_back(s.substr(pos, t - pos));
while (!isupper(s[t])) t++;
pos = t;
}
for(int i = 0; i < word.size(); i++)
{
if (i) printf("\n");
string sw = word[i];
for(int k = 0; k < 7; k++)
{
string tmp;
for(int j = 0; j < sw.size(); j++)
{
if (j != 0) tmp += " ";
tmp += alp[sw[j] - 'A'][k];
}
cout << tmp << '\n';
}
}
}
13分 代码 两个格式错误, 一个内存超限
方法一(较好):
#include <iostream>
#include <cstring>
using namespace std;
const int N = 200;
string l[N], res[N];
int main()
{
string str;
for(int i = 0; i < 182; i++)
{
getline(cin , str);
l[i] = str;
}
getline(cin, str);
int len = str.length();
int cnt = 7;
bool flag = true;
int f = 0;
for(int i = 0; i < len; i++)
{
int x = str[i] - 'A';
x *= 7;
if (str[i] >= 'A' && str[i] <= 'Z')
{
int idx = 0;
for(int j = cnt - 7; j < cnt; j++)
{
if (flag) flag = false;
else if (!flag && f >= 7) res[j] += " ";
res[j] += l[x + idx];
idx++;
f++;
}
}else
{
if (!flag && i != len - 1)
{
cnt++;
cnt += 7;
}
f = 0;
flag = true;
}
}
for(int i = 0; i < cnt; i++)
{
cout << res[i] << '\n';
}
}
1分 3个格式错误, 一个运行错误
方法二(vector设置大一些即可)
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
const int N = 190;
string arr[N];
vector<string> vc[N *2];
int cnt = 0, idx = 0;
bool check(char s)
{
if (s >= 'A' && s <= 'Z') return true;
else return false;
}
void getres(char s)
{
cnt = idx;
int x = s - 'A';
x = x * 7 + 1;
for(int i = x; i <= x + 6; i++)
{
vc[cnt++].push_back(arr[i] + " ");
}
}
int main()
{
string str;
for(int i = 1; i <= 182; i++)
{
getline(cin, str);
arr[i] = str;
}
getline(cin, str);
for(int i = 0; i < str.size(); i++)
{
if (check(str[i]))
{
break;
}else cnt++;
}
str = str.substr(cnt, str.size() - cnt);
string res;
int j;
for(int i = 0; i < str.size(); i++)
{
res += str[i];
if (!check(str[i]))
{
j = i + 1;
while (!check(str[j])) j++;
}
}
for(int i = 0; i < res.size(); i++)
{
if (check(res[i]))
{
getres(res[i]);
}else {
idx = cnt;
idx ++;
}
}
int len = max(cnt, idx);
for(int i = 0; i < len; i++)
{
if (vc[i].size() > 0)
{
for(j = 0; j < vc[i].size(); j++)
{
if (j == vc[i].size() - 1) vc[i][j].pop_back();
printf("%s", vc[i][j].c_str());
}
}
printf("\n");
}
}
满分, 第一次写的代码
方法三
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
const int N = 200;
const int M = 3000;
vector<string> res[N];
// 输入的时候段错误了
vector<string> vc[M];
int n1, cnt = 0;
void out_it(int x)
{
int len = x * 7;
int beg = len - 6;
// 每一列行向输出
n1 = cnt;
for(int i = beg; i <= len; i++)
{
vc[n1].push_back(res[i][0]);
string s = " ";
vc[n1].push_back(s);
n1++;
}
// 输出一列空格
}
void out_vc()
{
cnt = n1;
cnt++;
}
bool check(char a)
{
if (a >= 'A' && a <= 'Z')
{
return true;
}
return false;
}
int main()
{
int m = 26, x = 5, y = 7;
for(int i = 1; i <= 182; i++)
{
string sstr;
getline(cin, sstr);
res[i].push_back(sstr);
}
string str;
//getchar(); // 吸收换行, 在scanf或cin 之后,需要吸收换行
getline(cin , str);
for(int i = 0; i < str.size(); i++)
{
// 为啥下划线会进去???
if (check(str[i]))
{
int a = str[i] - 'A' + 1;
// 输出
out_it(a);
}else {
// 输入一行空格
out_vc();
}
}
bool flag = false;
for(int i = 0; i < n1; i++)
{
//printf("%d\n", vc[i].size());
if (vc[i].size() == 0) {
if (flag) printf("\n");
continue;
}
for(int j = 0; j < vc[i].size() - 1; j++)
{
flag = true;
cout << vc[i][j];
}
if (vc[i].size() == 0) cout << '\n';
if (i != n1 - 1) cout << '\n';
//cout << '\n';
}
}