本文主要是介绍C - Minimize The Integer,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
思路:
(1)注意到总体规律是奇数不能跨越奇数,偶数不能跨越偶数
(2)冒泡不可取;
(3)直接队列分别存奇偶,小的往前放就行。
代码:
#include<bits/stdc++.h>
using namespace std;int main()
{int t;cin >> t;cin.tie(0);while(t --){string x;cin >> x;queue<char> a,b;for(int i = 0;i < x.size();i ++)if((x[i]- '0') % 2 == 0)a.push(x[i]);else b.push(x[i]);string res = "";while(!a.empty() && !b.empty()){if(a.front() >= b.front()){res += b.front();b.pop();}else{res += a.front();a.pop();}}while(!a.empty()){res += a.front();a.pop();}while(!b.empty()){res += b.front();b.pop();}cout << res << endl; }return 0;
}
这篇关于C - Minimize The Integer的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!