1 #include <iostream> 2 #include <string> 3 #include <stdio.h> 4 #include <cmath> 5 #include <algorithm> 6 using namespace std; 7 8 bool judge(string &s) 9 { 10 if ( abs (count(s.begin(),s.end(),'B') - (count(s.begin(), s.end(), 'G') ) == 1) || count(s.begin(), s.end(), 'B') == (count(s.begin(), s.end(), 'G'))) 11 return true; 12 else 13 return false; 14 } 15 16 17 void printans(string &s) 18 { 19 20 if (count(s.begin(), s.end(), 'B') - count(s.begin(), s.end(), 'G') == 1) 21 { 22 int n = count(s.begin(), s.end(), 'B'); 23 for (int i = 0; i < n-1; i++) 24 cout << "BG"; 25 cout << "B" << endl; 26 } 27 if (count(s.begin(), s.end(), 'B') - count(s.begin(), s.end(), 'G') == -1) 28 { 29 int n = count(s.begin(), s.end(), 'G'); 30 for (int i = 0; i < n-1 ; i++) 31 cout << "GB"; 32 cout << "G" << endl; 33 } 34 if (count(s.begin(), s.end(), 'B') == count(s.begin(), s.end(), 'G')) 35 { 36 int n = count(s.begin(), s.end(), 'G'); 37 for (int i = 0; i < n ; i++) 38 cout << "BG"; 39 printf(" "); 40 } 41 42 } 43 44 45 46 47 48 int main() 49 { 50 51 string s; 52 while(cin >> s) 53 { 54 if (judge(s)) 55 printans(s); 56 else 57 cout << "No" << endl; 58 59 } 60 61 62 63 64 return 0; 65 }
WA 了
我就是想复习复习string