题目:字符串顺时针旋转90度
注意:刚开始总报错,主要是因为字符串长度不一。输出的时候不会输出\0,所以尾部长短不一的地方就会造成错误。改正方法:当输出时,检测是不是\0,如果是则输出空格
#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <cmath>
#include <ctgmath>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[]) {
string m;
char Output[110][110];
memset(Output, 0, sizeof(Output));
int j = 0;
int length = 0;
int max_length = 0;
while(getline(cin,m)){
length = m.length();
if(length > max_length) max_length = length;
for(int i = 0; i < m.length(); i ++){
Output[i][j]=m[i];
}
j++;
}
for(int i = 0; i < max_length; i++){
for(int m = j - 1; m >= 0; m--) {
if(Output[i][m] == '\0') cout<<" ";//////没有这句话总是WA
else cout<<Output[i][m];
}
cout<<endl;
}
//"I think, therefore I am."
}
本文介绍了一种解决字符串顺时针旋转90度的问题,通过C++实现,重点解决了因字符串长度不一致导致的输出错误问题。文章提供了一个完整的代码示例,包括如何正确处理不同长度字符串的边界条件。
1610

被折叠的 条评论
为什么被折叠?



