您的位置:首页 > 其它

HDU 1062 Text Reverse

2016-01-25 21:28 465 查看
字符串反转:每个单词反转,然后输出。

PE做法:所有单词反转并写入另一个数组中,附代码

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=1005;
int main() {
char ol
,no
;
int n;
// freopen("C:\\CODE\\in.txt", "r", stdin);
// freopen("C:\\CODE\\out.txt","w",stdout);
scanf("%d",&n);
getchar();
while(n--) {

gets(ol);

for(int i=0,j=0; i<=strlen(ol); i++) {
if(ol[i+1]==' '||i+1==strlen(ol)) {

no[i+1]=ol[i+1];
for(int k1=i,k2=j; k1>=j; k2++,k1--) {
no[k1]=ol[k2];
}
j=i+2;

}
}
puts(no);
memset(no,0,sizeof(no));
}

return 0;
}


AC做法:每个单词写入数组并在空格时输出,考虑结束的情况,附代码

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=1005;
int main() {
char ol
,no
;
int n;
freopen("C:\\CODE\\in.txt", "r", stdin);
//freopen("C:\\CODE\\out.txt","w",stdout);
scanf("%d",&n);
getchar();
while(n--) {

gets(ol);

for(int i=0,j=0; i<=strlen(ol); i++) {
if(i == strlen(ol)){
while(j){
j--;
printf("%c",no[j]);
}
}
if(ol[i]!=' '){
no[j]=ol[i];
j++;
}
else{
while(j){
j--;
printf("%c",no[j]);
}
printf(" ");
}

}
putchar('\n');

}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: