zoukankan      html  css  js  c++  java
  • 北邮OJ103.反转单词 c++/java

    103. 反转单词

    时间限制 1000 ms 内存限制 65536 KB

    题目描述

    给出一句英文句子(只由大小写字母和空格组成,不含标点符号,也不会出现连续的空格),请将其中的所有单词顺序翻转

    输入格式

    多组数据,以EOF结束。
    每行一句英文句子(确保只由大小写字母和空格组成,不含标点符号,也不会出现连续的空格,字符串总长度1000以内)

    输出格式

    每组数据输出一行,为反转后的字符串

    输入样例

    It is a apple

    输出样例

    apple a is It

    eof: 在window下时是ctrl+z 

           在linux下时是ctrl+d

    c++:

    #include<stdio.h>
    #include<string>
    #include<stack>
    
    #include<iostream>
    using namespace std;
    int main(){
        stack<string> s;
        string ss;
        while (cin>>ss){
            s.push(ss);
            if(cin.get() =='
    '){
                while(true){
                    ss = s.top();
                        s.pop();
                        cout<<ss;
                        if(!s.empty())cout<<" ";
                        if(s.empty()) break;
                }
    cout<<endl;
            }
    
        }
    
    
    	return 0;
    
    }
    

    java:

    
    
    import java.io.BufferedInputStream;
    import java.util.Scanner;
    
    public class FanZhuanDanCiMain {
    
    	public static void main(String[] args) {
    		Scanner scan = new Scanner(new BufferedInputStream(System.in));
    		String a ;
    		String as[] ;
    		while(true){
    			a = scan.nextLine();
    			int tem = a.split(" ").length;
    			as = a.split(" ");
    			for(int i=tem-1;i>=0;i--){
    				if(i==0){
    					System.out.println(as[0]);
    				}else{
    					System.out.print(as[i]+" ");
    				}
    				
    			}
    		}
    	}
    
    }
    
  • 相关阅读:
    python汉诺塔
    圆周率计算
    PIL: 建立一个GIF图
    Jieba库使用和好玩的词云
    Turtle库的建立——汉诺塔
    计算pi的精度+进度条显示
    Python——我所学习的turtle函数库
    Python——教你画朵太阳花
    Python常用模块re的使用
    正则表达式字符组/元字符/量词
  • 原文地址:https://www.cnblogs.com/lingluan533/p/12537293.html
Copyright © 2011-2022 走看看