zoukankan      html  css  js  c++  java
  • p1205单词翻转-递归解决

    题目描述 Description

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

    输入描述 Input Description

    输入包括一个英语句子。

    输出描述 Output Description

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

    样例输入 Sample Input

    I love you

    样例输出 Sample Output

    you love I

    /*
    作者:1c3z
    题目:p1205 单词翻转
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    void fun(char *str, int low, int hi)
    {
        if(low < -1)
        {
            return;
        }
        //判断是不是空格,或者到句末了
        if(low == -1 || str[low] == ' ' )
        {
            int i;
            for(i = low + 1; i < hi; i++)
            {
                printf("%c", str[i]);
            }
            //最后一个单词后面不应该有空格
            if(low != -1)
            {
                printf(" ");
            }
    
            fun(str, low - 1, low);
        }
        else
        {
            fun(str, low - 1, hi);
        }
    }
    
    int main()
    {
    
        char str[100] = {''};
        int len;
        gets(str);
        len = strlen(str);
        fun(str, len - 1, len);
    
        return 0;
    }
  • 相关阅读:
    pat 1034 Head of a Gang (30分)
    pta坑点
    Devc++ 编译 c++11
    Invitation Cards dijkstra法
    Cube Stacking
    畅通工程
    蚂蚁上树
    洛谷 P1439 【模板】最长公共子序列
    Recursive sequence
    A Simple Math Problem
  • 原文地址:https://www.cnblogs.com/icez/p/4188133.html
Copyright © 2011-2022 走看看