zoukankan      html  css  js  c++  java
  • 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。

    // ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
    //

    #include "stdafx.h"
    #include <iostream>
    #include <vector>
    #include <string>
    #include<sstream>
    #include <deque>
    using namespace std;
    class Solution {
    public:
    	string PrintMinNumber(vector<int> numbers) {
    		string str;
    		for (int i = 0; i < numbers.size(); ++i)
    		{
    		
    			for (int j = i + 1; j < numbers.size(); ++j)
    			{
    				int temp = CompareNums(numbers[i],numbers[j]);
    				if (temp == numbers[i])continue;//如果num的值就是结果值,返回继续循环
    				numbers[j] = numbers[i];
    				numbers[i] = temp;
    			}
    		}
    	
    		
    		
    	
    		for (int i = 0; i < numbers.size(); ++i)
    		{
    			stringstream stream;  //这块用到类型转换!!!!!!!!!!!!!
    			stream << numbers[i];//注意复习C++中类型转换!!!!!!!!!!!
    			str += stream.str();
    			stream.clear();
    			
    		}
    		cout << "str:" << str << endl;
    		return str;
    	}
    	int CompareNums(int num1,int num2)//选出两个数中位数较高的数字,比较小的那个数
    	{
    		deque <int> deq1;
    		deque <int> deq2;
    		deque <int> deq11;//辅助队列, 当num1在前面时
    		deque <int> deq22;//辅助队列,当num2在前面时
    		int copyNum1 = num1;
    		int copyNum2 = num2;
    		int num3=num1;//辅助数字,当num3等于0的时候返回哪个数字都可以,最好返回小的数字
    		while (copyNum1!=0)
    		{
    			deq1.push_front(copyNum1 % 10);
    			deq11.push_front(copyNum1 % 10);
    		//	cout << "copyNum1%10:" << copyNum1 % 10 << endl;
    			copyNum1 = copyNum1 / 10;
    			
    		}
    
    		while (copyNum2 != 0)
    		{
    			deq2.push_front(copyNum2 % 10);
    			deq22.push_front(copyNum2 % 10);
    		//	cout << "copyNum2%10:" << copyNum2 % 10 << endl;
    			copyNum2 = copyNum2 / 10;
    		}
    
    		
    		while (!deq2.empty())
    		{
    			deq11.push_back(deq2.front());
    			deq2.pop_front();
    		}
    		
    		while (!deq1.empty ())
    		{
    			deq22.push_back(deq1.front());
    			deq1.pop_front();
    		}
    		cout << "deq11:";
    		for (auto it = deq11.begin(); it != deq11.end(); ++it)
    			cout << *it;
    		cout << endl;
    
    		cout << "deq22:";
    		for (auto it = deq22.begin(); it != deq22.end(); ++it)
    			cout << *it;
    		cout << endl;
    		while (!deq11.empty()&&!deq22.empty())//两者都不为空,两者长度肯定都是相等的
    		{
    			if (deq11.front() < deq22.front())
    			{
    				num3 = num1;
    				break;
    			}
    			else if (deq11.front() > deq22.front())
    			{
    				num3 = num2;
    				break;
    			}
    			else
    			{
    				deq11.pop_front();
    				deq22.pop_front();
    			}
    		}
    
    		return num3;
    	}
    };
    
    
    int main()
    {
    	Solution so;
    	vector<int> vec = { 3,32,321 };
    	string str = so.PrintMinNumber(vec);
    	/*cout << "结果是:" << endl;
    	int num3 = so.CompareNums(123, 121);
    	cout << "num3:" << num3 << endl;*/
    return 0;
    }
  • 相关阅读:
    111
    实验 12 综合练习二
    实验 11结构体
    作业 5 指针应用1
    实验 10 指针2
    实验9 指针1
    实验8 数组2
    实验7
    321
    实验9-2
  • 原文地址:https://www.cnblogs.com/wdan2016/p/6089386.html
Copyright © 2011-2022 走看看