第一种:
#include<string.h>
using namespace std;
struct
{
char str[27];
int n;
int length;
}a[17];
int main()
{ int i,n,j,len;
char s[27];
int sum,num;
sum=0;
while(cin>>num)
{ if(num==0) break;
sum++;
i=0;
n=num;
while(num--)
{ cin>>a[i].str;
a[i].length=strlen(a[i].str);
i++;
}
for(i=0;i<n;i++)
{ for(j=0;j<n-1-i;j++)
{
if(a[j].length>a[j+1].length)
{ strcpy(s,a[j].str);
strcpy(a[j].str,a[j+1].str);
strcpy(a[j+1].str,s);
len=a[j].length;
a[j].length=a[j+1].length;
a[j+1].length=len;
}
}
}
cout<<"SET "<<sum<<endl;
for(i=0;i<n;i=i+2)
cout<<a[i].str<<endl;
if(n%2!=0)
{
for(i=n-2;i>=1;i=i-2)
cout<<a[i].str<<endl;
}
if(n%2==0)
{
for(i=n-1;i>=1;i=i-2)
cout<<a[i].str<<endl;
}
}
return 0;
}
题目283优秀代码--运行号:90749
运行时间:
2011-11-13 15:02:24 | 编程语言:
C/C++ | 运行人:
丁国强
01.
#include<stdio.h>
02.
#include<string.h>
03.
main()
04.
{
05.
int
x,i,j,q=0;
06.
char
s[16][26],t[26],z[16][26];
07.
while
(1)
08.
{
09.
scanf
(
"%d"
,&x);
10.
if
(x==0)
break
;
11.
q++;
12.
getchar
();
13.
for
(i=0;i<x;i++)
14.
gets
(s[i]);
15.
for
(i=1;i<x;i++)
16.
for
(j=0;j<x-i;j++)
17.
if
(
strlen
(s[j])>
strlen
(s[j+1]))
18.
{
strcpy
(t,s[j]);
strcpy
(s[j],s[j+1]);
strcpy
(s[j+1],t);}
19.
for
(i=0,j=0;i<x;i++,j++)
20.
{
strcpy
(z[j],s[i]);
21.
if
(i!=x-1)
strcpy
(z[x-j-1],s[++i]);
22.
}
23.
printf
(
"SET %d\n"
,q);
24.
for
(i=0;i<x;i++)
25.
puts
(z[i]);
26.
}
27.
}
第二种(程序略显繁琐,初学者可以参考此程序):#include<iostream>#include<string.h>
using namespace std;
struct
{
char str[27];
int n;
int length;
}a[17];
int main()
{ int i,n,j,len;
char s[27];
int sum,num;
sum=0;
while(cin>>num)
{ if(num==0) break;
sum++;
i=0;
n=num;
while(num--)
{ cin>>a[i].str;
a[i].length=strlen(a[i].str);
i++;
}
for(i=0;i<n;i++)
{ for(j=0;j<n-1-i;j++)
{
if(a[j].length>a[j+1].length)
{ strcpy(s,a[j].str);
strcpy(a[j].str,a[j+1].str);
strcpy(a[j+1].str,s);
len=a[j].length;
a[j].length=a[j+1].length;
a[j+1].length=len;
}
}
}
cout<<"SET "<<sum<<endl;
for(i=0;i<n;i=i+2)
cout<<a[i].str<<endl;
if(n%2!=0)
{
for(i=n-2;i>=1;i=i-2)
cout<<a[i].str<<endl;
}
if(n%2==0)
{
for(i=n-1;i>=1;i=i-2)
cout<<a[i].str<<endl;
}
}
return 0;
}