• HDU 1062 Text Reverse


    字符串反转:每个单词反转,然后输出。

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

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 const int N=1005;
     6 int main() {
     7     char ol[N],no[N];
     8     int n;
     9    // freopen("C:\CODE\in.txt", "r", stdin);
    10    // freopen("C:\CODE\out.txt","w",stdout);
    11     scanf("%d",&n);
    12     getchar();
    13     while(n--) {
    14 
    15         gets(ol);
    16 
    17         for(int i=0,j=0; i<=strlen(ol); i++) {
    18             if(ol[i+1]==' '||i+1==strlen(ol)) {
    19 
    20                 no[i+1]=ol[i+1];
    21                 for(int k1=i,k2=j; k1>=j; k2++,k1--) {
    22                     no[k1]=ol[k2];
    23                 }
    24                 j=i+2;
    25 
    26             }
    27         }
    28         puts(no);
    29         memset(no,0,sizeof(no));
    30     }
    31 
    32 
    33     return 0;
    34 }

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

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 const int N=1005;
     6 int main() {
     7     char ol[N],no[N];
     8     int n;
     9     freopen("C:\CODE\in.txt", "r", stdin);
    10     //freopen("C:\CODE\out.txt","w",stdout);
    11     scanf("%d",&n);
    12     getchar();
    13     while(n--) {
    14 
    15         gets(ol);
    16 
    17         for(int i=0,j=0; i<=strlen(ol); i++) {
    18             if(i == strlen(ol)){
    19                 while(j){
    20                     j--;
    21                     printf("%c",no[j]);
    22                 }
    23             }
    24             if(ol[i]!=' '){
    25                 no[j]=ol[i];
    26                 j++;
    27             }
    28             else{
    29                 while(j){
    30                     j--;
    31                     printf("%c",no[j]);
    32                 }
    33                 printf(" ");
    34             }
    35 
    36         }
    37         putchar('
    ');
    38 
    39     }
    40 
    41 
    42     return 0;
    43 }
    ---------------- 人们生成的最美好的岁月其实就是最痛苦的时候,只是事后回忆起来的时候才那么幸福。
  • 相关阅读:
    Spring 事务XML配置
    启用事务注解
    ebay 店铺状态
    lambda Map Reduce
    sublime3注册码
    使用多线程
    Spring AOP学习(六)
    添加依赖库
    CodeForces 867B Save the problem
    POJ 3264 Balanced Lineup (线段树查找最大最小值)
  • 原文地址:https://www.cnblogs.com/livelihao/p/5158770.html
Copyright © 2020-2023  润新知