奇偶分开即可。代码简单思路重要。
1 #include<bits/stdc++.h> 2 #include <string> 3 using namespace std; 4 int T; 5 string s; 6 int len; 7 int arr[105]; 8 string s1; 9 string s2; 10 int main(){ 11 cin>>T; 12 while(T--){ 13 cin>>s; 14 len=s.size(); 15 for(int i=0;i<len;i++){ 16 arr[i]=s[i]-'a'; 17 } 18 sort(arr,arr+len); 19 for(int i=0;i<len;i++){ 20 if(arr[i]%2){ 21 s1 += arr[i] + 'a'; 22 }else{ 23 s2 += arr[i] + 'a'; 24 } 25 } 26 if (abs(s1[s1.size() - 1] - s2[0]) != 1){ 27 cout<<s1<<s2<<endl; 28 } else if(abs(s2[s2.size()-1]-s1[0])!=1){ 29 cout<<s2<<s1<<endl; 30 }else{ 31 cout<<"No answer"<<endl; 32 } 33 s=""; 34 s1=""; 35 s2=""; 36 } 37 return 0; 38 }