結果

問題 No.39 桁の数字を入れ替え
ユーザー KlayKlay
提出日時 2017-04-28 21:54:38
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,808 bytes
コンパイル時間 641 ms
コンパイル使用メモリ 62,736 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-10 05:43:23
合計ジャッジ時間 1,450 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0