原题如下
1009. 说反话 (20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。
输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有多余的空格。
输出格式:每个测试用例的输出占一行,输出倒序后的句子。
输入样例:
Hello World Here I Come
输出样例:
Come I Here World Hello
笔者用C语言写的代码如下
#include "stdio.h"
int main()
{
char a[100][100],temp;
int i=0,j=0;
while((temp=getchar())!='\n')
{
if(temp==' ')
{
i++;
j=0;
}
else
{
a[i][j]=temp;
j++;
}
}
for(j=i; j>0; j--)
printf("%s ",a[j]);
printf("%s",a[j]);
return 0;
}
C语言提交结果如下:
C++代码如下
#include "iostream"
#include "stdio.h"
using namespace std;
int main()
{
char a[100][100],temp;
int i=0,j=0;
while((temp=getchar())!='\n')
{
if(temp==' ')
{
i++;
j=0;
}
else
{
a[i][j]=temp;
j++;
}
}
for(j=i; j>0; j--)
cout<<a[j]<<" ";
cout<<a[j];
return 0;
}
C++提交结果如下
Java代码如下
/**
* Created by muyangren907 on 2017/3/16/0016.
*/
import java.util.Scanner;
public class Main {
public static void main(String[]args)
{
String[] s=new String[80];
String temp;
int i=0,j,kg=0;
Scanner sc=new Scanner(System.in);
temp=sc.nextLine();
char[] c=new char[temp.length()];
c=temp.toCharArray();
for(i=0; i<temp.length(); i++)
{
if(c[i]==' ')
kg++;
}
Scanner in=new Scanner(temp).useDelimiter("\\s");
for(i=0; i<kg+1; i++)
s[i]=in.next();
for(j=i-1; j>0; j--)
System.out.printf("%s ",s[j]);
System.out.printf("%s",s[j]);
}
}
Java提交结果如下