Open maud99 opened 2 years ago
同学,要考虑n=1的情况,我加入考虑n=1的情况后通过了。
是要考虑n=1的情形,分享一下我的代码 `
using namespace std;
int main() { int n; string s; cin >> n>>s; char c[20][20] = { 0}; int i, j; int x = 0, y = 0; bool downFlag = 0; if (n != 1) { for (i = 0; i < s.size(); i++) { c[x][y] = s[i]; if (x == 0 || x == n - 1) downFlag = 1 - downFlag;
if (downFlag)x++;
else
{
x--;
y++;
}
}
}
else
for (i = 0; i < s.size(); i++)
c[0][i] = s[i];
for (i = 0; i < n; i++)
for (j = 0; j < s.size()&&j<20;j++)
if (c[i][j] != '\0')
cout << c[i][j];
return 0;
}`
include
include
using namespace std;
int main() { int n; string s1; cin >> n >> s1;//输入 string s2;//存储转变后的字符串 int i = 0;//第一行的情况 while (i <= s1.length()-1) { s2 += s1[i]; i += 2n-2; } for (i = 1;i <= n-2;i++)//中间几行的情况 { int j = i;//第i+1行 while (j <= s1.length()-1) { s2 += s1[j]; if (j+2n-2i-2 <= s1.length()-1) s2 += s1[j+2n-2i-2]; j += 2n-2; } } i = n-1;//最后一行 while (i <= s1.length()-1) { s2 += s1[i]; i += 2*n-2; } cout << s2;//输出变换后的字符串 return 0; }
可以请助教老师和通过的同学帮忙看看吗