Java——HDOJ——1062 Text Reverse

Text Reverse

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 38304    Accepted Submission(s): 14950


 

Problem Description

Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.

 

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.

 

Output

For each test case, you should output the text which is processed.

 

Sample Input

 

3 olleh !dlrow m'I morf .udh I ekil .mca

 

Sample Output

 

hello world! I'm from hdu. I like acm.


import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=0;
        n=sc.nextInt();
        char[][] c=new char[n+1][2000];
        int[] a=new int[n+1];
        char t;
        for(int i=0;i<n+1;i++) {
            String str=sc.nextLine();
            int l=str.length();a[i]=l;
            int start=0;
            for(int j=0;j<l;j++) {
                t=str.charAt(j);
                if(t==' ') {
                    for(int k=start,kk=j-1;k<j;k++,kk--) {
                        c[i][k]=str.charAt(kk);
                    }
                    c[i][j]=' ';
                    start=j+1;
                }    
            }
            for(int k=start,kk=l-1;k<l;k++,kk--) 
                c[i][k]=str.charAt(kk);
        }
        for(int i=1;i<n+1;i++) {
            for(int j=0;j<a[i];j++)
            System.out.print(c[i][j]);
            System.out.println();
        }
    }
}
 

猜你喜欢

转载自blog.csdn.net/lannister_awalys_pay/article/details/82811424