Symmetric Order
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 14750 Accepted: 8722
Description
In your job at Albatross Circus Management (yes, it’s run by a bunch of clowns), you have just finished writing a program whose output is a list of names in nondescending order by length (so that each name is at least as long as the one preceding it). However, your boss does not like the way the output looks, and instead wants the output to appear more symmetric, with the shorter strings at the top and bottom and the longer strings in the middle. His rule is that each pair of names belongs on opposite ends of the list, and the first name in the pair is always in the top part of the list. In the first example set below, Bo and Pat are the first pair, Jean and Kevin the second pair, etc.
Input
The input consists of one or more sets of strings, followed by a final line containing only the value 0. Each set starts with a line containing an integer, n, which is the number of strings in the set, followed by n strings, one per line, sorted in nondescending order by length. None of the strings contain spaces. There is at least one and no more than 15 strings per set. Each string is at most 25 characters long.
Output
For each input set print “SET n” on a line, where n starts at 1, followed by the output set as shown in the sample output.
Sample Input
7
Bo
Pat
Jean
Kevin
Claude
William
Marybeth
6
Jim
Ben
Zoe
Joey
Frederick
Annabelle
5
John
Bill
Fran
Stan
Cece
0
Sample Output
SET 1
Bo
Jean
Claude
Marybeth
William
Kevin
Pat
SET 2
Jim
Zoe
Frederick
Annabelle
Joey
Ben
SET 3
John
Fran
Cece
Stan
Bill
法一:
在输入的时候直接将每个字符串分配到他原有的位置,然后顺序输出
import java.util.Scanner;
public class Main {
public static void main(String []args) {
Scanner in = new Scanner(System.in);
int x,n=1;
while((x=in.nextInt())!=0) {
in.nextLine();
String []str = new String[x];
for(int i=0;i<x;i++)
{
if(i%2==0) {
str[i/2]=in.nextLine();
}else {
str[x-i/2-1]=in.nextLine();
}
}
System.out.println("SET "+n);
n++;
for(int i=0;i<x;i++)
{
System.out.println(str[i]);
}
}
}
}
时间复杂度:O(3n)。输入并分组数据O(2n),输出数据O(n).
法二:
先将所有字符串存储到字符串数组中,然后按条件先输出前半部分,之后再输出后半部分。
import java.util.Scanner;
public class Main {
public static void main(String []args) {
Scanner in = new Scanner(System.in);
int x,n=1;
while((x=in.nextInt())!=0) {
in.nextLine();
String []str = new String[x];
for(int i=0;i<x;i++)
{
str[i]=in.nextLine();
}
System.out.println("SET "+n);
n++;
//先输出前半部分
for(int i=0;i<x;i=i+2)
{
System.out.println(str[i]);
}
//之后输出后半部分
for(int i=x-x%2-1;i>=1;i=i-2)
{
System.out.println(str[i]);
}
}
}
}
时间复杂度:O(2n)。输入数据O(n),输出结果O(n)。
法三:
递归处理
每次讲第偶数个数据打印输出,将第奇数个数据压栈。当偶数数据输出完毕,依次出栈打印奇数数据。
import java.util.Scanner;
public class Main {
public static void print(int n,Scanner in) {
String str;
System.out.println(in.nextLine());
if(--n!=0) {
str=in.nextLine();
if(--n!=0) print(n,in);
System.out.println(str);
}
}
public static void main(String []args) {
Scanner in = new Scanner(System.in);
int n,loop=1;
while((n=in.nextInt())!=0) {
in.nextLine();
System.out.println("SET "+loop);
loop++;
print(n,in);
}
}
}