輸入一個正整數數組,把數組里所有數字拼接起來排成一個數,打印能拼接出的所有數字中最小的一個。例如輸入數組{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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 [Python]-輸入一個正整數數組,把數組里所有數字拼接起來排成一個數,打印能拼接出的所有數字中最小的一個。 輸入一個正整數數組,把數組里所有數字拼接起來排成一個數,打印能拼接出的所有數字的最小的一個 劍指Offer(Java版)第五十六題:在一個長度為n的數組里的所有數字都在0到n-1的范圍內。 數組中某些數字是重復的,但不知道有幾個數字是重復的。 也不知道每個數字重復幾次。請找出數組中任意一個重復的數字。 例如,如果輸入長度為7的數組{2,3,1,0,2,5,3},那么對應的輸出是第一個重復的數字2。 面試題3:在一個長度為n的數組里的所有數字都在0到n-1的范圍內。 數組中某些數字是重復的,但不知道有幾個數字是重復的。也不知道每個數字重復幾次。請找出數組中任意一個重復的數字。 例如,如果輸入長度為7的數組{2,3,1,0,2,5,3},那么對應的輸出是第一個重復的數字2。 在一個長度為n的數組里的所有數字都在0到n-1的范圍內。 數組中某些數字是重復的,但不知道有幾個數字是重復的。也不知道每個數字重復幾次。請找出數組中任意一個重復的數字。 例如,如果輸入長度為7的數組{2,3,1,0,2,5,3},那么對應的輸出是重復的數字2或者3 劍指offer(Java版)第一題:在一個長度為n的數組里的所有數字都在0到n-1的范圍內。 數組中某些數字是重復的,但不知道有幾個數字重復了,也不知道每個數字重復了幾次。 *請找出數組中任意一個重復的數字。 *例如,如果輸入長度為7的數組{2, 3, 1, 0, 2, 5, 3},那么對應的輸出是重復的數字2或者3。 在一個長度為n的數組里的所有數字都在0到n-1的范圍內。 數組中某些數字是重復的,但不知道有幾個數字是重復的。也不知道每個數字重復幾次。請找出數組中任意一個重復的數字。 例如,如果輸入長度為7的數組{2,3,1,0,2,5,3},那么對應的輸出是重復的數字2或者3。 如何將一個數字字符串數組轉化為數字整數數組? 把一個數組forEach循環出來的值用“,”拼接起來 輸入n個整數,找出其中最小的K個數。例如輸入4,5,1,6,2,7,3,8這8個數字,則最小的4個數字是1,2,3,4,。
 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM