C - Minimize The Integer

2023-10-28 03:20
文章标签 integer minimize

本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/290342

相关文章

A. Minimize!

time limit per test 1 second memory limit per test 256 megabytes You are given two integers aa and bb (a≤ba≤b). Over all possible integer values of cc (a≤c≤ba≤c≤b), find the minimum value of (c−a)

如何简便的将List<Integer>转换成int[]?

使用Java 8的流(Streams)  ArrayList<Integer> list = new ArrayList<>();int[] intArray = list.stream().mapToInt(Integer::intValue).toArray();  若是maven项目可使用Apache Commons Lang库 <dependency> <groupId>

[LeetCode] 7. Reverse Integer

题:https://leetcode.com/problems/reverse-integer/description/ 题目 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123Output: 321Example 2:Input: -123Output: -321Ex

LeetCode - 12. Integer to Roman

12. Integer to Roman  Problem's Link  ---------------------------------------------------------------------------- Mean:  将一个int型的整数转化为罗马数字. analyse: 没什么好说的,直接维基百科. Time complexity: O(

带小数的String转整数Integer

其实String和Integer、Float、Double等相互转换这都很容易。可是带小数的String转Float、Double可能会出现“模糊数字”。 那么怎么避免呢?见下实例和结论。 System.out.println("**********2.4***********");String a = "2.4"; System.out.println(a); // 2.4System.o

Insertion Sort Integer Array Insertion Sort Linked List

Sort Integer Array using Insertion sort. //********************************************************************************************************/* Insertion Sort 原理:就是前面的sort部分全部是相对值,从后面拿一个元素,然后跟

Subtract the Product and Sum of Digits of an Integer

Given an integer number n, return the difference between the product of its digits and the sum of its digits. Example 1: Input: n = 234Output: 15 Explanation: Product of digits = 2 * 3 * 4 = 24

Redis的incr命令引发的反序列化异常和ERR value is not an integer or out of range异常

在Java中使用inc命令的时候发现redis中的值被反序列化后居然不是数字,检查后发现可能是序列化器没对,在redis配置的地方将序列化器设置为 Jackson2JsonRedisSerializer后使用整成,贴上代码 @Bean(name = "RedisTemplate")@SuppressWarnings("all")public RedisTemplate<String,

java 2.6** Summing the digits in an integer

例如 : 999 process: sum=9+9+9=27; 例如 : 932 process: sum=9+3+2=14;   import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner input=new Scanner(System.in);int

Integer的自动装箱过程

先来看道题  int  a=100;  int  b=100;  Integer  c=a;  Integer  d=b;  System.out.println(a==b);  System.out.println(c==d); 其实这道题  和  a 与  b 没有什么关系,可以直接看成   Integer c=100;   Integer d=100; System.out.