結果
| 問題 | No.39 桁の数字を入れ替え | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2017-04-28 21:54:38 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 5,000 ms | 
| コード長 | 2,808 bytes | 
| コンパイル時間 | 620 ms | 
| コンパイル使用メモリ | 61,048 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-10-02 07:19:08 | 
| 合計ジャッジ時間 | 1,214 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 19 | 
ソースコード
#include <iostream>
#include <string>
#include <vector>
int main(void)
{
	int N, N_buff;
	
	std::vector<int> a;
	
	std::cin >> N;
	
	N_buff = N;
	
	int befor_i, after_i, befor, after;
	
	for(int i = 0; N_buff > 0;i ++)
	{	
		a.push_back(N_buff % 10);
		
		N_buff -= a[i];
		
		N_buff /= 10;
	}
	
	std::vector<int> number;
	std::vector<int> th;
	
	//for debug
	/*
	for(int i = 0; i < a.size(); i ++)
	{
		std::cout << a[i];
	}
	
	std::cout << std::endl;
	*/
	
	for(int n = 9; n >= 1; n --)
	{
		for(int i = 0; i < a.size(); i ++)
		{
			if(a[i] == n)
			{
				number.push_back(a[i]);
				th.push_back(i);
			}
		}
	}
	
	//for debug
	/*
	std::cout << std::endl;
	
	for(int i = 0; i < a.size(); i ++)
	{
		std::cout << number[i];
	}
	
	std::cout << std::endl;
	
	for(int i = 0; i < a.size(); i ++)
	{
		std::cout << th[i];
	}
	
	std::cout << std::endl << std::endl;
	*/
	
	bool success = false;
	
	for(int i = 0; i < a.size() - 1; i ++)
	{
		int buffer = number[i];
		int candidate = th[i];
		int buffer2;
		
		for(int j = i + 1; j < a.size(); j ++)
		{
			if(buffer > number[j] && candidate < th[j])
			{
				//std::cout << "yes (" << number[i] << ")" << std::endl;
			
				candidate = th[j];
				
				//std::cout << "candidate = " << candidate << std::endl;
				
				success = true;
				
				buffer2 = j;
			}
		}
		
		if(success)
		{
			//std::cout << candidate << std::endl;
			number[i] = number[buffer2];
			number[buffer2] = buffer;
			
			goto OUT;
		}
		//else
		//{
			//std::cout << "no (" << number[i] << ")" << std::endl;
		//}
	}
	
	OUT:
	
	//for debug
	/*
	std::cout << std::endl << std::endl;
	
	for(int i = 0; i < a.size(); i ++)
	{
		std::cout << number[i];
	}
	
	std::cout << std::endl;
	
	for(int i = 0; i < a.size(); i ++)
	{
		std::cout << th[i];
	}
	
	std::cout << std::endl << std::endl;
	*/
	
	for(int i = a.size() - 1; i >= 0; i --)
	{
		for(int j = 0; j < a.size(); j ++)
		{
			if(th[j] == i)
			{
				std::cout << number[j];
			}
		}
	}
	
	std::cout << std::endl;
	
	//for debug
	/*
	for(int i = 0; i < a.size(); i ++)
	{
		std::cout << number[i];
	}
	
	std::cout << std::endl;
	
	for(int i = 0; i < a.size(); i ++)
	{
		std::cout << th[i];
	}
	
	std::cout << std::endl;
	*/
	/*
	after = 0;
	
	for(int i = 0; i < a.size(); i ++)
	{
		if(a[i] > after)
		{
			after = a[i];
			after_i = i;
		}
	}
	
	
	//for debug
	std::cout << a[after_i] << ", " << after_i << std::endl;
	
	bool boolba = false;
	
	for(int i = a.size() - 1; i >= after; i --)
	{
		if(after > a[i])
		{
			befor = a[i];
			befor_i = i;
			a[i] = after;
			boolba = true;
			break;
		}
	}
	
	//for debug
	
	if(boolba)
	{
		std::cout << befor << ", " << befor_i << std::endl;
	
		a[after_i] = befor;
	}
	
	for(int i = a.size() - 1; i >= 0; i --)
	{
		std::cout << a[i];
	}
	
	std::cout << std::endl;
	*/
	
	return 0;
}
            
            
            
        