沐里纷纷


私信TA

用户名:Epoch

访问量:62812

签 名:

我不会算法

等  级
排  名 37
经  验 12818
参赛次数 1
文章发表 172
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

不会算法

解题思路:

递归啊递归,题目要求,楼上大佬们都没注意吧

注意事项:

  1. 考虑到输入可能很长,就用printf代替cout

  2. a[i]和a[j]之间有一个空格......



参考代码:

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stdio.h>

using namespace std;

vector<int> A;

void chooseSort(vector<int>::iterator start)
{
	if (start == A.end())
		return;
	vector<int>::iterator nextElem = ++start;
	--start;
	vector<int>::iterator minElem = min_element(start, A.end());
	int temp = *minElem;
	*minElem = *start;
	*start = temp;
	printf("swap(a[%d], a[%d]):", start - A.begin(), minElem - A.begin());
	for (int i = 0; i < A.size(); i++)
		printf("%d ", A[i]);
	printf("\n");
	chooseSort(nextElem);
}

int main(int argc, char** argv)
{
	int n = 0;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int temp;
		cin >> temp;
		A.push_back(temp);
	}
	chooseSort(A.begin());
	return 0;
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区