输入数据有多组,每组占一行,包含一个句子(句子长度小于1000个字符)
对于每个测试示例,要求输出句子中单词反转后形成的句子
hello xiao mi
mi xiao hello
先将字符串按空格拆分成字符串数组 String 中的 spilt() 方法 再用个字符串倒叙输出就可以了 import java.util.*;
public class Main{ public static void main(String [] args){ Scanner sc=new Scanner(System.in); String str=sc.nextLine(); printIt(str); } public static void printIt(String s){ String []st=s.split(" "); String s1=""; for(int i=st.length-1;i>=0;i--){ s1=s1+st[i]+" "; } System.out.println(s1.trim()); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ String str = scanner.nextLine(); String[] strs = str.split(" "); for (int i = strs.length-1; i >= 0; i--) { System.out.print(strs[i]); if(i != 0){ System.out.print(" "); } } } } }
importjava.util.Scanner; public class Main{public static void main(String args[]){ Scanner sc=newScanner(System.in); String s=sc.nextLine(); String[] arr=s.split(" "); int min=0; int max=arr.length-1; while(min<max){ String temp=arr[min]; arr[min]=arr[max]; arr[max]=temp; min++; max--; } for(inti=0;i<arr.length;i++){ if(i<arr.length-1){ System.out.print(arr[i]+" "); }else{ System.out.print(arr[i]); } } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); String str = in.nextLine(); char[] ch = str.toCharArray(); int start = 0; int end = ch.length-1; Main rs = new Main(); rs.reverse(ch,start,end); start = end = 0; while(start < ch.length){ if(ch[start] == ' '){ start++; end++; }else if(end >= ch.length || ch[end] == ' '){ rs.reverse(ch,start,end-1); start = end+1; end = end + 1; }else { end++; } } System.out.println(String.valueOf(ch)); } public void reverse(char[] ch,int start,int end){ while(start < end){ char temp = ch[start]; ch[start] = ch[end]; ch[end] = temp; end--; start++; } } }
import java.util.Scanner; public class Main{ public static void main(String[] args) {
Scanner sc = new Scanner(System.in); while(sc.hasNext()) {
String s = sc.nextLine(); String result = reverse(s); System.out.println(result); }} public static String reverse(String str){
StringBuffer sb = new StringBuffer(); if(-1==str.indexOf(" ")){ return sb.append(str).toString(); } else{
String[] st = str.split(" "); for(int i=0;ilength/2;i++){
String temp = st[i]; st[i] = st[st.length-1-i]; st[st.length-1-i] = temp; } for(int i=0;ilength;i++){ if(0==sb.length()){
sb.append(st[i]); }else{
sb.append(" "+st[i]); }
}
} return sb.toString(); }
}
import java.util.Scanner; public class demo01 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str; String [] str1; while (sc.hasNext()){ str=sc.nextLine(); str1=str.split(" "); for(int i=str1.length-1;i>=0;i--){ if(i!=0){ System.out.print(str1[i]+" "); }else { System.out.print(str1[i]); } }} } }
import java.util.Scanner; public class Main{ public static void main(String [] args){ Scanner sc=new Scanner(System.in); while(sc.hasNext()){ String str=sc.nextLine(); char [] arr=str.toCharArray(); reverse(arr,0,arr.length-1); int left=-1,right=0; for(int i=0;i<arr.length;i++){ if(arr[i]==' '){ right=i; reverse(arr,left+1,right-1); left=right; } } reverse(arr,left+1,arr.length-1); System.out.println(String.valueOf(arr)); } } public static void reverse(char [] nums,int i,int j){ char temp; while(i<j){ temp=nums[i]; nums[i]=nums[j]; nums[j]=temp; i++; j--; } } }
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String str = scanner.nextLine();
String[] strings = str.split(" ");
for (int i = strings.length - 1; i >= 0; i--) {
if (i == 0) {
System.out.print(strings[i]);
} else {
System.out.print(strings[i] + " ");
}
}
System.out.println();
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String test = sc.nextLine();
String[] strs = test.split(" ");
StringBuilder sb = new StringBuilder();
for (int i = strs.length-1;i>=0;i--){
if (i==0){
sb.append(strs[i]);
}else{
sb.append(strs[i]+" ");
}
}
System.out.println(sb.toString());
return;
}
}
}
import java.util.Scanner;
public class juzifanzhuan {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
String[] ss=s.split(" ");
for(int i=ss.length-1;i>=1;i--) {
System.out.print(ss[i]+" ");
}
System.out.print(ss[0]);
}
}
import java.util.Stack; import java.util.Scanner; public class Main{ public static void main(String []args){ Stack<String> st = new Stack<String>(); Scanner s = new Scanner(System.in); while(s.hasNext())st.add(s.next()); boolean first=true; while(st.isEmpty()!=true){ if(first==false)System.out.print(" "); else first=false; System.out.print(st.pop()); } System.out.println(); } }
测试对比了以下使用ArrayList和Stack数据结构的性能
使用ArrayList
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.IOException;
import java.util.Collections;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
while((str = br.readLine() ) != null){
String[] arr = str.split(" ");
ArrayList list = new ArrayList();
for(String item:arr){list.add(item);}
Collections.reverse(list);
for(int i = 0;i < list.size();i ++){
if(i == list.size() - 1 )
System.out.print(list.get( i ));
else
System.out.print(list.get(i)+" ");
}
}
}
}
使用Stack
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.IOException;
import java.util.Collections;
import java.util.Stack;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
while((str = br.readLine() ) != null){
String[] arr = str.split(" ");
Stack stack = new Stack();
for(int i = 0;i < arr.length ;i++){
if(i == arr.length - 1){
stack.push(arr[i]);
}else{
stack.push(arr[i]);
stack.push(" ");
}
}
while(!stack.isEmpty()){
System.out.print(stack.pop());
}
}
}
}
时间:14ms
空间:8564k
真是十分明显的时间换取空间