[1205 单词翻转] 解题报告

题目描述 Description

给出一个英语句子,希望你把句子里的单词顺序都翻转过来

输入描述 Input Description

输入包括一个英语句子。

输出描述 Output Description

按单词的顺序把单词倒序输出

样例输入 Sample Input

I love you

样例输出 Sample Output

you love I

数据范围及提示 Data Size & Hint

简单的字符串操作

解题思路:

步骤一:简单来讲可以使用两次翻转实现,第一次对整体做一个翻转,得到"ahah tneduts a ma I"

步骤二:然后对得到的字符串中的单词做一个翻转,得到"haha student a am I"

代码:

#include <stdio.h>
#include <string.h>

void Reverse(char*pbegin,char *pend){
    if(pbegin==NULL||pend==NULL)
        return;
    while(pbegin<pend)
    {
        char tmp;
        tmp=*pbegin;
        *pbegin=*pend;
        *pend=tmp;
        ++pbegin;
        --pend;
    }
}

int main()
{
    char str[100];
    char temp = NULL;
    int i = 0;
    while ((temp = getchar())!= '\n') {
        str[i++] = temp;
    }
    str[i] = '\0';

    // 1.整体翻转"tnedutS a ma I"
    Reverse(str, str + strlen(str)-1);

    // 2.单词翻转"Student a am I"
    char *sentence = str;
    char *pWord = str;
    int wordLength = 0;
    while (*sentence != '\0'){         // sentence结束条件
        if (*pWord != ' ') {
            pWord++;
            wordLength++;
        }else{
            Reverse(pWord -wordLength, pWord - 1);     // 当*pWord == ' ',那么就进行单词翻转
            pWord++;
            wordLength = 0;
        }
        sentence++;
    }

    printf("%s\n",str);
    return 0;
}
时间: 2024-08-29 20:30:20

[1205 单词翻转] 解题报告的相关文章

1205 单词翻转

1205 单词翻转 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题解 题目描述 Description 给出一个英语句子,希望你把句子里的单词顺序都翻转过来 输入描述 Input Description 输入包括一个英语句子. 输出描述 Output Description 按单词的顺序把单词倒序输出 样例输入 Sample Input I love you 样例输出 Sample Output you love I 数据范围及提示 Data Size

Codevs 1205 单词翻转

时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题解 题目描述 Description 给出一个英语句子,希望你把句子里的单词顺序都翻转过来 输入描述 Input Description 输入包括一个英语句子. 输出描述 Output Description 按单词的顺序把单词倒序输出 样例输入 Sample Input I love you 样例输出 Sample Output you love I 数据范围及提示 Data Size & Hint 简单的

leetcode 211. 添加与搜索单词 - 数据结构设计 解题报告

设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z . . 可以表示任何一个字母. 示例: addWord("bad") addWord("dad") addWord("mad") search("pad") -> false search("bad"

单词翻转

1205 单词翻转 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题目描述 Description 给出一个英语句子,希望你把句子里的单词顺序都翻转过来 输入描述 Input Description 输入包括一个英语句子. 输出描述 Output Description 按单词的顺序把单词倒序输出 样例输入 Sample Input I love you 样例输出 Sample Output you love I

2020-3-14 acm训练联盟周赛Preliminaries for Benelux Algorithm Programming Contest 2019 解题报告+补题报告

2020-3-15比赛解题报告+2020-3-8—2020-3-15的补题报告 2020-3-15比赛题解 训练联盟周赛Preliminaries for Benelux Algorithm Programming Contest 2019  A建筑(模拟) 耗时:3ms 244KB 建筑 你哥哥在最近的建筑问题突破大会上获得了一个奖项 并获得了千载难逢的重新设计城市中心的机会 他最喜欢的城市奈梅根.由于城市布局中最引人注目的部分是天际线, 你的兄弟已经开始为他想要北方和东方的天际线画一些想法

Winter-2-STL-E Andy&#39;s First Dictionary 解题报告及测试数据

use stringstream Time Limit:3000MS     Memory Limit:0KB Description Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thin

解题报告【pat-1076】

最近一直在忙项目都没时间好好总结写博客,说起来真实惭愧啊. 下面就把自己最近做的几题好好总结一下,主要记录一些注意点,以防以后遇到再犯. 1076. Forwards on Weibo (30) 时间限制 3000 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Weibo is known as the Chinese version of Twitter.  One user on Weibo may have many

USACO Section1.2 Transformations 解题报告

transform解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] 给出两个N×N的矩阵A和B,找出将A转换为B的方式中编号最小的,输出编号. #1:转90度:图案按顺时针转90度. #2:

暑假第二次考试 冲刺Noip2017模拟赛2 解题报告——五十岚芒果酱

题1 牛跑步(running) [题目描述] 新牛到部队,CG 要求它们每天早上搞晨跑,从 A 农场跑到 B 农场.从 A 农场到 B 农场中有 n-2 个路口,分别标上号,A 农场为 1 号,B 农场为 n 号,路口分别为 2...n-1 号,从 A 农场到 B 农场有很多条路径可以到达,而 CG 发现有的路口是必须经过的,即每条路径都经过的路口,CG 要把它们记录下来,这样 CG 就可以先到那个路口,观察新牛们有没有偷懒,而你的任务就是找出所有必经路口. [输入格式] 第一行两个用空格隔开的