PTA特色题目 语言 stl语法 乱七八糟

PTA 7-32 说反话-加强版

2019-03-17  本文已影响0人  smatrcHendsa

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;
}
上一篇下一篇

猜你喜欢

热点阅读