PTA 7-32 说反话-加强版

作者: smatrcHendsa | 来源:发表于2019-03-17 10:06 被阅读0次

    learn how to use cin.getline().
    I haven't considered the cases when input is " " , only one word whose length is 500000, and one word with many spaces behind it;
    https://pintia.cn/problem-sets/14/problems/812

    #include <cstdio>
    #include <iostream>
    #include <cmath>
    #include <algorithm>
    #include <string.h>
    using namespace std;
    char map[5000][200];
    char str[500001];
    
    int main() {
        int i, j;
        int rcnt;
    
        cin.getline(str, 500001);
        rcnt = strlen(str);
    
        for (i = 0; i < strlen(str); i++) {
            if (str[i] != ' ')
                break;
        }
    
        int first = i;
    
        for (i = strlen(str) - 1; i >= 0; i--) {
            if (str[i] == ' ') {
                rcnt = i;
            }
            else {
                while (i >= 0 && str[i] != ' ') {
                    i--;
                }
                for (j = i + 1; j < rcnt; j++) {
                    printf("%c", str[j]);
                }
                if (i >= first) 
                    printf(" ");
                rcnt = i;
            }
        }
    
        printf("\n");
    
        return 0;
    }
    

    相关文章

      网友评论

        本文标题:PTA 7-32 说反话-加强版

        本文链接:https://www.haomeiwen.com/subject/eyvymqtx.html