题解 | #句子逆序#

句子逆序

https://www.nowcoder.com/practice/48b3cb4e3c694d9da5526e6255bb73c3

import java.io.IOException;
import java.util.Scanner; 
public class Main {
    public static void main(String[] args) throws IOException {
       Scanner sc = new Scanner(System.in);
        StringBuffer sb =new StringBuffer();

        while(sc.hasNextLine()){
            String str = sc.nextLine();

            if(!str.contains(" ")){
                for (int i = 0; i < str.length(); i++) {
                    System.out.print(str.charAt(i));
                }
            }else{
                String[] strings = str.split(" ");
                for (int i = strings.length - 1; i >=0; i--) {
                    if(i == 0) {
                        System.out.println(strings[0]);
                        strings[0] = "";

                    }
                    System.out.print(strings[i]+" ");
                    strings[i] = "";
                }
            }

        }

    }
}
#华为OD机考#
全部评论

相关推荐

04-06 11:24
已编辑
太原学院 C++
真烦好烦真烦:感觉不太对劲,这种主动加微信的一般都是坑,要小心辨别
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务