Next Number with Same Set of Digits

本文介绍了一个算法,用于找出一个整数的所有可能排列中紧接其后的最小数字。通过交换和反转操作实现,适用于数字处理和排序算法的学习。

Problem: Reorder the digits of a number, in order to get the next number which is the least one that is greater than the input number. For example, the number 34724126 is the next number of 34722641 when reordering digits.

/* Copyleft: Ming Lin <minggr@gmail.com> */

#include <stdio.h>

void reverse(int *data, int n)
{
	int *s, *e;

	s = data;	
	e = data + (n-1);
	while (s < e) {
		int tmp;

		tmp = *s;
		*s = *e;
		*e = tmp;
		s++;
		e--;
	}
}

/*
 * 34722641 -> swap 2 and 4
 * 34724621 -> reverse "621"
 * 34724126
 */
int least_greater_number(int *data, int n)
{
	int tmp;
	int i, j;	

	i = n-1;
	tmp = data[i--];
	while (i >= 0) {
		if (data[i] < tmp)	
			break;
		tmp = data[i--];
	}

	if (i < 0)
		return 0;

	j = n;
	while (j--) {
		if (data[j] > data[i])
			break;
	}

	tmp = data[i];
	data[i] = data[j];
	data[j] = tmp;

	reverse(&data[i+1], n-i-1);

	return 1;
}

void print_data(int *data, int n)
{
	int i;

	for (i = 0; i < n; i++)
		printf("%d", data[i]);
	printf("\n");
}

int main()
{
	int data[] = {3, 4, 7, 2, 2, 6, 4, 1};
	int n = 8;

	print_data(data, n);
	if (least_greater_number(data, n))
		print_data(data, n);

	return 0;
}


c++题解,代码无注释 T-3 Artificial Intelligence Detective-Fuzzy Matching 分数 35 作者 陈越 单位 浙江大学 Artificial intelligence technology has made great progress in the field of criminal investigation. It can identify the facial features of any person in a crowd, match the facial features of all criminals in the database, and automatically identify the criminals among the people. Now you are asked to implement a simple auto-recognition program to determine whether a person is a criminal based on the 5 facial features extracted by the system. These 5 features are: interpupillary distance (瞳距), nose length, nose width, lip width, and lip thickness. A certain range of matching errors is allowed -- that is, for a captured face, if the relative error between each of its features and the corresponding features of a criminal does not exceed a given threshold T, the match is considered successful. (Note: The relative error between the approximation A and the criminal's exact value B is defined as ∣A−B∣/∣B∣.) Input Specification: The first line of input gives a positive integer N (≤10 5 ) and a positive real number T (≤0.20), which are the number of criminals in the database, and the upper limit threshold of the matching relative error, respectively. Then N lines follow, each contains 5 integers, separated by spaces. The i-th line gives the facial features of the i-th criminal (1≤i≤N) in the format: Feature1 Feature2 …… Feature5 ID where each Feature is an integer in the interval [1,10 7 ]; ID is the criminal's number, a positive integer not exceeding 6 digits. Note that features at different positions correspond to different types, for example, Feature1 corresponds to interpupillary distance, Feature2 corresponds to nose length, and so on. It is guaranteed that no two criminals have exactly the same facial features, and all ID's are different. Then in the next line, a positive integer M (≤10 6 ) is given, followed by M lines, each gives a set of facial features of a captured face, and the features are also integers within the interval [1,10 7 ]. Output Specification: For each captured face, if the relative error between each of its features and the corresponding features of a criminal does not exceed the given threshold T, the match is considered successful. Print all matched criminal numbers in ascending order on a single line. In case no criminal is matched, output Pass instead. All the numbers in a line must be separated by exactly space, and there must be no extra spaces at the beginning or the end of the line. Sample Input: 9 0.10 15 23 20 35 12 000001 12 32 15 18 5 200001 29 35 25 48 24 987654 15 22 18 37 11 693112 15 32 20 40 18 123456 9 16 8 15 4 745210 15 24 19 33 13 023333 18 25 16 30 14 182539 16 22 18 36 11 593112 4 9 16 8 15 4 16 29 21 36 19 30 35 25 48 27 15 23 18 35 12 Sample Output: 745210 123456 Pass 000001 023333 593112 693112 代码长度限制 16 KB 时间限制 10000 ms 内存限制 64 MB 栈限制 8192 KB
08-21
请运用c++线性方法题解,代码无注释 T-3 Artificial Intelligence Detective-Fuzzy Matching 分数 35 作者 陈越 单位 浙江大学 Artificial intelligence technology has made great progress in the field of criminal investigation. It can identify the facial features of any person in a crowd, match the facial features of all criminals in the database, and automatically identify the criminals among the people. Now you are asked to implement a simple auto-recognition program to determine whether a person is a criminal based on the 5 facial features extracted by the system. These 5 features are: interpupillary distance (瞳距), nose length, nose width, lip width, and lip thickness. A certain range of matching errors is allowed – that is, for a captured face, if the relative error between each of its features and the corresponding features of a criminal does not exceed a given threshold T, the match is considered successful. (Note: The relative error between the approximation A and the criminal’s exact value B is defined as ∣A−B∣/∣B∣.) Input Specification: The first line of input gives a positive integer N (≤10 5 ) and a positive real number T (≤0.20), which are the number of criminals in the database, and the upper limit threshold of the matching relative error, respectively. Then N lines follow, each contains 5 integers, separated by spaces. The i-th line gives the facial features of the i-th criminal (1≤i≤N) in the format: Feature1 Feature2 …… Feature5 ID where each Feature is an integer in the interval [1,10 7 ]; ID is the criminal’s number, a positive integer not exceeding 6 digits. Note that features at different positions correspond to different types, for example, Feature1 corresponds to interpupillary distance, Feature2 corresponds to nose length, and so on. It is guaranteed that no two criminals have exactly the same facial features, and all ID’s are different. Then in the next line, a positive integer M (≤10 6 ) is given, followed by M lines, each gives a set of facial features of a captured face, and the features are also integers within the interval [1,10 7 ]. Output Specification: For each captured face, if the relative error between each of its features and the corresponding features of a criminal does not exceed the given threshold T, the match is considered successful. Print all matched criminal numbers in ascending order on a single line. In case no criminal is matched, output Pass instead. All the numbers in a line must be separated by exactly space, and there must be no extra spaces at the beginning or the end of the line. Sample Input: 9 0.10 15 23 20 35 12 000001 12 32 15 18 5 200001 29 35 25 48 24 987654 15 22 18 37 11 693112 15 32 20 40 18 123456 9 16 8 15 4 745210 15 24 19 33 13 023333 18 25 16 30 14 182539 16 22 18 36 11 593112 4 9 16 8 15 4 16 29 21 36 19 30 35 25 48 27 15 23 18 35 12 Sample Output: 745210 123456 Pass 000001 023333 593112 693112 代码长度限制 16 KB 时间限制 10000 ms 内存限制 64 MB 栈限制 8192 KB
08-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值