程序设计基础11 深度优先搜索(2)

The K−P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K−P factorization of N for any positive integers N, K and P.

Input Specification:

Each input file contains one test case which gives in a line the three positive integers N (≤400), K (≤N) and P (1<P≤7). The numbers in a line are separated by a space.

Output Specification:

For each case, if the solution exists, output in the format:

N = n[1]^P + ... n[K]^P

where n[i] (i = 1, ..., K) is the i-th factor. All the factors must be printed in non-increasing order.

Note: the solution may not be unique. For example, the 5-2 factorization of 169 has 9 solutions, such as 12​2​​+4​2​​+2​2​​+2​2​​+1​2​​, or 11​2​​+6​2​​+2​2​​+2​2​​+2​2​​, or more. You must output the one with the maximum sum of the factors. If there is a tie, the largest factor sequence must be chosen -- sequence { a​1​​,a​2​​,⋯,a​K​​ } is said to be larger than { b​1​​,b​2​​,⋯,b​K​​ } if there exists 1≤L≤K such that a​i​​=b​i​​ for i<L and a​L​​>b​L​​.

If there is no solution, simple output Impossible.

Sample Input 1:

169 5 2

Sample Output 1:

169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2

Sample Input 2:

169 167 3

Sample Output 2:

Impossible

一, 我的思想

    DFS()函数只有一个参数,就是当前的P次方的和,其他的参数:

    1,当前下标:用开头的for搜索,为了使后一个比前一个小,通过全局变量num存储vector末尾元素,for从num开始往后循环。

     2,当前矢量数组中元素个数:不设这个变量,直接在if中判定vec.size()与K的大小关系。

     3,当前数组中元素和,在搜索到符合条件的数组时候在与全局变量比较。

    缺点:不好维护,不模块化。

二,正确思想:

    把这三个参数和当前的P次方的和都添加到DFS()的形参中,同时把P次方到N的那些元素和结果用vector存起来,元素是下标,结果是数组中的元素。

三,小tip:

1,把一个vector的值赋给另一个vector不能用

for (int i = 0; i < K; i++) {answer[i] = temp[i];}

而应当用

answer = temp;

直接相等即可。

2,不要被例子带着走,像判断nowK与K的关系,应当是nowK > K,而当初是nowK > 5,还有其他的几个参数用了例子中的常量,切记,不要被例子带着走。

四,我的代码

#include<cstdio>
#include<vector>
#include<math.h>
using namespace std;
//到17:05
int N = 0;
int K = 0;
int P = 0;
int sum_1 = -1;
int num = 0;
int countting = 0;
int arr[500] = { 0 };
int simu[500] = { 0 };
vector<int> vec;
void compare(int arr_1[]) {
	int sum = 0;
	int flag = 0;
	for (int i = 0; i < K; i++) {
		sum += arr_1[i];
	}
	if (sum > sum_1) {
		sum_1 = sum;
		for (int i = 0; i < K; i++) {
			arr[i] = arr_1[i];
		}
	}
	num = arr[0];
}
void DFS(int sum) {
	for (int i = num; i > 0; i--) {
		int P_mul = 1;
		for (int j = 0; j < P; j++) {
			P_mul *= i;
		}
		if ((vec.size() < K) && (sum + P_mul <= N)) {
			vec.push_back(i);
			num = i;
			DFS(sum + P_mul);
			if (vec.size() == K && sum + P_mul == N) {
				countting++;
				vector<int>::iterator it = vec.begin();
				for (int k = 0; k < K; k++) {
					simu[k] = *(it + k);
				}
				compare(simu);
			}
			vec.pop_back();
		}
	}
}
int main() {
	scanf("%d %d %d", &N, &K, &P);
	num = pow(N, 1.0 / P);
	DFS(0);
	if (countting == 0) {
		printf("Impossible");
		return 0;
	}
	printf("%d = ", N);
	for (int i = 0; i < K; i++) {
		printf("%d^2", arr[i]);
		if (i != K - 1) {
			printf(" + ");
		}
	}
	return 0;
}

五,正确代码:

#include<cstdio>
#include<vector>
#include<math.h>
using namespace std;
//到17:05
int N = 0, K = 0, P = 0;
int maxSum = -1;
vector<int> fac, answer, temp;
int pow(int num) {
	int ans = 1;
	for (int i = 0; i < P; i++) {
		ans *= num;
	}
	return ans;
}
void init() {
	int temp = 0;
	int i = 0;
	while (temp<=N) {
		fac.push_back(temp);
		temp = pow(++i);
	}
}
void DFS(int index, int nowK, int sum, int facsum) {
	if (sum == N&&nowK == K) {
		if (facsum > maxSum) {
			maxSum = facsum;
			answer = temp;//for (int i = 0; i < K; i++) {answer[i] = temp[i];}这样写是不对的
		}
		return;
	}
	if (nowK > K || sum > N)return;           //当初>=不对     当初nowK > 5不对
	if(index - 1 >= 0) {
		temp.push_back(index);
		DFS(index, nowK + 1, sum + fac[index], facsum + index);
		temp.pop_back();
		DFS(index - 1, nowK, sum, facsum);
	}
}
int main() {
	scanf("%d %d %d", &N, &K, &P);
//	int num = pow(N, 1.0 / P);
	init();
	DFS(fac.size()-1,0,0,0);
	if (maxSum == -1) {
		printf("Impossible");
		return 0;
	}
	printf("%d = ", N);
	for (int i = 0; i < K; i++) {
		printf("%d^%d", answer[i],P);
		if (i != K - 1) {
			printf(" + ");
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq2285580599/article/details/82555657
今日推荐