【多校联赛】The Crime-solving Plan of Groundhog

The Crime-solving Plan of Groundhog

题意:

给定n个介于0到9之间的数字,请使用它们生成两个正整数,且不带前导零,乘积最小化。输出乘积。

题解:

选出最小的挑出来(非零),剩下的组成最小值,相乘

个人问题:

首先没有考虑到大数,其次arr[i] = arr[i] * k + ans;和arr[i] *= k + ans;含义不同

代码:
///The Crime-solving Plan of Groundhog(大数+最小排序)
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
bool P(int x, int y) { return x < y; }
int arr[1000000];
long long int getbug(int n) {
	long long int ans = 0,S=1;
	for (int i = n-1; i >=0; i--) {
		S *= 10;
		ans += arr[i] * S/10;
	}
	return ans;
}
int main(){
	long long int T;
	cin >> T;
	while (T--) {
		long long int n, m, k;
		cin >> n >> k;
		for (long long int i = 0; i < n-1; i++) {
			cin >> m;
			if (m != 0 && m < k) {
				k = k + m;
				m = k - m;
				k = k - m;
			}
			if (k == 0 && m != 0) {
				k = k + m;
				m = k - m;
				k = k - m;
			}
			arr[i] = m;
		}
		sort(arr, arr + n-1,P);
		if (arr[0] == 0) {
			for (long long int i = 1; i < n - 1; i++) {
				if (arr[i] != 0) {
					arr[i] = arr[i] + arr[0];
					arr[0] = arr[i] - arr[0];
					arr[i] = arr[i] - arr[0];
					break;
				}
			}
		}
		int ans=0;
		for (int i = n - 2; i >= 0; i--) {
			arr[i] = arr[i] * k + ans;
			ans = arr[i] / 10;
			arr[i] %= 10;
		}
		if (ans) cout << ans;
		for (int i = 0; i <= n - 2; i++) {
			cout << arr[i];
		}
		cout << endl;
	}
	return 0;
}

你可能感兴趣的:(题解)