NYOJ 283 对称排序
地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=283
题目分析:意思是你已经写好了一个程序,可惜你的程序输出方式老板不喜欢!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.老板希望你能“symmetric”匀称的输出!所以刚开始你就必须认为输入的字符串的长度是和顺序毫无关联的!理解到这里,题目差不多就已经解出来了!即先把字符串排好序,短的在上长的在下,然后“symmetric”的输出就OK了!
思路:
本题题意是先将输入的字符串进行由短到长排序后在进行调换输出。是奇数个则输出1 3 5 7 6 4 2。是偶数个则输出1 3 5 6 4 2
代码如下:
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 using namespace std; 5 string str1[25],str2[25]; 6 bool cmp(string a,string b) 7 { 8 return a.length()<b.length(); 9 } 10 int main() 11 { 12 int n,i,j,k,t=1; 13 while(cin>>n,n) 14 { 15 j=0;k=n-1; 16 for( i = 0 ; i < n ; i ++ ) 17 cin>>str1[i]; 18 sort(str1,str1+n,cmp); 19 for( i = 0 ; i < n ; i ++ ) 20 { 21 if( i % 2 == 0 ) 22 str2[j++] = str1[i]; 23 else 24 str2[k--] = str1[i]; 25 } 26 cout<<"SET "<<t++<<endl; 27 for(i=0;i<n;i++) 28 cout<<str2[i]<<endl; 29 } 30 return 0; 31 } 32