1009. 说反话 (20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。
输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有多余的空格。
输出格式:每个测试用例的输出占一行,输出倒序后的句子。
输入样例:Hello World Here I Come输出样例:
Come I Here World Hello
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char ch[80];
gets(ch);
int length = strlen(ch);
int pos[80];
int count = 0;
for(int i=0;i<length;i++){
if(ch[i]==' '){
pos[count++] = i;
}
}
pos[count] = length;
for(int i=count-1;i>=0;i--){
for(int j = pos[i]+1;j<pos[i+1];j++){
cout<<ch[j];
}
cout<<" ";
}
for(int i=0;i<pos[0];++i)
cout<<ch[i];
return 0;
}
Java:
import java.util.Scanner;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input= new Scanner(System.in);
String s;
s = input.nextLine();
String[] strA = s.split(" ");
for(int i= strA.length-1;i>=0;i--){
System.out.print(strA[i]);
if(i>0)
System.out.print(" ");
}
}
}