Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
1.5 kB
4
Indexable
Never
#include <iostream>
using namespace std;

int arr[11][10005];

int getNumber(char str[], int size) {
	int res = 0;
	for(int i = 0; i < size; i++) {
		int number = str[i] - '0';
		res = res * 10 + number;
	}
	return res;
}

void changeNumber(int number, char str[], int size) {
	for(int i = 0; i < size; i++) {
		char c = (number % 10) + '0';
		number /= 10;
		str[size - i - 1] = c;
	}
}

int main() {
	freopen("input.txt", "r", stdin);
	int T;
	cin >>T;

	for(int tc = 1; tc <= T; tc++) {
		char str[100];
		cin >>str;
		int N;
		cin >>N;
		int size = 0;
		while(str[size] != '\0') size++;
		int defaultValue = getNumber(str, size);
		arr[0][0] = defaultValue;

		int count = 0, count2 = 1, max = 0;
		for(int l = 0; l < N; l++) {
			count = count2;
			count2 = 0;
			for(int k = 0; k < count; k++) {
				changeNumber(arr[l][k], str, size); 

				for(int i = 0; i < size - 1; i++) {
					for(int j = i+1; j < size; j++) {
						char tmp = str[j];
						str[j] = str[i];
						str[i] = tmp;
						int value = getNumber(str, size);


						bool existed = false;
						for(int m = 0; m < count2; m++) {
							if(arr[l+1][m] == value) {
								existed = true;
								break;
							}
						}

						if(!existed) arr[l+1][count2++] = value;
						if(l == N-1 && max < value) max = value;

						char tmp2 = str[j];
						str[j] = str[i];
						str[i] = tmp2;
					}
				}
			}
		}

		cout <<"Case #" <<tc <<"\n" <<max <<endl;
	}

	return 0;
}