TEST # 1
Description: If your are in a company, and you have just finished writing a program, the output of which is a list of names in ascending order by length (i.e. each name is not shorter than the one preceding it). However, the boss want the list to apear more symmetric with shorter names at the top or bottom, while the longer names in the middle. His rule is that each pair of names belongs on opposite ends of the list, and the first name of each pair is always in the top part of the symmetric list. In the first example set as follow, Jo and Lat are the first pair, Kean and Kevin are the sceond pair, etc.
Input: The inupt consists of one or more sets of strings, followed by the value 0 at the final line to indicate that the input has completed. Each set starts with a line containing an interger, n, which is the number of strings in the set, followed by n strings, one per line, sorted in ascending 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
Jo
Lat
Kean
Kevin
Claude
Willianm
Marybeth
6
Jam
Bin
Zoe
Joey
Frederick
Annabelle
5
Bill
Fran
John
Cece
Stan
0
Sample Output:
SET 1
Jo
Kean
Claude
Marybeth
Willianm
Kevin
Lat
SET 2
Jam
Zoe
Frederick
Annabelle
Joey
Bin
SET 3
Bill
John
Stan
Cece
Fran
实现代码如下:
/*******************************************
*项目:算法实验一 TEST 1 V1
*姓名:卿*
*学号:SA14226***
*******************************************/
#include "stdio.h"
#include "string.h"
/*
函数名:initbuf
输入:char *buf[],int n
功能:为字符串指针数组buf对初始化规模n做初始化空间申请并分配
*/
int initbuf(char *buf[],int n){
int i;
for(i = 0;i < n;i++){
buf[i] = (char *)malloc(sizeof(char)*26);
}
return 0;
}
/*
函数名:freebuf
输入:char *buf[],int n
功能:为字符串指针数组buf对规模为n大小做空间释放
*/
int freebuf(char *buf[],int n){
int i;
for(i = 0;i < n;i++){
free(buf[i]);
}
return 0;
}
int main(){
int n;
int count = 0;
int i,j;
char *buf[15];
while(scanf("%d",&n)){
if (n == 0)
break;
count++;
initbuf(buf,n);
/*数据读入*/
for(i = 0;i < n;i++) {
scanf("%s",(char *)buf[i]);
}
/*数据输出*/
printf("SET %d\n", count);
for(j = 0;j < n;j++){
/*当输入字符串组数量为奇数时进行输出*/
/*先输出指针数组中的奇数项*/
/*再逆序输出其中的偶数项*/
if(n%2 == 1){
if (j <= n/2)
printf("%s\n",buf[2*j]);
else
printf("%s\n",buf[2*(n-j)-1]);
}
/*当输入字符串组数量为偶数时进行输出*/
/*先输出指针数组中的奇数项*/
/*再逆序输出其中的偶数项*/
else{
if (j < n/2)
printf("%s\n",buf[2*j]);
else
printf("%s\n",buf[2*(n-j)-1]);
}
}
freebuf(buf,n);
}
return 0;
}